C++カスタム時間クラス

12088 ワード

時間制御に基づくシステムでは、時間の管理が重要である.これはシステム全体の駆動力であり、システム全体が正常に動作するかどうかの重要な要素でもあります.データの時間フォーマットは必ずしも一定ではないため、時間制御クラスには強い変換機能が必要であり、様々な時間フォーマットを互換性があることが要求される.
以下は自分が仕事の実践の中で整理した時間管理類で、攻撃の参考です.
#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);
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;
}


aaa