00001
00011 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00012 #pragma once
00013 #endif
00014 #ifndef COMMON_DATETIME_H_
00015 #define COMMON_DATETIME_H_
00016
00017 #include <time.h>
00018
00019 namespace common
00020 {
00021
00026
00028 enum WEEKDAY
00029 {
00030 SUN, MON, TUE, WED, THU, FRI, SAT, INV_WEEKDAY
00031 };
00032
00034 enum MONTH
00035 {
00036 JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, INV_MONTH
00037 };
00038
00040 enum NAME_FORM
00041 {
00042 NAME_SHORT = 0x00,
00043 NAME_LONG = 0x01,
00044 NAME_LOWERCASE = 0x00,
00045 NAME_FIRST_UPPERCASE = 0x10,
00046 NAME_UPPERCASE = 0x20,
00047 NAME_SHORT_LOWERCASE = 0x00,
00048 NAME_SHORT_FIRST_UPPERCASE = 0x10,
00049 NAME_SHORT_UPPERCASE = 0x20,
00050 NAME_LONG_LOWERCASE = 0x01,
00051 NAME_LONG_FIRST_UPPERCASE = 0x11,
00052 NAME_LONG_UPPERCASE = 0x21,
00053 };
00054
00056 extern const tchar * const DEFAULT_FORMAT;
00057
00058 struct DATESPAN;
00059 struct TIMESPAN;
00060 struct TMSTRUCT;
00061 struct DATETIME;
00062
00064
00066 struct DATESPAN
00067 {
00068 private:
00069 int m_Years;
00070 int m_Months;
00071 int m_Weeks;
00072 int m_Days;
00073
00074 public:
00076 static DATESPAN Days(int n) { return DATESPAN(0, 0, 0, n); }
00078 static DATESPAN Day() { return Days(1); }
00080 static DATESPAN Weeks(int n) { return DATESPAN(0, 0, n, 0); }
00082 static DATESPAN Week() { return Weeks(1); }
00084 static DATESPAN Months(int n) { return DATESPAN(0, n, 0, 0); }
00086 static DATESPAN Month() { return Months(1); }
00088 static DATESPAN Years(int n) { return DATESPAN(n, 0, 0, 0); }
00090 static DATESPAN Year() { return Years(1); }
00091
00092 DATESPAN(int Years, int Months, int Weeks, int Days) : m_Years(Years), m_Months(Months), m_Weeks(Weeks), m_Days(Days) { }
00093
00094 DATESPAN & operator += (const DATESPAN &ds) { m_Years += ds.m_Years; m_Months += ds.m_Months; m_Weeks += ds.m_Weeks; m_Days += ds.m_Days; return *this; }
00095 DATESPAN & operator -= (const DATESPAN &ds) { m_Years -= ds.m_Years; m_Months -= ds.m_Months; m_Weeks -= ds.m_Weeks; m_Days -= ds.m_Days; return *this; }
00096 DATESPAN & operator *= (int n) { m_Years *= n; m_Months *= n; m_Weeks *= n; m_Days *= n; return *this; }
00097 DATESPAN operator - () const { return DATESPAN(-m_Years, -m_Months, -m_Weeks, -m_Days); }
00098 bool operator == (const DATESPAN &ds) const { return m_Years == ds.m_Years && m_Months == ds.m_Months && m_Weeks == ds.m_Weeks && m_Days == ds.m_Days; }
00099 bool operator != (const DATESPAN &ds) const { return m_Years != ds.m_Years || m_Months != ds.m_Months || m_Weeks != ds.m_Weeks || m_Days != ds.m_Days; }
00100
00102 int GetDays() const { return m_Days; }
00104 int GetWeeks() const { return m_Weeks; }
00106 int GetMonths() const { return m_Months; }
00108 int GetYears() const { return m_Years; }
00110 int GetTotalDays() const { return m_Weeks * 7 + m_Days; }
00111
00114 void Set(int Years, int Months, int Weeks, int Days) { m_Years = Years; m_Months = Months; m_Weeks = Weeks; m_Days = Days; }
00115 void SetDays(int n) { m_Days = n; }
00116 void SetWeeks(int n) { m_Weeks = n; }
00117 void SetMonths(int n) { m_Months = n; }
00118 void SetYears(int n) { m_Years = n; }
00120 };
00121
00122 inline DATESPAN operator + (const DATESPAN &ds1, const DATESPAN &ds2) { return DATESPAN(ds1.GetYears()+ds2.GetYears(), ds1.GetMonths()+ds2.GetMonths(), ds1.GetWeeks()+ds2.GetWeeks(), ds1.GetDays()+ds2.GetDays()); }
00123 inline DATESPAN operator - (const DATESPAN &ds1, const DATESPAN &ds2) { return DATESPAN(ds1.GetYears()-ds2.GetYears(), ds1.GetMonths()-ds2.GetMonths(), ds1.GetWeeks()-ds2.GetWeeks(), ds1.GetDays()-ds2.GetDays()); }
00124 inline DATESPAN operator * (const DATESPAN &ds, int n) { return DATESPAN(ds.GetYears()*n, ds.GetMonths()*n, ds.GetWeeks()*n, ds.GetDays()*n); }
00125 inline DATESPAN operator * (int n, const DATESPAN &ds) { return DATESPAN(ds.GetYears()*n, ds.GetMonths()*n, ds.GetWeeks()*n, ds.GetDays()*n); }
00126
00127
00129
00130
00131 struct TIMESPAN
00132 {
00133 public:
00136 static TIMESPAN Milliseconds(int8 Milliseconds) { return TIMESPAN(0, 0, 0, Milliseconds); }
00137 static TIMESPAN Millisecond() { return Milliseconds(1); }
00138 static TIMESPAN Seconds(int8 Seconds) { return TIMESPAN(0, 0, Seconds); }
00139 static TIMESPAN Second() { return Seconds(1); }
00140 static TIMESPAN Minutes(int Minutes) { return TIMESPAN(0, Minutes, 0); }
00141 static TIMESPAN Minutes() { return Minutes(1); }
00142 static TIMESPAN Hours(int Hours) { return TIMESPAN(Hours, 0, 0); }
00143 static TIMESPAN Hour() { return Hours(1); }
00144 static TIMESPAN Days(int Days) { return Hours(Days * 24); }
00145 static TIMESPAN Day() { return Days(1); }
00146 static TIMESPAN Weeks(int Weeks) { return Days(Weeks * 7); }
00147 static TIMESPAN Week() { return Weeks(1); }
00149
00151 int8 m_Diff;
00152
00154 TIMESPAN() { }
00156 explicit TIMESPAN(int8 Diff) { Set(Diff); }
00158 TIMESPAN(int Hours, int Minutes, int8 Seconds, int8 Milliseconds = 0) { Set(Hours, Minutes, Seconds, Milliseconds); }
00159
00160 void Set(int8 Diff) { m_Diff = Diff; }
00161 void Set(int Hours, int Minutes, int8 Seconds, int8 Milliseconds = 0);
00162
00163 bool operator == (const TIMESPAN &t) const { return m_Diff == t.m_Diff; }
00164 bool operator != (const TIMESPAN &t) const { return m_Diff != t.m_Diff; }
00165 bool operator <= (const TIMESPAN &t) const { return m_Diff <= t.m_Diff; }
00166 bool operator >= (const TIMESPAN &t) const { return m_Diff >= t.m_Diff; }
00167 bool operator < (const TIMESPAN &t) const { return m_Diff < t.m_Diff; }
00168 bool operator > (const TIMESPAN &t) const { return m_Diff > t.m_Diff; }
00169 TIMESPAN operator - () const { return TIMESPAN(-m_Diff); }
00170 TIMESPAN & operator *= (int n) { m_Diff *= n; return *this; }
00171 TIMESPAN & operator += (const TIMESPAN &t) { m_Diff += t.m_Diff; return *this; }
00172 TIMESPAN & operator -= (const TIMESPAN &t) { m_Diff -= t.m_Diff; return *this; }
00173
00175 bool IsShorterThan(const TIMESPAN &t) const { return Abs() < t.Abs(); }
00177 bool IsLongerThan(const TIMESPAN &t) const { return Abs() > t.Abs(); }
00178
00179 bool IsNull() const { return m_Diff == 0; }
00180 bool IsPositive() const { return m_Diff > 0; }
00181 bool IsNegative() const { return m_Diff < 0; }
00182
00183 int GetWeeks() const { return GetDays() / 7; }
00184 int GetDays() const { return GetHours() / 24; }
00185 int GetHours() const { return GetMinutes() / 60; }
00186 int GetMinutes() const { return (int)(GetSeconds() / 60); }
00187 int8 GetSeconds() const { return m_Diff / 1000; }
00188 int8 GetMilliseconds() const { return m_Diff; }
00189
00191 TIMESPAN Abs() const { return TIMESPAN(m_Diff < 0 ? -m_Diff : m_Diff); }
00192
00194 void ToString(tstring *Out, bool ShowMilliseconds = false) const;
00196 void ToString_Nice(tstring *Out) const;
00197 };
00198
00199 inline TIMESPAN operator * (const TIMESPAN &t, int n) { return TIMESPAN(t.m_Diff * n); }
00200 inline TIMESPAN operator * (int n, const TIMESPAN &t) { return TIMESPAN(t.m_Diff * n); }
00201 inline TIMESPAN operator + (const TIMESPAN &t1, const TIMESPAN &t2) { return TIMESPAN(t1.m_Diff + t2.m_Diff); }
00202 inline TIMESPAN operator - (const TIMESPAN &t1, const TIMESPAN &t2) { return TIMESPAN(t1.m_Diff - t2.m_Diff); }
00203
00205
00208 struct TMSTRUCT
00209 {
00210 private:
00211 uint2 msec, sec, min, hour, mday;
00212 MONTH mon;
00213 int year;
00214
00215 mutable WEEKDAY wday;
00216
00218 void ComputeWeekDay() const;
00219
00220 public:
00221
00223 TMSTRUCT() : wday(INV_WEEKDAY) { }
00225 explicit TMSTRUCT(const DATETIME &dt) { Set(dt); }
00226 explicit TMSTRUCT(const struct tm &tm) { Set(tm); }
00227 TMSTRUCT(uint2 Day, MONTH Month, int Year, uint2 Hour, uint2 Minute, uint2 Second, uint2 Millisec = 0) { Set(Day, Month, Year, Hour, Minute, Second, Millisec); }
00228 TMSTRUCT(uint2 Day, MONTH Month, int Year) { Set(Day, Month, Year, 0, 0, 0); }
00229
00230 void Set(const DATETIME &dt);
00231 void Set(const struct tm &tm);
00232
00233 int GetYear() const { return year; }
00234 MONTH GetMonth() const { return mon; }
00235 uint2 GetDay() const { return mday; }
00236 uint2 GetHour() const { return hour; }
00237 uint2 GetMinute() const { return min; }
00238 uint2 GetSecond() const { return sec; }
00239 uint2 GetMillisecond() const { return msec; }
00240
00242 WEEKDAY GetWeekDay() const
00243 {
00244 if (wday == INV_WEEKDAY)
00245 ComputeWeekDay();
00246 return (WEEKDAY)wday;
00247 }
00248
00249 TMSTRUCT & operator += (const DATESPAN &d) { Add(d); return *this; }
00250 TMSTRUCT & operator -= (const DATESPAN &d) { Subtract(d); return *this; }
00251
00252 void Add(const DATESPAN &d);
00253 void Subtract(const DATESPAN &d) { Add(-d); }
00254
00256 bool IsValid() const;
00257 bool IsSameDate(const TMSTRUCT &tm) const;
00258 bool IsSameTime(const TMSTRUCT &tm) const;
00259
00261
00262 void AddMonths(int MonDiff);
00264 void AddDays(int DayDiff);
00265
00267 void Set(uint2 Day, MONTH Month, int Year, uint2 Hour, uint2 Minute, uint2 Second, uint2 Millisec = 0);
00268 void Set(uint2 Day, MONTH Month, int Year) { Set(Day, Month, Year, 0, 0, 0); }
00269 void SetYear(int Year) { year = Year; wday = INV_WEEKDAY; }
00270 void SetMonth(MONTH Month) { mon = Month; wday = INV_WEEKDAY; }
00271 void SetDay(uint2 Day) { mday = Day; wday = INV_WEEKDAY; }
00272 void SetHour(uint2 Hour) { hour = Hour; }
00273 void SetMinute(uint2 Minute) { min = Minute; }
00274 void SetSecond(uint2 Second) { sec = Second; }
00275 void SetMillisecond(uint2 Millisecond) { msec = Millisecond; }
00276
00278 void ResetTime() { hour = min = sec = msec = 0; }
00279
00281 void GetTm(struct tm *Out) const;
00282 };
00283
00284 inline TMSTRUCT operator + (const TMSTRUCT &tm, const DATESPAN &ds) { TMSTRUCT R = tm; R += ds; return R; }
00285 inline TMSTRUCT operator - (const TMSTRUCT &tm, const DATESPAN &ds) { TMSTRUCT R = tm; R -= ds; return R; }
00286
00288
00289 struct DATETIME
00290 {
00291 private:
00292 bool IsInStdRange() const;
00293
00294 public:
00295 int8 m_Time;
00296
00298 DATETIME() { }
00300 explicit DATETIME(time_t Time) { Set(Time); }
00301 explicit DATETIME(const struct tm &Time) { Set(Time); }
00302 explicit DATETIME(const TMSTRUCT &Time) { Set(Time); }
00303
00304 bool operator == (const DATETIME &t) const { return m_Time == t.m_Time; }
00305 bool operator != (const DATETIME &t) const { return m_Time != t.m_Time; }
00306 bool operator <= (const DATETIME &t) const { return m_Time <= t.m_Time; }
00307 bool operator >= (const DATETIME &t) const { return m_Time >= t.m_Time; }
00308 bool operator < (const DATETIME &t) const { return m_Time < t.m_Time; }
00309 bool operator > (const DATETIME &t) const { return m_Time > t.m_Time; }
00310
00311 DATETIME & operator += (const TIMESPAN &t) { Add(t); return *this; }
00312 DATETIME & operator -= (const TIMESPAN &t) { Subtract(t); return *this; }
00313
00316 void SetValue(int8 Value) { m_Time = Value; }
00317 void Set(time_t Time);
00318 void Set(const struct tm &Time);
00319 void Set(const TMSTRUCT &Time);
00320 void SetJDN(double JDN);
00322
00324 void SetMillisecond(uint Millisecond) { m_Time -= m_Time % 1000; m_Time += Millisecond; }
00326 void Add(int8 Milliseconds) { m_Time += Milliseconds; }
00328 void Add(const TIMESPAN &t) { m_Time += t.m_Diff; }
00330 void Subtract(int8 Milliseconds) { m_Time -= Milliseconds; }
00332 void Subtract(const TIMESPAN &t) { m_Time -= t.m_Diff; }
00334
00335 bool IsStrictlyBetween(const DATETIME &dt1, const DATETIME &dt2) const { return (m_Time > dt1.m_Time) && (m_Time < dt2.m_Time); }
00336 bool IsBetween(const DATETIME &dt1, const DATETIME &dt2) const { return (m_Time >= dt1.m_Time) && (m_Time <= dt2.m_Time); }
00337 bool IsEqualUpTo(const DATETIME &dt, const TIMESPAN &ts) const;
00338
00340
00341 time_t GetTicks() const;
00342 };
00343
00344 inline TIMESPAN operator - (const DATETIME &dt1, const DATETIME &dt2) { return TIMESPAN(dt1.m_Time - dt2.m_Time); }
00345 inline DATETIME operator + (const DATETIME &dt, const TIMESPAN &ts) { DATETIME R = dt; R += ts; return R; }
00346 inline DATETIME operator - (const DATETIME &dt, const TIMESPAN &ts) { DATETIME R = dt; R -= ts; return R; }
00347
00349 inline bool IsLeapYear(int Year) { return (Year % 4 == 0) && ((Year % 100 != 0) || (Year % 400 == 0)); }
00351 inline uint GetNumOfDaysInYear(int Year) { return IsLeapYear(Year) ? 366 : 365; }
00353 uint GetNumOfDaysInMonth(int Year, uint Month);
00355 inline int GetCentury(int Year) { return (Year > 0) ? (Year / 100) : (Year / 100 - 1); }
00356
00358 time_t GetTimeNow();
00360 void GetTmNow(struct tm *Out);
00362 DATETIME Now();
00364 DATETIME UNow();
00365
00367 const tchar * GetWeekDayName(WEEKDAY WeekDay, NAME_FORM Form);
00369 const tchar * GetMonthName(MONTH Month, NAME_FORM Form);
00371 void DateToStr(tstring *Out, const TMSTRUCT &tm, const tstring &Format);
00372
00374
00375 WEEKDAY StrToWeekDay(const tstring &s);
00377
00378 MONTH StrToMonth(const tstring &s);
00380
00382 bool StrToDate(TMSTRUCT *Out, const tstring &s, const tstring &Format);
00383
00385
00386
00387 }
00388
00391
00392 template <>
00393 struct SthToStr_obj<common::TMSTRUCT>
00394 {
00395 void operator () (tstring *Str, const common::TMSTRUCT &Sth)
00396 {
00397 common::DateToStr(Str, Sth, common::DEFAULT_FORMAT);
00398 }
00399 static inline bool IsSupported() { return true; }
00400 };
00401
00402 template <>
00403 struct StrToSth_obj<common::TMSTRUCT>
00404 {
00405 bool operator () (common::TMSTRUCT *Sth, const tstring &Str)
00406 {
00407 return common::StrToDate(Sth, Str, common::DEFAULT_FORMAT);
00408 }
00409 static inline bool IsSupported() { return true; }
00410 };
00411
00412 template <>
00413 struct SthToStr_obj<common::DATETIME>
00414 {
00415 void operator () (tstring *Str, const common::DATETIME &Sth)
00416 {
00417 SthToStr(Str, Sth.m_Time);
00418 }
00419 static inline bool IsSupported() { return true; }
00420 };
00421
00422 template <>
00423 struct SthToStr_obj<common::TIMESPAN>
00424 {
00425 void operator () (tstring *Str, const common::TIMESPAN &Sth)
00426 {
00427 SthToStr(Str, Sth.m_Diff);
00428 }
00429 static inline bool IsSupported() { return true; }
00430 };
00431
00433
00434
00435 #endif