1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/fxcrt/fx_system.h"
#include "xfa/fgas/localization/fgas_datetime.h"
#if _FX_OS_ == _FX_LINUX_DESKTOP_ || _FX_OS_ == _FX_ANDROID_ || \
_FX_OS_ == _FX_MACOSX_ || _FX_OS_ == _FX_IOS_
#include <sys/time.h>
#include <time.h>
#endif
namespace {
const uint8_t g_FXDaysPerMonth[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
const uint8_t g_FXDaysPerLeapMonth[12] = {31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
const int32_t g_FXDaysBeforeMonth[12] = {0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334};
const int32_t g_FXDaysBeforeLeapMonth[12] = {0, 31, 60, 91, 121, 152,
182, 213, 244, 274, 305, 335};
const int32_t g_FXDaysPerYear = 365;
const int32_t g_FXDaysPerLeapYear = 366;
const int32_t g_FXDaysPer4Years = 1461;
const int32_t g_FXDaysPer100Years = 36524;
const int32_t g_FXDaysPer400Years = 146097;
const int64_t g_FXMillisecondsPerSecond = 1000;
const int64_t g_FXMillisecondsPerMinute = 60000;
const int64_t g_FXMillisecondsPerHour = 3600000;
const int64_t g_FXMillisecondsPerDay = 86400000;
int64_t GetDayOfAD(int64_t unitime) {
bool bBC = unitime < 0;
int64_t iDays = unitime / g_FXMillisecondsPerDay;
iDays += bBC ? -1 : 0;
if (bBC && (unitime % g_FXMillisecondsPerDay) == 0)
iDays++;
return iDays;
}
int32_t DaysBeforeMonthInYear(int32_t iYear, uint8_t iMonth) {
ASSERT(iYear != 0);
ASSERT(iMonth >= 1 && iMonth <= 12);
const int32_t* p =
FX_IsLeapYear(iYear) ? g_FXDaysBeforeLeapMonth : g_FXDaysBeforeMonth;
return p[iMonth - 1];
}
int32_t DaysInYear(int32_t iYear) {
ASSERT(iYear != 0);
return FX_IsLeapYear(iYear) ? g_FXDaysPerLeapYear : g_FXDaysPerYear;
}
int64_t DateToDays(int32_t iYear,
uint8_t iMonth,
uint8_t iDay,
bool bIncludeThisDay) {
ASSERT(iYear != 0);
ASSERT(iMonth >= 1 && iMonth <= 12);
ASSERT(iDay >= 1 && iDay <= FX_DaysInMonth(iYear, iMonth));
int64_t iDays = DaysBeforeMonthInYear(iYear, iMonth);
iDays += iDay;
if (!bIncludeThisDay)
iDays--;
if (iYear > 0) {
iYear--;
} else {
iDays -= DaysInYear(iYear);
iYear++;
}
return iDays + static_cast<int64_t>(iYear) * 365 + iYear / 4 - iYear / 100 +
iYear / 400;
}
void DaysToDate(int64_t iDays,
int32_t* retYear,
uint8_t* retMonth,
uint8_t* retDay) {
bool bBC = iDays < 0;
if (bBC)
iDays = -iDays;
int32_t iYear = 1;
uint8_t iMonth = 1;
uint8_t iDay = 1;
if (iDays >= g_FXDaysPer400Years) {
iYear += static_cast<int32_t>(iDays / g_FXDaysPer400Years * 400);
iDays %= g_FXDaysPer400Years;
}
if (iDays >= g_FXDaysPer100Years) {
if (iDays == g_FXDaysPer100Years * 4) {
iYear += 300;
iDays -= g_FXDaysPer100Years * 3;
} else {
iYear += static_cast<int32_t>(iDays / g_FXDaysPer100Years * 100);
iDays %= g_FXDaysPer100Years;
}
}
if (iDays >= g_FXDaysPer4Years) {
iYear += static_cast<int32_t>(iDays / g_FXDaysPer4Years * 4);
iDays %= g_FXDaysPer4Years;
}
while (true) {
int32_t iYearDays = DaysInYear(iYear);
if (iDays < iYearDays) {
if (bBC) {
iYear = -iYear;
iDays = iYearDays - iDays;
}
break;
}
iYear++;
iDays -= iYearDays;
}
while (true) {
int32_t iMonthDays = FX_DaysInMonth(iYear, iMonth);
if (iDays < iMonthDays)
break;
iMonth++;
iDays -= iMonthDays;
}
iDay += static_cast<uint8_t>(iDays);
*retYear = iYear;
*retMonth = iMonth;
*retDay = iDay;
}
struct FXUT_SYSTEMTIME {
uint16_t wYear;
uint16_t wMonth;
uint16_t wDayOfWeek;
uint16_t wDay;
uint16_t wHour;
uint16_t wMinute;
uint16_t wSecond;
uint16_t wMilliseconds;
};
} // namespace
uint8_t FX_DaysInMonth(int32_t iYear, uint8_t iMonth) {
ASSERT(iYear != 0);
ASSERT(iMonth >= 1 && iMonth <= 12);
const uint8_t* p =
FX_IsLeapYear(iYear) ? g_FXDaysPerLeapMonth : g_FXDaysPerMonth;
return p[iMonth - 1];
}
bool FX_IsLeapYear(int32_t iYear) {
ASSERT(iYear != 0);
return ((iYear % 4) == 0 && (iYear % 100) != 0) || (iYear % 400) == 0;
}
void CFX_Unitime::Now() {
FXUT_SYSTEMTIME utLocal;
#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \
_FX_OS_ == _FX_WIN64_
::GetLocalTime((LPSYSTEMTIME)&utLocal);
#elif _FX_OS_ != _FX_EMBEDDED_
timeval curTime;
gettimeofday(&curTime, nullptr);
struct tm st;
localtime_r(&curTime.tv_sec, &st);
utLocal.wYear = st.tm_year + 1900;
utLocal.wMonth = st.tm_mon + 1;
utLocal.wDayOfWeek = st.tm_wday;
utLocal.wDay = st.tm_mday;
utLocal.wHour = st.tm_hour;
utLocal.wMinute = st.tm_min;
utLocal.wSecond = st.tm_sec;
utLocal.wMilliseconds = curTime.tv_usec / 1000;
#endif // _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \
// _FX_OS_ == _FX_WIN64_
Set(utLocal.wYear, static_cast<uint8_t>(utLocal.wMonth),
static_cast<uint8_t>(utLocal.wDay), static_cast<uint8_t>(utLocal.wHour),
static_cast<uint8_t>(utLocal.wMinute),
static_cast<uint8_t>(utLocal.wSecond),
static_cast<uint16_t>(utLocal.wMilliseconds));
}
void CFX_Unitime::Set(int32_t year,
uint8_t month,
uint8_t day,
uint8_t hour,
uint8_t minute,
uint8_t second,
uint16_t millisecond) {
ASSERT(hour <= 23);
ASSERT(minute <= 59);
ASSERT(second <= 59);
ASSERT(millisecond <= 999);
m_iUnitime = static_cast<int64_t>(hour) * g_FXMillisecondsPerHour +
static_cast<int64_t>(minute) * g_FXMillisecondsPerMinute +
static_cast<int64_t>(second) * g_FXMillisecondsPerSecond +
millisecond;
if (year <= 0)
return;
m_iUnitime += DateToDays(year, month, day, false) * g_FXMillisecondsPerDay;
}
int32_t CFX_Unitime::GetYear() const {
int32_t iYear;
uint8_t iMonth, iDay;
DaysToDate(GetDayOfAD(m_iUnitime), &iYear, &iMonth, &iDay);
return iYear;
}
uint8_t CFX_Unitime::GetMonth() const {
int32_t iYear;
uint8_t iMonth, iDay;
DaysToDate(GetDayOfAD(m_iUnitime), &iYear, &iMonth, &iDay);
return iMonth;
}
uint8_t CFX_Unitime::GetDay() const {
int32_t iYear;
uint8_t iMonth, iDay;
DaysToDate(GetDayOfAD(m_iUnitime), &iYear, &iMonth, &iDay);
return iDay;
}
uint8_t CFX_Unitime::GetHour() const {
int32_t v = static_cast<int32_t>(m_iUnitime % g_FXMillisecondsPerDay);
if (v < 0)
v += g_FXMillisecondsPerDay;
return static_cast<uint8_t>(v / g_FXMillisecondsPerHour);
}
uint8_t CFX_Unitime::GetMinute() const {
int32_t v = static_cast<int32_t>(m_iUnitime % g_FXMillisecondsPerHour);
if (v < 0)
v += g_FXMillisecondsPerHour;
return static_cast<uint8_t>(v / g_FXMillisecondsPerMinute);
}
uint8_t CFX_Unitime::GetSecond() const {
int32_t v = static_cast<int32_t>(m_iUnitime % g_FXMillisecondsPerMinute);
if (v < 0)
v += g_FXMillisecondsPerMinute;
return static_cast<uint8_t>(v / g_FXMillisecondsPerSecond);
}
uint16_t CFX_Unitime::GetMillisecond() const {
int32_t v = static_cast<int32_t>(m_iUnitime % g_FXMillisecondsPerSecond);
if (v < 0)
v += g_FXMillisecondsPerSecond;
return static_cast<uint16_t>(v);
}
bool CFX_DateTime::Set(int32_t year, uint8_t month, uint8_t day) {
ASSERT(year != 0);
ASSERT(month >= 1 && month <= 12);
ASSERT(day >= 1 && day <= FX_DaysInMonth(year, month));
m_DateTime.Date.sDate.year = year;
m_DateTime.Date.sDate.month = month;
m_DateTime.Date.sDate.day = day;
m_DateTime.Time.sTime.hour = 0;
m_DateTime.Time.sTime.minute = 0;
m_DateTime.Time.sTime.second = 0;
m_DateTime.Time.sTime.millisecond = 0;
return true;
}
int32_t CFX_DateTime::GetDayOfWeek() const {
int32_t v = static_cast<int32_t>(DateToDays(m_DateTime.Date.sDate.year,
m_DateTime.Date.sDate.month,
m_DateTime.Date.sDate.day, true) %
7);
if (v < 0)
v += 7;
return v;
}
|