OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/include/fx_ext.h" | |
dsinclair
2016/08/11 14:01:04
cpdfsdk_datetime.h should come first with a blank
jaepark
2016/08/11 18:09:50
Done.
| |
8 #include "fpdfsdk/include/cpdfsdk_datetime.h" | |
9 | |
10 namespace { | |
11 | |
12 int gAfxGetTimeZoneInSeconds(int8_t tzhour, uint8_t tzminute) { | |
13 return (int)tzhour * 3600 + (int)tzminute * (tzhour >= 0 ? 60 : -60); | |
14 } | |
15 | |
16 bool gAfxIsLeapYear(int16_t year) { | |
17 return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))); | |
18 } | |
19 | |
20 uint16_t gAfxGetYearDays(int16_t year) { | |
21 return (gAfxIsLeapYear(year) ? 366 : 365); | |
22 } | |
23 | |
24 uint8_t gAfxGetMonthDays(int16_t year, uint8_t month) { | |
dsinclair
2016/08/11 14:01:04
Let's drop the gAfx prefix from these names, they
jaepark
2016/08/11 18:09:50
Done.
| |
25 uint8_t mDays; | |
26 switch (month) { | |
27 case 1: | |
28 case 3: | |
29 case 5: | |
30 case 7: | |
31 case 8: | |
32 case 10: | |
33 case 12: | |
34 mDays = 31; | |
35 break; | |
36 | |
37 case 4: | |
38 case 6: | |
39 case 9: | |
40 case 11: | |
41 mDays = 30; | |
42 break; | |
43 | |
44 case 2: | |
45 if (gAfxIsLeapYear(year)) | |
46 mDays = 29; | |
47 else | |
48 mDays = 28; | |
49 break; | |
50 | |
51 default: | |
52 mDays = 0; | |
53 break; | |
54 } | |
55 | |
56 return mDays; | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 CPDFSDK_DateTime::CPDFSDK_DateTime() { | |
62 ResetDateTime(); | |
63 } | |
64 | |
65 CPDFSDK_DateTime::CPDFSDK_DateTime(const CFX_ByteString& dtStr) { | |
66 ResetDateTime(); | |
67 | |
68 FromPDFDateTimeString(dtStr); | |
69 } | |
70 | |
71 CPDFSDK_DateTime::CPDFSDK_DateTime(const CPDFSDK_DateTime& datetime) { | |
72 operator=(datetime); | |
73 } | |
74 | |
75 CPDFSDK_DateTime::CPDFSDK_DateTime(const FX_SYSTEMTIME& st) { | |
76 operator=(st); | |
77 } | |
78 | |
79 void CPDFSDK_DateTime::ResetDateTime() { | |
80 tzset(); | |
81 | |
82 time_t curTime; | |
83 time(&curTime); | |
84 struct tm* newtime = localtime(&curTime); | |
85 | |
86 dt.year = newtime->tm_year + 1900; | |
87 dt.month = newtime->tm_mon + 1; | |
88 dt.day = newtime->tm_mday; | |
89 dt.hour = newtime->tm_hour; | |
90 dt.minute = newtime->tm_min; | |
91 dt.second = newtime->tm_sec; | |
92 } | |
93 | |
94 CPDFSDK_DateTime& CPDFSDK_DateTime::operator=( | |
95 const CPDFSDK_DateTime& datetime) { | |
96 FXSYS_memcpy(&dt, &datetime.dt, sizeof(FX_DATETIME)); | |
dsinclair
2016/08/11 14:01:04
If this isn't used outside the file lets move it i
jaepark
2016/08/11 18:09:50
Done.
| |
97 return *this; | |
98 } | |
99 | |
100 CPDFSDK_DateTime& CPDFSDK_DateTime::operator=(const FX_SYSTEMTIME& st) { | |
dsinclair
2016/08/11 14:01:04
Is this used outside of this file? If not, lets re
jaepark
2016/08/11 18:09:50
Done.
| |
101 tzset(); | |
102 | |
103 dt.year = static_cast<int16_t>(st.wYear); | |
104 dt.month = static_cast<uint8_t>(st.wMonth); | |
105 dt.day = static_cast<uint8_t>(st.wDay); | |
106 dt.hour = static_cast<uint8_t>(st.wHour); | |
107 dt.minute = static_cast<uint8_t>(st.wMinute); | |
108 dt.second = static_cast<uint8_t>(st.wSecond); | |
109 return *this; | |
110 } | |
111 | |
112 bool CPDFSDK_DateTime::operator==(const CPDFSDK_DateTime& datetime) const { | |
113 return (FXSYS_memcmp(&dt, &datetime.dt, sizeof(FX_DATETIME)) == 0); | |
114 } | |
115 | |
116 bool CPDFSDK_DateTime::operator!=(const CPDFSDK_DateTime& datetime) const { | |
117 return !(*this == datetime); | |
118 } | |
119 | |
120 time_t CPDFSDK_DateTime::ToTime_t() const { | |
121 struct tm newtime; | |
122 | |
123 newtime.tm_year = dt.year - 1900; | |
124 newtime.tm_mon = dt.month - 1; | |
125 newtime.tm_mday = dt.day; | |
126 newtime.tm_hour = dt.hour; | |
127 newtime.tm_min = dt.minute; | |
128 newtime.tm_sec = dt.second; | |
129 | |
130 return mktime(&newtime); | |
131 } | |
132 | |
133 CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString( | |
134 const CFX_ByteString& dtStr) { | |
135 int strLength = dtStr.GetLength(); | |
136 if (strLength <= 0) | |
137 return *this; | |
138 | |
139 int i = 0; | |
140 while (i < strLength && !std::isdigit(dtStr[i])) | |
141 ++i; | |
142 | |
143 if (i >= strLength) | |
144 return *this; | |
145 | |
146 int j = 0; | |
147 int k = 0; | |
148 FX_CHAR ch; | |
149 while (i < strLength && j < 4) { | |
150 ch = dtStr[i]; | |
151 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
152 j++; | |
153 if (!std::isdigit(ch)) | |
154 break; | |
155 i++; | |
156 } | |
157 dt.year = static_cast<int16_t>(k); | |
158 if (i >= strLength || j < 4) | |
159 return *this; | |
160 | |
161 j = 0; | |
162 k = 0; | |
163 while (i < strLength && j < 2) { | |
164 ch = dtStr[i]; | |
165 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
166 j++; | |
167 if (!std::isdigit(ch)) | |
168 break; | |
169 i++; | |
170 } | |
171 dt.month = static_cast<uint8_t>(k); | |
172 if (i >= strLength || j < 2) | |
173 return *this; | |
174 | |
175 j = 0; | |
176 k = 0; | |
177 while (i < strLength && j < 2) { | |
178 ch = dtStr[i]; | |
179 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
180 j++; | |
181 if (!std::isdigit(ch)) | |
182 break; | |
183 i++; | |
184 } | |
185 dt.day = static_cast<uint8_t>(k); | |
186 if (i >= strLength || j < 2) | |
187 return *this; | |
188 | |
189 j = 0; | |
190 k = 0; | |
191 while (i < strLength && j < 2) { | |
192 ch = dtStr[i]; | |
193 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
194 j++; | |
195 if (!std::isdigit(ch)) | |
196 break; | |
197 i++; | |
198 } | |
199 dt.hour = static_cast<uint8_t>(k); | |
200 if (i >= strLength || j < 2) | |
201 return *this; | |
202 | |
203 j = 0; | |
204 k = 0; | |
205 while (i < strLength && j < 2) { | |
206 ch = dtStr[i]; | |
207 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
208 j++; | |
209 if (!std::isdigit(ch)) | |
210 break; | |
211 i++; | |
212 } | |
213 dt.minute = static_cast<uint8_t>(k); | |
214 if (i >= strLength || j < 2) | |
215 return *this; | |
216 | |
217 j = 0; | |
218 k = 0; | |
219 while (i < strLength && j < 2) { | |
220 ch = dtStr[i]; | |
221 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
222 j++; | |
223 if (!std::isdigit(ch)) | |
224 break; | |
225 i++; | |
226 } | |
227 dt.second = static_cast<uint8_t>(k); | |
228 if (i >= strLength || j < 2) | |
229 return *this; | |
230 | |
231 ch = dtStr[i++]; | |
232 if (ch != '-' && ch != '+') | |
233 return *this; | |
234 if (ch == '-') | |
235 dt.tzHour = -1; | |
236 else | |
237 dt.tzHour = 1; | |
238 j = 0; | |
239 k = 0; | |
240 while (i < strLength && j < 2) { | |
241 ch = dtStr[i]; | |
242 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
243 j++; | |
244 if (!std::isdigit(ch)) | |
245 break; | |
246 i++; | |
247 } | |
248 dt.tzHour *= static_cast<int8_t>(k); | |
249 if (i >= strLength || j < 2) | |
250 return *this; | |
251 | |
252 if (dtStr[i++] != '\'') | |
253 return *this; | |
254 j = 0; | |
255 k = 0; | |
256 while (i < strLength && j < 2) { | |
257 ch = dtStr[i]; | |
258 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
259 j++; | |
260 if (!std::isdigit(ch)) | |
261 break; | |
262 i++; | |
263 } | |
264 dt.tzMinute = static_cast<uint8_t>(k); | |
265 return *this; | |
266 } | |
267 | |
268 CFX_ByteString CPDFSDK_DateTime::ToCommonDateTimeString() { | |
269 CFX_ByteString str1; | |
270 str1.Format("%04d-%02u-%02u %02u:%02u:%02u ", dt.year, dt.month, dt.day, | |
271 dt.hour, dt.minute, dt.second); | |
272 if (dt.tzHour < 0) | |
273 str1 += "-"; | |
274 else | |
275 str1 += "+"; | |
276 CFX_ByteString str2; | |
277 str2.Format("%02d:%02u", std::abs(static_cast<int>(dt.tzHour)), dt.tzMinute); | |
278 return str1 + str2; | |
279 } | |
280 | |
281 CFX_ByteString CPDFSDK_DateTime::ToPDFDateTimeString() { | |
282 CFX_ByteString dtStr; | |
283 char tempStr[32]; | |
284 memset(tempStr, 0, sizeof(tempStr)); | |
285 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "D:%04d%02u%02u%02u%02u%02u", | |
286 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second); | |
287 dtStr = CFX_ByteString(tempStr); | |
288 if (dt.tzHour < 0) | |
289 dtStr += CFX_ByteString("-"); | |
290 else | |
291 dtStr += CFX_ByteString("+"); | |
292 memset(tempStr, 0, sizeof(tempStr)); | |
293 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "%02d'%02u'", | |
294 std::abs(static_cast<int>(dt.tzHour)), dt.tzMinute); | |
295 dtStr += CFX_ByteString(tempStr); | |
296 return dtStr; | |
297 } | |
298 | |
299 void CPDFSDK_DateTime::ToSystemTime(FX_SYSTEMTIME& st) { | |
300 time_t t = this->ToTime_t(); | |
301 struct tm* pTime = localtime(&t); | |
302 if (pTime) { | |
dsinclair
2016/08/11 14:01:04
if (!pTime)
return;
jaepark
2016/08/11 18:09:50
Done.
| |
303 st.wYear = static_cast<uint16_t>(pTime->tm_year) + 1900; | |
304 st.wMonth = static_cast<uint16_t>(pTime->tm_mon) + 1; | |
305 st.wDay = static_cast<uint16_t>(pTime->tm_mday); | |
306 st.wDayOfWeek = static_cast<uint16_t>(pTime->tm_wday); | |
307 st.wHour = static_cast<uint16_t>(pTime->tm_hour); | |
308 st.wMinute = static_cast<uint16_t>(pTime->tm_min); | |
309 st.wSecond = static_cast<uint16_t>(pTime->tm_sec); | |
310 st.wMilliseconds = 0; | |
311 } | |
312 } | |
313 | |
314 CPDFSDK_DateTime CPDFSDK_DateTime::ToGMT() const { | |
315 CPDFSDK_DateTime new_dt = *this; | |
316 new_dt.AddSeconds( | |
317 -gAfxGetTimeZoneInSeconds(new_dt.dt.tzHour, new_dt.dt.tzMinute)); | |
318 new_dt.dt.tzHour = 0; | |
319 new_dt.dt.tzMinute = 0; | |
320 return new_dt; | |
321 } | |
322 | |
323 CPDFSDK_DateTime& CPDFSDK_DateTime::AddDays(short days) { | |
324 if (days == 0) | |
325 return *this; | |
326 | |
327 int16_t y = dt.year; | |
328 uint8_t m = dt.month; | |
329 uint8_t d = dt.day; | |
330 | |
331 int ldays = days; | |
332 if (ldays > 0) { | |
333 int16_t yy = y; | |
334 if ((static_cast<uint16_t>(m) * 100 + d) > 300) | |
335 yy++; | |
336 int ydays = gAfxGetYearDays(yy); | |
337 int mdays; | |
338 while (ldays >= ydays) { | |
339 y++; | |
340 ldays -= ydays; | |
341 yy++; | |
342 mdays = gAfxGetMonthDays(y, m); | |
343 if (d > mdays) { | |
344 m++; | |
345 d -= mdays; | |
346 } | |
347 ydays = gAfxGetYearDays(yy); | |
348 } | |
349 mdays = gAfxGetMonthDays(y, m) - d + 1; | |
350 while (ldays >= mdays) { | |
351 ldays -= mdays; | |
352 m++; | |
353 d = 1; | |
354 mdays = gAfxGetMonthDays(y, m); | |
355 } | |
356 d += ldays; | |
357 } else { | |
358 ldays *= -1; | |
359 int16_t yy = y; | |
360 if ((static_cast<uint16_t>(m) * 100 + d) < 300) | |
361 yy--; | |
362 int ydays = gAfxGetYearDays(yy); | |
363 while (ldays >= ydays) { | |
364 y--; | |
365 ldays -= ydays; | |
366 yy--; | |
367 int mdays = gAfxGetMonthDays(y, m); | |
368 if (d > mdays) { | |
369 m++; | |
370 d -= mdays; | |
371 } | |
372 ydays = gAfxGetYearDays(yy); | |
373 } | |
374 while (ldays >= d) { | |
375 ldays -= d; | |
376 m--; | |
377 d = gAfxGetMonthDays(y, m); | |
378 } | |
379 d -= ldays; | |
380 } | |
381 | |
382 dt.year = y; | |
383 dt.month = m; | |
384 dt.day = d; | |
385 | |
386 return *this; | |
387 } | |
388 | |
389 CPDFSDK_DateTime& CPDFSDK_DateTime::AddSeconds(int seconds) { | |
390 if (seconds == 0) | |
391 return *this; | |
392 | |
393 int n; | |
394 int days; | |
395 | |
396 n = dt.hour * 3600 + dt.minute * 60 + dt.second + seconds; | |
397 if (n < 0) { | |
398 days = (n - 86399) / 86400; | |
399 n -= days * 86400; | |
400 } else { | |
401 days = n / 86400; | |
402 n %= 86400; | |
403 } | |
404 dt.hour = static_cast<uint8_t>(n / 3600); | |
405 dt.hour %= 24; | |
406 n %= 3600; | |
407 dt.minute = static_cast<uint8_t>(n / 60); | |
408 dt.second = static_cast<uint8_t>(n % 60); | |
409 if (days != 0) | |
410 AddDays(days); | |
411 | |
412 return *this; | |
413 } | |
OLD | NEW |