1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef _FX_DATETIME_H_ 8 #define _FX_DATETIME_H_ 9 class CFX_Unitime; 10 class CFX_DateTime; 11 typedef int64_t FX_UNITIME; 12 enum FX_WEEKDAY { 13 FX_Sunday = 0, 14 FX_Monday, 15 FX_Tuesday, 16 FX_Wednesday, 17 FX_Thursday, 18 FX_Friday, 19 FX_Saturday, 20 }; 21 FX_BOOL FX_IsLeapYear(int32_t iYear); 22 int32_t FX_DaysInYear(int32_t iYear); 23 uint8_t FX_DaysInMonth(int32_t iYear, uint8_t iMonth); 24 class CFX_Unitime { 25 public: CFX_Unitime()26 CFX_Unitime() { m_iUnitime = 0; } CFX_Unitime(FX_UNITIME iUnitime)27 CFX_Unitime(FX_UNITIME iUnitime) { m_iUnitime = iUnitime; } CFX_Unitime(const CFX_Unitime & unitime)28 CFX_Unitime(const CFX_Unitime& unitime) { m_iUnitime = unitime.m_iUnitime; } 29 operator FX_UNITIME*() { return &m_iUnitime; } 30 operator FX_UNITIME const*() const { return &m_iUnitime; } 31 operator FX_UNITIME&() { return m_iUnitime; } 32 operator const FX_UNITIME&() const { return m_iUnitime; } 33 CFX_Unitime& operator=(const CFX_Unitime& t) { 34 m_iUnitime = t.m_iUnitime; 35 return *this; 36 } 37 CFX_Unitime& operator=(FX_UNITIME t) { 38 m_iUnitime = t; 39 return *this; 40 } 41 CFX_Unitime& operator+=(const CFX_Unitime& t) { 42 m_iUnitime += t.m_iUnitime; 43 return *this; 44 } 45 CFX_Unitime& operator+=(FX_UNITIME t) { 46 m_iUnitime += t; 47 return *this; 48 } 49 CFX_Unitime& operator-=(const CFX_Unitime& t) { 50 m_iUnitime -= t.m_iUnitime; 51 return *this; 52 } 53 CFX_Unitime& operator-=(FX_UNITIME t) { 54 m_iUnitime -= t; 55 return *this; 56 } 57 void Now(); 58 void SetGMTime(); 59 void Set(int32_t year, 60 uint8_t month, 61 uint8_t day, 62 uint8_t hour = 0, 63 uint8_t minute = 0, 64 uint8_t second = 0, 65 FX_WORD millisecond = 0); 66 void Set(FX_UNITIME t); 67 int32_t GetYear() const; 68 uint8_t GetMonth() const; 69 uint8_t GetDay() const; 70 FX_WEEKDAY GetDayOfWeek() const; 71 FX_WORD GetDayOfYear() const; 72 int64_t GetDayOfAD() const; 73 uint8_t GetHour() const; 74 uint8_t GetMinute() const; 75 uint8_t GetSecond() const; 76 FX_WORD GetMillisecond() const; 77 FX_BOOL AddYears(int32_t iYears); 78 FX_BOOL AddMonths(int32_t iMonths); 79 FX_BOOL AddDays(int32_t iDays); 80 FX_BOOL AddHours(int32_t iHours); 81 FX_BOOL AddMinutes(int32_t iMinutes); 82 FX_BOOL AddSeconds(int32_t iSeconds); 83 FX_BOOL AddMilliseconds(int32_t iMilliseconds); 84 friend CFX_Unitime operator+(const CFX_Unitime& t1, const CFX_Unitime& t2) { 85 return CFX_Unitime(t1.m_iUnitime + t2.m_iUnitime); 86 } 87 friend CFX_Unitime operator+(const CFX_Unitime& t1, FX_UNITIME t2) { 88 return CFX_Unitime(t1.m_iUnitime + t2); 89 } 90 friend CFX_Unitime operator+(FX_UNITIME t1, const CFX_Unitime& t2) { 91 return CFX_Unitime(t1 + t2.m_iUnitime); 92 } 93 friend CFX_Unitime operator-(const CFX_Unitime& t1, const CFX_Unitime& t2) { 94 return CFX_Unitime(t1.m_iUnitime + t2.m_iUnitime); 95 } 96 friend CFX_Unitime operator-(const CFX_Unitime& t1, FX_UNITIME t2) { 97 return CFX_Unitime(t1.m_iUnitime + t2); 98 } 99 friend CFX_Unitime operator-(FX_UNITIME t1, const CFX_Unitime& t2) { 100 return CFX_Unitime(t1 + t2.m_iUnitime); 101 } 102 friend FX_BOOL operator==(const CFX_Unitime& t1, const CFX_Unitime& t2) { 103 return t1.m_iUnitime == t2.m_iUnitime; 104 } 105 friend FX_BOOL operator==(const CFX_Unitime& t1, FX_UNITIME t2) { 106 return t1.m_iUnitime == t2; 107 } 108 friend FX_BOOL operator==(FX_UNITIME t1, const CFX_Unitime& t2) { 109 return t1 == t2.m_iUnitime; 110 } 111 friend FX_BOOL operator!=(const CFX_Unitime& t1, const CFX_Unitime& t2) { 112 return t1.m_iUnitime != t2.m_iUnitime; 113 } 114 friend FX_BOOL operator!=(const CFX_Unitime& t1, FX_UNITIME t2) { 115 return t1.m_iUnitime != t2; 116 } 117 friend FX_BOOL operator!=(FX_UNITIME t1, const CFX_Unitime& t2) { 118 return t1 != t2.m_iUnitime; 119 } 120 friend FX_BOOL operator>(const CFX_Unitime& t1, const CFX_Unitime& t2) { 121 return t1.m_iUnitime > t2.m_iUnitime; 122 } 123 friend FX_BOOL operator>(const CFX_Unitime& t1, FX_UNITIME t2) { 124 return t1.m_iUnitime > t2; 125 } 126 friend FX_BOOL operator>(FX_UNITIME t1, const CFX_Unitime& t2) { 127 return t1 > t2.m_iUnitime; 128 } 129 friend FX_BOOL operator>=(const CFX_Unitime& t1, const CFX_Unitime& t2) { 130 return t1.m_iUnitime >= t2.m_iUnitime; 131 } 132 friend FX_BOOL operator>=(const CFX_Unitime& t1, FX_UNITIME t2) { 133 return t1.m_iUnitime >= t2; 134 } 135 friend FX_BOOL operator>=(FX_UNITIME t1, const CFX_Unitime& t2) { 136 return t1 >= t2.m_iUnitime; 137 } 138 friend FX_BOOL operator<(const CFX_Unitime& t1, const CFX_Unitime& t2) { 139 return t1.m_iUnitime < t2.m_iUnitime; 140 } 141 friend FX_BOOL operator<(const CFX_Unitime& t1, FX_UNITIME t2) { 142 return t1.m_iUnitime < t2; 143 } 144 friend FX_BOOL operator<(FX_UNITIME t1, const CFX_Unitime& t2) { 145 return t1 < t2.m_iUnitime; 146 } 147 friend FX_BOOL operator<=(const CFX_Unitime& t1, const CFX_Unitime& t2) { 148 return t1.m_iUnitime <= t2.m_iUnitime; 149 } 150 friend FX_BOOL operator<=(const CFX_Unitime& t1, FX_UNITIME t2) { 151 return t1.m_iUnitime <= t2; 152 } 153 friend FX_BOOL operator<=(FX_UNITIME t1, const CFX_Unitime& t2) { 154 return t1 <= t2.m_iUnitime; 155 } 156 157 private: 158 FX_UNITIME m_iUnitime; 159 }; 160 #if _FX_OS_ != _FX_ANDROID_ 161 #pragma pack(push, 1) 162 #endif 163 typedef struct _FX_DATE { 164 int32_t year; 165 uint8_t month; 166 uint8_t day; 167 } FX_DATE, *FX_LPDATE; 168 typedef FX_DATE const* FX_LPCDATE; 169 typedef struct _FX_TIME { 170 uint8_t hour; 171 uint8_t minute; 172 uint8_t second; 173 FX_WORD millisecond; 174 } FX_TIME, *FX_LPTIME; 175 typedef FX_TIME const* FX_LPCTIME; 176 typedef struct _FX_TIMEZONE { 177 int8_t tzHour; 178 uint8_t tzMinute; 179 } FX_TIMEZONE, *FX_LPTIMEZONE; 180 typedef FX_TIMEZONE const* FX_LPCTIMEZONE; 181 typedef struct _FX_DATETIME { 182 union { 183 struct { 184 int32_t year; 185 uint8_t month; 186 uint8_t day; 187 } sDate; 188 FX_DATE aDate; 189 } Date; 190 union { 191 struct { 192 uint8_t hour; 193 uint8_t minute; 194 uint8_t second; 195 FX_WORD millisecond; 196 } sTime; 197 FX_TIME aTime; 198 } Time; 199 } FX_DATETIME, *FX_LPDATETIME; 200 typedef FX_DATETIME const* FX_LPCDATETIME; 201 typedef struct _FX_DATETIMEZONE { 202 union { 203 struct { 204 union { 205 struct { 206 int32_t year; 207 uint8_t month; 208 uint8_t day; 209 }; 210 FX_DATE date; 211 }; 212 union { 213 struct { 214 uint8_t hour; 215 uint8_t minute; 216 uint8_t second; 217 FX_WORD millisecond; 218 }; 219 FX_TIME time; 220 }; 221 }; 222 FX_DATETIME dt; 223 }; 224 union { 225 struct { 226 int8_t tzHour; 227 uint8_t tzMinute; 228 }; 229 FX_TIMEZONE tz; 230 }; 231 } FX_DATETIMEZONE, *FX_LPDATETIMEZONE; 232 typedef FX_DATETIMEZONE const* FX_LPCDATETIMEZONE; 233 #if _FX_OS_ != _FX_ANDROID_ 234 #pragma pack(pop) 235 #endif 236 class CFX_DateTime { 237 public: CFX_DateTime()238 CFX_DateTime() {} CFX_DateTime(const FX_DATETIME & dt)239 CFX_DateTime(const FX_DATETIME& dt) { m_DateTime = dt; } CFX_DateTime(const CFX_DateTime & dt)240 CFX_DateTime(const CFX_DateTime& dt) { m_DateTime = dt.m_DateTime; } ~CFX_DateTime()241 virtual ~CFX_DateTime() {} 242 operator FX_DATETIME*() { return &m_DateTime; } 243 operator FX_DATETIME const*() const { return &m_DateTime; } 244 operator FX_DATETIME&() { return m_DateTime; } 245 operator const FX_DATETIME&() const { return m_DateTime; } 246 CFX_DateTime& operator=(const CFX_DateTime& dt) { 247 m_DateTime = dt.m_DateTime; 248 return *this; 249 } 250 CFX_DateTime& operator=(const FX_DATETIME& dt) { 251 m_DateTime = dt; 252 return *this; 253 } 254 CFX_DateTime& operator+=(const CFX_DateTime& dt) { 255 FromUnitime(ToUnitime() + dt.ToUnitime()); 256 return *this; 257 } 258 CFX_DateTime& operator+=(const FX_DATETIME& dt) { 259 FromUnitime(ToUnitime() + ((const CFX_DateTime&)dt).ToUnitime()); 260 return *this; 261 } 262 CFX_DateTime& operator-=(const CFX_DateTime& dt) { 263 FromUnitime(ToUnitime() - dt.ToUnitime()); 264 return *this; 265 } 266 CFX_DateTime& operator-=(const FX_DATETIME& dt) { 267 FromUnitime(ToUnitime() - ((const CFX_DateTime&)dt).ToUnitime()); 268 return *this; 269 } 270 virtual FX_BOOL Set(int32_t year, 271 uint8_t month, 272 uint8_t day, 273 uint8_t hour = 0, 274 uint8_t minute = 0, 275 uint8_t second = 0, 276 FX_WORD millisecond = 0); 277 virtual FX_BOOL FromUnitime(FX_UNITIME t); 278 virtual FX_UNITIME ToUnitime() const; 279 virtual int32_t GetYear() const; 280 virtual uint8_t GetMonth() const; 281 virtual uint8_t GetDay() const; 282 virtual FX_WEEKDAY GetDayOfWeek() const; 283 virtual FX_WORD GetDayOfYear() const; 284 virtual int64_t GetDayOfAD() const; 285 virtual uint8_t GetHour() const; 286 virtual uint8_t GetMinute() const; 287 virtual uint8_t GetSecond() const; 288 virtual FX_WORD GetMillisecond() const; 289 virtual FX_BOOL AddYears(int32_t iYears); 290 virtual FX_BOOL AddMonths(int32_t iMonths); 291 virtual FX_BOOL AddDays(int32_t iDays); 292 virtual FX_BOOL AddHours(int32_t iHours); 293 virtual FX_BOOL AddMinutes(int32_t iMinutes); 294 virtual FX_BOOL AddSeconds(int32_t iSeconds); 295 virtual FX_BOOL AddMilliseconds(int32_t iMilliseconds); 296 friend CFX_DateTime operator+(const CFX_DateTime& dt1, 297 const CFX_DateTime& dt2) { 298 CFX_DateTime dt; 299 dt.FromUnitime(dt1.ToUnitime() + dt2.ToUnitime()); 300 return dt; 301 } 302 friend CFX_DateTime operator+(const CFX_DateTime& dt1, 303 const FX_DATETIME& dt2) { 304 CFX_DateTime dt; 305 dt.FromUnitime(dt1.ToUnitime() + ((const CFX_DateTime&)dt2).ToUnitime()); 306 return dt; 307 } 308 friend CFX_DateTime operator+(const FX_DATETIME& dt1, 309 const CFX_DateTime& dt2) { 310 CFX_DateTime dt; 311 dt.FromUnitime(((const CFX_DateTime&)dt1).ToUnitime() + dt2.ToUnitime()); 312 return dt; 313 } 314 friend CFX_DateTime operator-(const CFX_DateTime& dt1, 315 const CFX_DateTime& dt2) { 316 CFX_DateTime dt; 317 dt.FromUnitime(dt1.ToUnitime() - dt2.ToUnitime()); 318 return dt; 319 } 320 friend CFX_DateTime operator-(const CFX_DateTime& dt1, 321 const FX_DATETIME& dt2) { 322 CFX_DateTime dt; 323 dt.FromUnitime(dt1.ToUnitime() - ((const CFX_DateTime&)dt2).ToUnitime()); 324 return dt; 325 } 326 friend CFX_DateTime operator-(const FX_DATETIME& dt1, 327 const CFX_DateTime& dt2) { 328 CFX_DateTime dt; 329 dt.FromUnitime(((const CFX_DateTime&)dt1).ToUnitime() - dt2.ToUnitime()); 330 return dt; 331 } 332 friend FX_BOOL operator==(const CFX_DateTime& dt1, const CFX_DateTime& dt2) { 333 return FXSYS_memcmp((FX_LPCDATETIME)dt1, (FX_LPCDATETIME)dt2, 334 sizeof(FX_DATETIME)) == 0; 335 } 336 friend FX_BOOL operator==(const CFX_DateTime& dt1, const FX_DATETIME& dt2) { 337 return FXSYS_memcmp((FX_LPCDATETIME)dt1, &dt2, sizeof(FX_DATETIME)) == 0; 338 } 339 friend FX_BOOL operator==(const FX_DATETIME& dt1, const CFX_DateTime& dt2) { 340 return FXSYS_memcmp(&dt1, (FX_LPCDATETIME)dt2, sizeof(FX_DATETIME)) == 0; 341 } 342 friend FX_BOOL operator!=(const CFX_DateTime& dt1, const CFX_DateTime& dt2) { 343 return FXSYS_memcmp((FX_LPCDATETIME)dt1, (FX_LPCDATETIME)dt2, 344 sizeof(FX_DATETIME)) != 0; 345 } 346 friend FX_BOOL operator!=(const CFX_DateTime& dt1, const FX_DATETIME& dt2) { 347 return FXSYS_memcmp((FX_LPCDATETIME)dt1, &dt2, sizeof(FX_DATETIME)) != 0; 348 } 349 friend FX_BOOL operator!=(const FX_DATETIME& dt1, const CFX_DateTime& dt2) { 350 return FXSYS_memcmp(&dt1, (FX_LPCDATETIME)dt2, sizeof(FX_DATETIME)) != 0; 351 } 352 friend FX_BOOL operator>(const CFX_DateTime& dt1, const CFX_DateTime& dt2) { 353 return dt1.ToUnitime() > dt2.ToUnitime(); 354 } 355 friend FX_BOOL operator>(const CFX_DateTime& dt1, const FX_DATETIME& dt2) { 356 return dt1.ToUnitime() > ((const CFX_DateTime&)dt2).ToUnitime(); 357 } 358 friend FX_BOOL operator>(const FX_DATETIME& dt1, const CFX_DateTime& dt2) { 359 return ((const CFX_DateTime&)dt1).ToUnitime() > dt2.ToUnitime(); 360 } 361 friend FX_BOOL operator>=(const CFX_DateTime& dt1, const CFX_DateTime& dt2) { 362 return dt1.ToUnitime() >= dt2.ToUnitime(); 363 } 364 friend FX_BOOL operator>=(const CFX_DateTime& dt1, const FX_DATETIME& dt2) { 365 return dt1.ToUnitime() >= ((const CFX_DateTime&)dt2).ToUnitime(); 366 } 367 friend FX_BOOL operator>=(const FX_DATETIME& dt1, const CFX_DateTime& dt2) { 368 return ((const CFX_DateTime&)dt1).ToUnitime() >= dt2.ToUnitime(); 369 } 370 friend FX_BOOL operator<(const CFX_DateTime& dt1, const CFX_DateTime& dt2) { 371 return dt1.ToUnitime() < dt2.ToUnitime(); 372 } 373 friend FX_BOOL operator<(const CFX_DateTime& dt1, const FX_DATETIME& dt2) { 374 return dt1.ToUnitime() < ((const CFX_DateTime&)dt2).ToUnitime(); 375 } 376 friend FX_BOOL operator<(const FX_DATETIME& dt1, const CFX_DateTime& dt2) { 377 return ((const CFX_DateTime&)dt1).ToUnitime() < dt2.ToUnitime(); 378 } 379 friend FX_BOOL operator<=(const CFX_DateTime& dt1, const CFX_DateTime& dt2) { 380 return dt1.ToUnitime() <= dt2.ToUnitime(); 381 } 382 friend FX_BOOL operator<=(const CFX_DateTime& dt1, const FX_DATETIME& dt2) { 383 return dt1.ToUnitime() <= ((const CFX_DateTime&)dt2).ToUnitime(); 384 } 385 friend FX_BOOL operator<=(const FX_DATETIME& dt1, const CFX_DateTime& dt2) { 386 return ((const CFX_DateTime&)dt1).ToUnitime() <= dt2.ToUnitime(); 387 } 388 389 private: 390 FX_DATETIME m_DateTime; 391 }; 392 #endif 393