📄 cdate.cpp
字号:
#include "stdafx.h"#include "CDate.h"#include <stdio.h>//string CDate_T::dateTimeFormat = "%02d%02d%02d %02d:%02d:%02d.%d";//string CDate_T::dateTimeFormat = "%02d%02d%02d %02d:%02d:%02d";string CDate_T::dateTimeFormat = "%.04d-%.02d-%.02d %.02d:%.02d:%.02d";void CDate_T::setDateTimeFormat (const string & format){ dateTimeFormat = format;}CDate_T::CDate_T (const CDate_T & o){ tick = o.tick; mytime = o.mytime; }intCDate_T::calmytime (int t){ return ((t - 16 * 3600) % (24 * 3600)) * 10000;}intCDate_T::calDayStart (int t){ const int oneday = 24 * 3600; return (((t - 16 * 3600) / oneday) * oneday) + 16 * 3600;}CDate_T::CDate_T ():tick (0), mytime (0){ //dateTimeFormat = "%02d%02d%02d %02d:%02d:%02d.%d";}CDate_T::CDate_T (int t){ tick = t;// dateTimeFormat = "%02d%02d%02d %02d:%02d:%02d.%d"; mytime = calmytime (tick);}int CDate_T::getDayStart() const { return calDayStart( tick );}CDate_T::CDate_T (int t, int z_time){// dateTimeFormat = "%02d%02d%02d %02d:%02d:%02d.%d"; const int oneday = 24 * 3600; int s = z_time / (10000); int d1; if (s < 3600) { d1 = calDayStart (t + 3600); } else if (s > 23 * 3600) { d1 = calDayStart (t - 3600); } else { d1 = calDayStart (t); } mytime = z_time; tick = d1 + s;}CDate_T::CDate_T (int year, int mon, int day, int z_time){// dateTimeFormat = "%02d%02d%02d %02d:%02d:%02d.%d"; long s; struct tm stm; stm.tm_year = (year % 100) + 100; stm.tm_mon = mon - 1; stm.tm_mday = day; stm.tm_hour = 0; stm.tm_min = 0; stm.tm_sec = 0; stm.tm_isdst = 0; s = mktime (&stm); tick = s + z_time / 10000; mytime = z_time;}CDate_T::CDate_T (const string & s, const string & format){// dateTimeFormat = "%02d%02d%02d %02d:%02d:%02d.%d"; string f; char temp[100]; strncpy (temp, s.c_str (), 100); f = (format == "") ? dateTimeFormat : format; int year = 0, mon = 1, day = 1, hour = 0, min = 0, sec = 0, ms = 0; sscanf (temp, f.c_str (), &year, &mon, &day, &hour, &min, &sec, &ms); long st; struct tm stm; stm.tm_year = (year % 100) + 100; stm.tm_mon = mon - 1; stm.tm_mday = day; stm.tm_hour = hour; stm.tm_min = min; stm.tm_sec = sec; stm.tm_isdst = 0; st = mktime (&stm); tick = st; mytime = calmytime (tick); mytime += ms * 10;}intCDate_T::getTime () const{ return tick;}intCDate_T::getMyTime () const{ return mytime;}intCDate_T::getDetail (int *year, int *mon, int *day, int *hour, int *min, int *sec, int *ms){ long t = tick; struct tm *ptm = localtime (&t); if (year) *year = ptm->tm_year; if (mon) *mon = ptm->tm_mon; if (day) *day = ptm->tm_mday; if (hour) *hour = ptm->tm_hour; if (min) *min = ptm->tm_min; if (sec) *sec = ptm->tm_sec; if (ms) *ms = (mytime % 10000) / 10; return tick;}intCDate_T::year (){ long t = tick; struct tm *ptm = localtime (&t); return (ptm->tm_year)%100;}intCDate_T::mon (){ long t = tick; struct tm *ptm = localtime (&t); return ptm->tm_mon + 1 ;}intCDate_T::day (){ long t = tick; struct tm *ptm = localtime (&t); return ptm->tm_mday;}intCDate_T::hour (){ long t = tick; struct tm *ptm = localtime (&t); return ptm->tm_hour;}intCDate_T::minute (){ long t = tick; struct tm *ptm = localtime (&t); return ptm->tm_min;}intCDate_T::second (){ long t = tick; struct tm *ptm = localtime (&t); return ptm->tm_sec;}intCDate_T::ms (){ return (mytime % 10000) / 10;}string CDate_T::getDate (const string & format) const{ char temp[20]; long t = tick; struct tm * ptm = localtime (&t); sprintf (temp, format.c_str (), (ptm->tm_year) % 100, ptm->tm_mon + 1, ptm->tm_mday); return temp;}string CDate_T::getDateTime (const string & f) const{ string format; if (f == "") { format = dateTimeFormat; } else { format = f; } char temp[40]; long t = tick; struct tm * ptm = localtime (&t); sprintf (temp, format.c_str (), (ptm->tm_year+1900) % 10000, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec); return temp;}intCDate_T::addSeconds (int t){ tick += t; mytime = calmytime (tick) + mytime % 10000; return tick;}intCDate_T::addMyTime (int t){ int u = t % 10000; int s = mytime % 10000 + u; tick += t / 10000 + s / 10000; mytime = calmytime (tick); mytime += s % 10000; return tick;}intCDate_T::secTimeLen (const CDate_T & t) const{ return tick - t.tick;}intCDate_T::msTimeLen (const CDate_T & t) const{ int s = tick - t.tick; return (mytime % 10000 - t.mytime % 10000) / 10 + s * 1000;// return (((tick - t.tick)*10000) + (mytime-t.mytime)%10000)/10 ;}CDate_T CDate_T::operator+ (int miao){ CDate_T temp (*this); temp.addSeconds (miao); return temp;}CDate_T CDate_T::operator- (int miao){ CDate_T temp (*this); temp.addSeconds (-miao); return temp;}intCDate_T::comp (const CDate_T & t) const{ if (tick != t.tick) return tick - t.tick; return mytime - t.mytime;}bool CDate_T::operator== (const CDate_T & o) const{ return comp (o) == 0;}bool CDate_T::operator!= (const CDate_T & o) const{ return comp (o) != 0;}bool CDate_T::operator>= (const CDate_T & o) const{ return comp (o) >= 0;}bool CDate_T::operator<= (const CDate_T & o) const{ return comp (o) <= 0;}bool CDate_T::operator> (const CDate_T & o) const{ return tick > o.tick;}bool CDate_T::operator< (const CDate_T & o) const{ return tick < o.tick;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -