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