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 #include "core/fxcrt/cfx_datetime.h"
8
9 #include "build/build_config.h"
10 #include "core/fxcrt/fx_system.h"
11
12 #if defined(OS_ANDROID) || defined(OS_LINUX) || defined(OS_MACOSX) || \
13 defined(OS_ASMJS) || defined(__wasm__)
14 #include <sys/time.h>
15 #include <time.h>
16 #endif
17
18 namespace {
19
20 const uint8_t g_FXDaysPerMonth[12] = {31, 28, 31, 30, 31, 30,
21 31, 31, 30, 31, 30, 31};
22 const uint8_t g_FXDaysPerLeapMonth[12] = {31, 29, 31, 30, 31, 30,
23 31, 31, 30, 31, 30, 31};
24 const int32_t g_FXDaysBeforeMonth[12] = {0, 31, 59, 90, 120, 151,
25 181, 212, 243, 273, 304, 334};
26 const int32_t g_FXDaysBeforeLeapMonth[12] = {0, 31, 60, 91, 121, 152,
27 182, 213, 244, 274, 305, 335};
28 const int32_t g_FXDaysPerYear = 365;
29 const int32_t g_FXDaysPerLeapYear = 366;
30
DaysBeforeMonthInYear(int32_t iYear,uint8_t iMonth)31 int32_t DaysBeforeMonthInYear(int32_t iYear, uint8_t iMonth) {
32 ASSERT(iYear != 0);
33 ASSERT(iMonth >= 1);
34 ASSERT(iMonth <= 12);
35
36 const int32_t* p =
37 FX_IsLeapYear(iYear) ? g_FXDaysBeforeLeapMonth : g_FXDaysBeforeMonth;
38 return p[iMonth - 1];
39 }
40
DaysInYear(int32_t iYear)41 int32_t DaysInYear(int32_t iYear) {
42 ASSERT(iYear != 0);
43 return FX_IsLeapYear(iYear) ? g_FXDaysPerLeapYear : g_FXDaysPerYear;
44 }
45
DateToDays(int32_t iYear,uint8_t iMonth,uint8_t iDay,bool bIncludeThisDay)46 int64_t DateToDays(int32_t iYear,
47 uint8_t iMonth,
48 uint8_t iDay,
49 bool bIncludeThisDay) {
50 ASSERT(iYear != 0);
51 ASSERT(iMonth >= 1);
52 ASSERT(iMonth <= 12);
53 ASSERT(iDay >= 1);
54 ASSERT(iDay <= FX_DaysInMonth(iYear, iMonth));
55
56 int64_t iDays = DaysBeforeMonthInYear(iYear, iMonth);
57 iDays += iDay;
58 if (!bIncludeThisDay)
59 iDays--;
60
61 if (iYear > 0) {
62 iYear--;
63 } else {
64 iDays -= DaysInYear(iYear);
65 iYear++;
66 }
67 return iDays + static_cast<int64_t>(iYear) * 365 + iYear / 4 - iYear / 100 +
68 iYear / 400;
69 }
70
71 struct FXUT_SYSTEMTIME {
72 uint16_t wYear;
73 uint16_t wMonth;
74 uint16_t wDayOfWeek;
75 uint16_t wDay;
76 uint16_t wHour;
77 uint16_t wMinute;
78 uint16_t wSecond;
79 uint16_t wMillisecond;
80 };
81
82 } // namespace
83
FX_DaysInMonth(int32_t iYear,uint8_t iMonth)84 uint8_t FX_DaysInMonth(int32_t iYear, uint8_t iMonth) {
85 ASSERT(iYear != 0);
86 ASSERT(iMonth >= 1);
87 ASSERT(iMonth <= 12);
88
89 const uint8_t* p =
90 FX_IsLeapYear(iYear) ? g_FXDaysPerLeapMonth : g_FXDaysPerMonth;
91 return p[iMonth - 1];
92 }
93
FX_IsLeapYear(int32_t iYear)94 bool FX_IsLeapYear(int32_t iYear) {
95 ASSERT(iYear != 0);
96 return ((iYear % 4) == 0 && (iYear % 100) != 0) || (iYear % 400) == 0;
97 }
98
99 // static
Now()100 CFX_DateTime CFX_DateTime::Now() {
101 FXUT_SYSTEMTIME utLocal;
102 #if defined(OS_WIN)
103 ::GetLocalTime((LPSYSTEMTIME)&utLocal);
104 #else
105 timeval curTime;
106 gettimeofday(&curTime, nullptr);
107
108 struct tm st;
109 localtime_r(&curTime.tv_sec, &st);
110 utLocal.wYear = st.tm_year + 1900;
111 utLocal.wMonth = st.tm_mon + 1;
112 utLocal.wDayOfWeek = st.tm_wday;
113 utLocal.wDay = st.tm_mday;
114 utLocal.wHour = st.tm_hour;
115 utLocal.wMinute = st.tm_min;
116 utLocal.wSecond = st.tm_sec;
117 utLocal.wMillisecond = curTime.tv_usec / 1000;
118 #endif // defined(OS_WIN)
119
120 return CFX_DateTime(utLocal.wYear, static_cast<uint8_t>(utLocal.wMonth),
121 static_cast<uint8_t>(utLocal.wDay),
122 static_cast<uint8_t>(utLocal.wHour),
123 static_cast<uint8_t>(utLocal.wMinute),
124 static_cast<uint8_t>(utLocal.wSecond),
125 static_cast<uint16_t>(utLocal.wMillisecond));
126 }
127
GetDayOfWeek() const128 int32_t CFX_DateTime::GetDayOfWeek() const {
129 int32_t v = static_cast<int32_t>(DateToDays(year_, month_, day_, true) % 7);
130 if (v < 0)
131 v += 7;
132 return v;
133 }
134
operator ==(const CFX_DateTime & other) const135 bool CFX_DateTime::operator==(const CFX_DateTime& other) const {
136 return year_ == other.year_ && month_ == other.month_ && day_ == other.day_ &&
137 hour_ == other.hour_ && minute_ == other.minute_ &&
138 second_ == other.second_ && millisecond_ == other.millisecond_;
139 }
140