📄 cdate.h
字号:
/*tick : 用time()返回的秒数(从1970年到现在的秒数)mytime: GPS时间,距离当天0点的0.1ms数时间格式: 例如:"%02d%02d%02d %02d:%02d:%02d.%d" 其中整数内容从年、月、日、时、分、秒、毫秒排列 可以设置成"%02d%02d%02d %02d:%02d:%02d" 这样就没有ms举例如下:#include "CDate.h"#include <iostream>using namespace std;int main(){ CDate_T t( time(0) ); cout<<t.getDateTime()<<endl; int hour ; t.getDetail(0 , 0 , 0 , &hour ) ; cout<<"hour is "<<hour<<endl; cout<<"hour is "<<t.hour()<<endl; CDate_T lastDay0 = CDate_T( t.getDayStart() - 3600*24 ); cout<<t.secTimeLen( lastDay0) <<" sec from the begining of last day "<< endl; //现在距离昨天凌晨多少秒 t = CDate_T("020306 00:04:04.0"); cout<<t.getDateTime()<<endl; t = t + 3600 ; cout<<t.getDateTime()<<endl; t = CDate_T( t.getTime() , 10*60*10000 ) ; //从1:04调整到0:10 cout<<t.getDateTime()<<endl;}*/#ifndef CDATE_H#define CDATE_H#include <time.h>#include <string>#include <assert.h>using std::string;class CDate_T{ int tick; // seconds geting from time() functions int mytime; // number of 0.1ms from the begining of the days .public: static int calmytime (int t); // calc mytime , t is ticks static int calDayStart (int t); // calc ticks of the the begining of day public: CDate_T (); CDate_T (const CDate_T & o); CDate_T (int t); // t is ticks CDate_T (int t, int z_time); // t is ticks , z_time is mytime CDate_T (int year, int mon, int day, int z_time = 0); CDate_T (const string & s, const string & format = ""); void setDateTimeFormat (const string & format); int getDayStart() const ; int getTime () const; // get ticks int getMyTime () const; // get mytime string getDate (const string & format = "%02d%02d%02d") const; // output the string of date according format /* output the string of datetime according format */ string getDateTime (const string & format = "") const; /* get the detail the time infomation */ int getDetail (int *year = 0, int *mon = 0, int *day = 0, int *hour = 0, int *min = 0, int *sec = 0, int *ms = 0); int year (); int mon (); int day (); int hour (); int minute (); int second (); int ms (); int addSeconds (int t); // add seconds . mytime will be change correspondly int addMyTime (int t); // add mytime , ticks will be change correspondly int secTimeLen (const CDate_T & t) const; // seconds away from t . *this - t int msTimeLen (const CDate_T & t) const; // ms away from t , *this - t CDate_T operator+ (int miao); CDate_T operator- (int miao); bool operator== (const CDate_T & o) const; // comare between datetime . bool operator!= (const CDate_T & o) const; bool operator>= (const CDate_T & o) const; bool operator<= (const CDate_T & o) const; bool operator> (const CDate_T & o) const; bool operator< (const CDate_T & o) const;private: int comp (const CDate_T & t) const; static string dateTimeFormat;};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -