在一个基于时间控制的系统中,时间的管理很是重要。它是整个系统的驱动力,也是整个系统是否可以正常运行的关键因素。由于数据的时间格式不一定,所以要求时间控制类需要有较强的转换功能,能够兼容各种各样的时间格式。
以下是自己在工作实践中整理的一个时间管理类,进攻参考:
#pragma once
#include "hcore_global.h"
#include
//运行时间
class RunningTime {
public:
RunningTime();
RunningTime(const RunningTime&rt);
RunningTime(int y, int mn, int d, int h, int mi, int s, int mils = 0);
~RunningTime();
//年
inline void setYear(int y) {
_year = y;
}
inline int getYear() {
return _year;
}
//月
inline void setMonth(int m) {
_month = m;
}
inline int getMonth() {
return _month;
}
//日
inline void setDay(int d) {
_day = d;
}
inline int getDay() {
return _day;
}
//时
inline void setHour(int h) {
_hour = h;
}
inline int getHour() {
return _hour;
}
//分
inline void setMinute(int m) {
_minute = m;
}
inline int getMinute() {
return _minute;
}
//秒
inline void setSecond(int s) {
_second = s;
}
inline int getSecond() {
return _second;
}
//毫秒
inline void setMilliSecond(int ms) {
_milliSecond = ms;
}
inline int getMilliSecond() {
return _milliSecond;
}
//当前时间转换成从零点开始的秒数
int toSecondSince0();
void fromSecondSince0(int seconds);
//获取当前时间 hh:mm:ss
std::string getTimeStr();
void fromTimeStr(std::string str);
//获取当前时间(含毫秒) hh:mm:ss:mmmm
std::string getTimeMilliStr();
void fromTimeMilliStr(std::string str);
//获取当前日期 yyyy-MM-dd
std::string getDateStr();
void fromDateStr(std::string str);
//获取当前日期 yyyy年MM月dd日
std::string getDateStrEx();
void fromDateStrEx(std::string str);
//获取当前日期 yyyy年MM月dd日 hh:mm:ss
std::string getDateTimeStr();
void fromDateTimeStr(std::string str);
//获取当前日期 yyyy-MM-dd hh:mm:ss
std::string getDateTimeStrEx();
void fromDateTimeStrEx(std::string str);
//获取当前日期 yyyy-MM-ddThh:mm:ss
std::string getDateTimeStr_T();
void fromDateTimeStr_T(std::string str);
//获取当前日期(含毫秒) yyyy-MM-dd hh:mm:ss:mmmm
std::string getDateTimeMilliStr();
void fromDateTimeMilliStr(std::string str);
//获取OLE时间
double getOleTime();
void fromOleTime(double oleTime);
//获取整型时间值 从1970-01-01T00:00:00开始的时间
long getTimeInt();
void fromTimeInt(long time);
//步增
void stepUp(int stepMilliSecond = 1000);
//步减
void stepDown(int stepMilliSecond = 1000);
//时间加
void increase(int second);
//时间减
void decrease(int second);
//重载运算符
bool operator<=(RunningTime&rt);
bool operator>=(RunningTime&rt);
bool operator<(RunningTime&rt);
bool operator>(RunningTime&rt);
private:
int _year ;//年
int _month ;//月
int _day ;//日
int _hour ;//时
int _minute ;//分
int _second ;//秒
int _milliSecond ;//毫秒
};
#include "HCore/RunningTime.h"
#include
#include
#include
RunningTime::RunningTime() {
_year = 2018;//年
_month = 12;//月
_day = 25;//日
_hour = 11;//时
_minute = 36;//分
_second = 0;//秒
_milliSecond = 0;//毫秒
}
RunningTime::RunningTime(int y, int mn, int d, int h, int mi, int s, int mils) {
_year = y;
_month = mn;
_day = d;
_hour = h;
_minute = mi;
_second = s;
_milliSecond = mils;
}
RunningTime::RunningTime(const RunningTime&rt) {
_year = rt._year;
_month = rt._month;
_day = rt._day;
_hour = rt._hour;
_minute = rt._minute;
_second = rt._second;
_milliSecond = rt._milliSecond;
}
RunningTime::~RunningTime() {
}
int RunningTime::toSecondSince0() {
return _hour * 3600 + _minute * 60 + _second;
}
void RunningTime::fromSecondSince0(int seconds) {
_hour = seconds / 3600;
_minute = (seconds - _hour * 3600) / 60;
_second = seconds - _hour * 3600 - _minute * 60;
}
std::string RunningTime::getTimeStr() {
QString strTime;
strTime += QString("%0").arg(_hour, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_minute, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_second, 2, 10, QChar('0'));
return strTime.toStdString();
}
void RunningTime::fromTimeStr(std::string str) {
sscanf(str.c_str(), "%d:%d:%d"
, &(_hour)
, &(_minute)
, &(_second));
}
std::string RunningTime::getTimeMilliStr() {
QString strTime;
strTime += QString("%0").arg(_hour, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_minute, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_second, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_milliSecond, 4, 10, QChar('0'));
return strTime.toStdString();
}
void RunningTime::fromTimeMilliStr(std::string str) {
sscanf(str.c_str(), "%d:%d:%d:%d"
, &(_hour)
, &(_minute)
, &(_second)
, &(_milliSecond));
}
std::string RunningTime::getDateStr() {
QString strDate;
strDate += QString("%0").arg(_year, 4, 10, QChar('0'));
strDate += QStringLiteral("-");
strDate += QString("%0").arg(_month, 2, 10, QChar('0'));
strDate += QStringLiteral("-");
strDate += QString("%0").arg(_day, 2, 10, QChar('0'));
return strDate.toStdString();
}
void RunningTime::fromDateStr(std::string str) {
sscanf(str.c_str(), "%d-%d-%d"
, &(_year)
, &(_month)
, &(_day));
}
std::string RunningTime::getDateStrEx() {
QString strDate;
strDate += QString("%0").arg(_year, 4, 10, QChar('0'));
strDate += QStringLiteral("年");
strDate += QString("%0").arg(_month, 2, 10, QChar('0'));
strDate += QStringLiteral("月");
strDate += QString("%0").arg(_day, 2, 10, QChar('0'));
strDate += QStringLiteral("日");
return strDate.toStdString();
}
void RunningTime::fromDateStrEx(std::string str) {
sscanf(str.c_str(), "%d年%d月%d"
, &(_year)
, &(_month)
, &(_day));
}
std::string RunningTime::getDateTimeStr() {
QString strDate;
strDate += QString("%0").arg(_year, 4, 10, QChar('0'));
strDate += QStringLiteral("年");
strDate += QString("%0").arg(_month, 2, 10, QChar('0'));
strDate += QStringLiteral("月");
strDate += QString("%0").arg(_day, 2, 10, QChar('0'));
strDate += QStringLiteral("日");
QString strTime;
strTime += QString("%0").arg(_hour, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_minute, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_second, 2, 10, QChar('0'));
return (strDate + " " + strTime).toStdString();
}
void RunningTime::fromDateTimeStr(std::string str) {
sscanf(str.c_str(), "%d年%d月%d %d:%d:%d"
, &(_year)
, &(_month)
, &(_day)
, &(_hour)
, &(_minute)
, &(_second));
}
std::string RunningTime::getDateTimeStrEx() {
QString strDate;
strDate += QString("%0").arg(_year, 4, 10, QChar('0'));
strDate += QStringLiteral("-");
strDate += QString("%0").arg(_month, 2, 10, QChar('0'));
strDate += QStringLiteral("-");
strDate += QString("%0").arg(_day, 2, 10, QChar('0'));
QString strTime;
strTime += QString("%0").arg(_hour, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_minute, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_second, 2, 10, QChar('0'));
return (strDate + " " + strTime).toStdString();
}
void RunningTime::fromDateTimeStrEx(std::string str) {
sscanf(str.c_str(), "%d-%d-%d %d:%d:%d"
, &(_year)
, &(_month)
, &(_day)
, &(_hour)
, &(_minute)
, &(_second));
}
std::string RunningTime::getDateTimeStr_T() {
QString strDate;
strDate += QString("%0").arg(_year, 4, 10, QChar('0'));
strDate += QStringLiteral("-");
strDate += QString("%0").arg(_month, 2, 10, QChar('0'));
strDate += QStringLiteral("-");
strDate += QString("%0").arg(_day, 2, 10, QChar('0'));
strDate += "T";
QString strTime;
strTime += QString("%0").arg(_hour, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_minute, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_second, 2, 10, QChar('0'));
return (strDate + strTime).toStdString();
}
void RunningTime::fromDateTimeStr_T(std::string str) {
sscanf(str.c_str(), "%d-%d-%dT%d:%d:%d"
, &(_year)
, &(_month)
, &(_day)
, &(_hour)
, &(_minute)
, &(_second));
}
std::string RunningTime::getDateTimeMilliStr() {
QString strDate;
strDate += QString("%0").arg(_year, 4, 10, QChar('0'));
strDate += QStringLiteral("年");
strDate += QString("%0").arg(_month, 2, 10, QChar('0'));
strDate += QStringLiteral("月");
strDate += QString("%0").arg(_day, 2, 10, QChar('0'));
strDate += QStringLiteral("日");
QString strTime;
strTime += QString("%0").arg(_hour, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_minute, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_second, 2, 10, QChar('0'));
strTime += ":";
strTime += QString("%0").arg(_milliSecond, 4, 10, QChar('0'));
return (strDate + " " + strTime).toStdString();
}
void RunningTime::fromDateTimeMilliStr(std::string str) {
sscanf(str.c_str(), "%d-%d-%d %d:%d:%d:%d"
, &(_year)
, &(_month)
, &(_day)
, &(_hour)
, &(_minute)
, &(_second)
, &(_milliSecond));
}
double RunningTime::getOleTime() {
QDateTime qDateTime(QDate(_year, _month, _day), QTime(_hour, _minute, _second));
long t = qDateTime.toTime_t();
return (double)(25569 + t / 86400.0 + 8.0 / 24.0);
}
void RunningTime::fromOleTime(double oleTime) {
__time64_t time = (__time64_t)((oleTime - 25569)*86400.0 - 8 * 3600 + 0.5);
fromTimeInt(time);
}
long RunningTime::getTimeInt() {
QDateTime qDateTime(QDate(_year, _month, _day), QTime(_hour, _minute, _second));
return qDateTime.toTime_t();
}
void RunningTime::fromTimeInt(long time) {
QDateTime dateTime = QDateTime::fromTime_t(time);
_year = dateTime.date().year();
_month = dateTime.date().month();
_day = dateTime.date().day();
_hour = dateTime.time().hour();
_minute = dateTime.time().minute();
_second = dateTime.time().second();
}
void RunningTime::stepUp(int stepMilliSecond) {
_milliSecond += stepMilliSecond;
if (_milliSecond >= 1000) {
_second += _milliSecond / 1000;
_milliSecond = _milliSecond % 1000;
if (_second >= 60) {
_minute += _second / 60;
_second = _second % 60;
if (_minute >= 60) {
_hour += _minute / 60;
_minute = _minute % 60;
if (_hour >= 24) {
_day++;
_hour = _hour % 24;
}
}
}
}
}
void RunningTime::stepDown(int stepMilliSecond ) {
_milliSecond -= stepMilliSecond;
if (_milliSecond <= 0) {
_second--;
_milliSecond = 0;
if (_second < 0) {
_minute--;
_second = 59;
if (_minute < 0) {
_hour--;
_minute = 59;
if (_hour < 0) {
_hour = 23;
_day--;
}
}
}
}
}
void RunningTime::increase(int second) {
unsigned int timeSeconds = getTimeInt();
timeSeconds += second;
fromTimeInt(timeSeconds);
}
void RunningTime::decrease(int second) {
unsigned int timeSeconds = getTimeInt();
timeSeconds -= second;
fromTimeInt(timeSeconds);
}
bool RunningTime::operator<=(RunningTime&rt) {
return getTimeInt() <= rt.getTimeInt() ? true : false;
}
bool RunningTime::operator>=(RunningTime&rt) {
return getTimeInt() >= rt.getTimeInt() ? true : false;
}
bool RunningTime::operator<(RunningTime&rt) {
return getTimeInt() < rt.getTimeInt() ? true : false;
}
bool RunningTime::operator>(RunningTime&rt) {
return getTimeInt() > rt.getTimeInt() ? true : false;
}
aaa