OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "fpdfsdk/include/cpdfsdk_datetime.h" | 7 #include "fpdfsdk/include/cpdfsdk_datetime.h" |
8 | 8 |
9 #include "core/fxcrt/include/fx_ext.h" | 9 #include "core/fxcrt/include/fx_ext.h" |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 | 59 |
60 } // namespace | 60 } // namespace |
61 | 61 |
62 CPDFSDK_DateTime::CPDFSDK_DateTime() { | 62 CPDFSDK_DateTime::CPDFSDK_DateTime() { |
63 ResetDateTime(); | 63 ResetDateTime(); |
64 } | 64 } |
65 | 65 |
66 CPDFSDK_DateTime::CPDFSDK_DateTime(const CFX_ByteString& dtStr) { | 66 CPDFSDK_DateTime::CPDFSDK_DateTime(const CFX_ByteString& dtStr) { |
67 ResetDateTime(); | 67 ResetDateTime(); |
68 | |
69 FromPDFDateTimeString(dtStr); | 68 FromPDFDateTimeString(dtStr); |
70 } | 69 } |
71 | 70 |
72 CPDFSDK_DateTime::CPDFSDK_DateTime(const CPDFSDK_DateTime& datetime) { | 71 CPDFSDK_DateTime::CPDFSDK_DateTime(const CPDFSDK_DateTime& that) |
73 FXSYS_memcpy(&dt, &datetime.dt, sizeof(FX_DATETIME)); | 72 : m_year(that.m_year), |
74 } | 73 m_month(that.m_month), |
| 74 m_day(that.m_day), |
| 75 m_hour(that.m_hour), |
| 76 m_minute(that.m_minute), |
| 77 m_second(that.m_second), |
| 78 m_tzHour(that.m_tzHour), |
| 79 m_tzMinute(that.m_tzMinute) {} |
75 | 80 |
76 CPDFSDK_DateTime::CPDFSDK_DateTime(const FX_SYSTEMTIME& st) { | 81 CPDFSDK_DateTime::CPDFSDK_DateTime(const FX_SYSTEMTIME& st) { |
77 tzset(); | 82 tzset(); |
78 | 83 |
79 dt.year = static_cast<int16_t>(st.wYear); | 84 m_year = static_cast<int16_t>(st.wYear); |
80 dt.month = static_cast<uint8_t>(st.wMonth); | 85 m_month = static_cast<uint8_t>(st.wMonth); |
81 dt.day = static_cast<uint8_t>(st.wDay); | 86 m_day = static_cast<uint8_t>(st.wDay); |
82 dt.hour = static_cast<uint8_t>(st.wHour); | 87 m_hour = static_cast<uint8_t>(st.wHour); |
83 dt.minute = static_cast<uint8_t>(st.wMinute); | 88 m_minute = static_cast<uint8_t>(st.wMinute); |
84 dt.second = static_cast<uint8_t>(st.wSecond); | 89 m_second = static_cast<uint8_t>(st.wSecond); |
85 } | 90 } |
86 | 91 |
87 void CPDFSDK_DateTime::ResetDateTime() { | 92 void CPDFSDK_DateTime::ResetDateTime() { |
88 tzset(); | 93 tzset(); |
89 | 94 |
90 time_t curTime; | 95 time_t curTime; |
91 time(&curTime); | 96 time(&curTime); |
| 97 |
92 struct tm* newtime = localtime(&curTime); | 98 struct tm* newtime = localtime(&curTime); |
93 | 99 m_year = newtime->tm_year + 1900; |
94 dt.year = newtime->tm_year + 1900; | 100 m_month = newtime->tm_mon + 1; |
95 dt.month = newtime->tm_mon + 1; | 101 m_day = newtime->tm_mday; |
96 dt.day = newtime->tm_mday; | 102 m_hour = newtime->tm_hour; |
97 dt.hour = newtime->tm_hour; | 103 m_minute = newtime->tm_min; |
98 dt.minute = newtime->tm_min; | 104 m_second = newtime->tm_sec; |
99 dt.second = newtime->tm_sec; | |
100 } | 105 } |
101 | 106 |
102 bool CPDFSDK_DateTime::operator==(const CPDFSDK_DateTime& datetime) const { | 107 bool CPDFSDK_DateTime::operator==(const CPDFSDK_DateTime& that) const { |
103 return (FXSYS_memcmp(&dt, &datetime.dt, sizeof(FX_DATETIME)) == 0); | 108 return m_year == that.m_year && m_month == that.m_month && |
| 109 m_day == that.m_day && m_hour == that.m_hour && |
| 110 m_minute == that.m_minute && m_second == that.m_second && |
| 111 m_tzHour == that.m_tzHour && m_tzMinute == that.m_tzMinute; |
104 } | 112 } |
105 | 113 |
106 bool CPDFSDK_DateTime::operator!=(const CPDFSDK_DateTime& datetime) const { | 114 bool CPDFSDK_DateTime::operator!=(const CPDFSDK_DateTime& datetime) const { |
107 return !(*this == datetime); | 115 return !(*this == datetime); |
108 } | 116 } |
109 | 117 |
110 time_t CPDFSDK_DateTime::ToTime_t() const { | 118 time_t CPDFSDK_DateTime::ToTime_t() const { |
111 struct tm newtime; | 119 struct tm newtime; |
112 | 120 |
113 newtime.tm_year = dt.year - 1900; | 121 newtime.tm_year = m_year - 1900; |
114 newtime.tm_mon = dt.month - 1; | 122 newtime.tm_mon = m_month - 1; |
115 newtime.tm_mday = dt.day; | 123 newtime.tm_mday = m_day; |
116 newtime.tm_hour = dt.hour; | 124 newtime.tm_hour = m_hour; |
117 newtime.tm_min = dt.minute; | 125 newtime.tm_min = m_minute; |
118 newtime.tm_sec = dt.second; | 126 newtime.tm_sec = m_second; |
119 | 127 |
120 return mktime(&newtime); | 128 return mktime(&newtime); |
121 } | 129 } |
122 | 130 |
123 CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString( | 131 CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString( |
124 const CFX_ByteString& dtStr) { | 132 const CFX_ByteString& dtStr) { |
125 int strLength = dtStr.GetLength(); | 133 int strLength = dtStr.GetLength(); |
126 if (strLength <= 0) | 134 if (strLength <= 0) |
127 return *this; | 135 return *this; |
128 | 136 |
129 int i = 0; | 137 int i = 0; |
130 while (i < strLength && !std::isdigit(dtStr[i])) | 138 while (i < strLength && !std::isdigit(dtStr[i])) |
131 ++i; | 139 ++i; |
132 | 140 |
133 if (i >= strLength) | 141 if (i >= strLength) |
134 return *this; | 142 return *this; |
135 | 143 |
136 int j = 0; | 144 int j = 0; |
137 int k = 0; | 145 int k = 0; |
138 FX_CHAR ch; | 146 FX_CHAR ch; |
139 while (i < strLength && j < 4) { | 147 while (i < strLength && j < 4) { |
140 ch = dtStr[i]; | 148 ch = dtStr[i]; |
141 k = k * 10 + FXSYS_toDecimalDigit(ch); | 149 k = k * 10 + FXSYS_toDecimalDigit(ch); |
142 j++; | 150 j++; |
143 if (!std::isdigit(ch)) | 151 if (!std::isdigit(ch)) |
144 break; | 152 break; |
145 i++; | 153 i++; |
146 } | 154 } |
147 dt.year = static_cast<int16_t>(k); | 155 m_year = static_cast<int16_t>(k); |
148 if (i >= strLength || j < 4) | 156 if (i >= strLength || j < 4) |
149 return *this; | 157 return *this; |
150 | 158 |
151 j = 0; | 159 j = 0; |
152 k = 0; | 160 k = 0; |
153 while (i < strLength && j < 2) { | 161 while (i < strLength && j < 2) { |
154 ch = dtStr[i]; | 162 ch = dtStr[i]; |
155 k = k * 10 + FXSYS_toDecimalDigit(ch); | 163 k = k * 10 + FXSYS_toDecimalDigit(ch); |
156 j++; | 164 j++; |
157 if (!std::isdigit(ch)) | 165 if (!std::isdigit(ch)) |
158 break; | 166 break; |
159 i++; | 167 i++; |
160 } | 168 } |
161 dt.month = static_cast<uint8_t>(k); | 169 m_month = static_cast<uint8_t>(k); |
162 if (i >= strLength || j < 2) | 170 if (i >= strLength || j < 2) |
163 return *this; | 171 return *this; |
164 | 172 |
165 j = 0; | 173 j = 0; |
166 k = 0; | 174 k = 0; |
167 while (i < strLength && j < 2) { | 175 while (i < strLength && j < 2) { |
168 ch = dtStr[i]; | 176 ch = dtStr[i]; |
169 k = k * 10 + FXSYS_toDecimalDigit(ch); | 177 k = k * 10 + FXSYS_toDecimalDigit(ch); |
170 j++; | 178 j++; |
171 if (!std::isdigit(ch)) | 179 if (!std::isdigit(ch)) |
172 break; | 180 break; |
173 i++; | 181 i++; |
174 } | 182 } |
175 dt.day = static_cast<uint8_t>(k); | 183 m_day = static_cast<uint8_t>(k); |
176 if (i >= strLength || j < 2) | 184 if (i >= strLength || j < 2) |
177 return *this; | 185 return *this; |
178 | 186 |
179 j = 0; | 187 j = 0; |
180 k = 0; | 188 k = 0; |
181 while (i < strLength && j < 2) { | 189 while (i < strLength && j < 2) { |
182 ch = dtStr[i]; | 190 ch = dtStr[i]; |
183 k = k * 10 + FXSYS_toDecimalDigit(ch); | 191 k = k * 10 + FXSYS_toDecimalDigit(ch); |
184 j++; | 192 j++; |
185 if (!std::isdigit(ch)) | 193 if (!std::isdigit(ch)) |
186 break; | 194 break; |
187 i++; | 195 i++; |
188 } | 196 } |
189 dt.hour = static_cast<uint8_t>(k); | 197 m_hour = static_cast<uint8_t>(k); |
190 if (i >= strLength || j < 2) | 198 if (i >= strLength || j < 2) |
191 return *this; | 199 return *this; |
192 | 200 |
193 j = 0; | 201 j = 0; |
194 k = 0; | 202 k = 0; |
195 while (i < strLength && j < 2) { | 203 while (i < strLength && j < 2) { |
196 ch = dtStr[i]; | 204 ch = dtStr[i]; |
197 k = k * 10 + FXSYS_toDecimalDigit(ch); | 205 k = k * 10 + FXSYS_toDecimalDigit(ch); |
198 j++; | 206 j++; |
199 if (!std::isdigit(ch)) | 207 if (!std::isdigit(ch)) |
200 break; | 208 break; |
201 i++; | 209 i++; |
202 } | 210 } |
203 dt.minute = static_cast<uint8_t>(k); | 211 m_minute = static_cast<uint8_t>(k); |
204 if (i >= strLength || j < 2) | 212 if (i >= strLength || j < 2) |
205 return *this; | 213 return *this; |
206 | 214 |
207 j = 0; | 215 j = 0; |
208 k = 0; | 216 k = 0; |
209 while (i < strLength && j < 2) { | 217 while (i < strLength && j < 2) { |
210 ch = dtStr[i]; | 218 ch = dtStr[i]; |
211 k = k * 10 + FXSYS_toDecimalDigit(ch); | 219 k = k * 10 + FXSYS_toDecimalDigit(ch); |
212 j++; | 220 j++; |
213 if (!std::isdigit(ch)) | 221 if (!std::isdigit(ch)) |
214 break; | 222 break; |
215 i++; | 223 i++; |
216 } | 224 } |
217 dt.second = static_cast<uint8_t>(k); | 225 m_second = static_cast<uint8_t>(k); |
218 if (i >= strLength || j < 2) | 226 if (i >= strLength || j < 2) |
219 return *this; | 227 return *this; |
220 | 228 |
221 ch = dtStr[i++]; | 229 ch = dtStr[i++]; |
222 if (ch != '-' && ch != '+') | 230 if (ch != '-' && ch != '+') |
223 return *this; | 231 return *this; |
224 if (ch == '-') | 232 if (ch == '-') |
225 dt.tzHour = -1; | 233 m_tzHour = -1; |
226 else | 234 else |
227 dt.tzHour = 1; | 235 m_tzHour = 1; |
228 j = 0; | 236 j = 0; |
229 k = 0; | 237 k = 0; |
230 while (i < strLength && j < 2) { | 238 while (i < strLength && j < 2) { |
231 ch = dtStr[i]; | 239 ch = dtStr[i]; |
232 k = k * 10 + FXSYS_toDecimalDigit(ch); | 240 k = k * 10 + FXSYS_toDecimalDigit(ch); |
233 j++; | 241 j++; |
234 if (!std::isdigit(ch)) | 242 if (!std::isdigit(ch)) |
235 break; | 243 break; |
236 i++; | 244 i++; |
237 } | 245 } |
238 dt.tzHour *= static_cast<int8_t>(k); | 246 m_tzHour *= static_cast<int8_t>(k); |
239 if (i >= strLength || j < 2) | 247 if (i >= strLength || j < 2) |
240 return *this; | 248 return *this; |
241 | 249 |
242 if (dtStr[i++] != '\'') | 250 if (dtStr[i++] != '\'') |
243 return *this; | 251 return *this; |
244 j = 0; | 252 j = 0; |
245 k = 0; | 253 k = 0; |
246 while (i < strLength && j < 2) { | 254 while (i < strLength && j < 2) { |
247 ch = dtStr[i]; | 255 ch = dtStr[i]; |
248 k = k * 10 + FXSYS_toDecimalDigit(ch); | 256 k = k * 10 + FXSYS_toDecimalDigit(ch); |
249 j++; | 257 j++; |
250 if (!std::isdigit(ch)) | 258 if (!std::isdigit(ch)) |
251 break; | 259 break; |
252 i++; | 260 i++; |
253 } | 261 } |
254 dt.tzMinute = static_cast<uint8_t>(k); | 262 m_tzMinute = static_cast<uint8_t>(k); |
255 return *this; | 263 return *this; |
256 } | 264 } |
257 | 265 |
258 CFX_ByteString CPDFSDK_DateTime::ToCommonDateTimeString() { | 266 CFX_ByteString CPDFSDK_DateTime::ToCommonDateTimeString() { |
259 CFX_ByteString str1; | 267 CFX_ByteString str1; |
260 str1.Format("%04d-%02u-%02u %02u:%02u:%02u ", dt.year, dt.month, dt.day, | 268 str1.Format("%04d-%02u-%02u %02u:%02u:%02u ", m_year, m_month, m_day, m_hour, |
261 dt.hour, dt.minute, dt.second); | 269 m_minute, m_second); |
262 if (dt.tzHour < 0) | 270 if (m_tzHour < 0) |
263 str1 += "-"; | 271 str1 += "-"; |
264 else | 272 else |
265 str1 += "+"; | 273 str1 += "+"; |
266 CFX_ByteString str2; | 274 CFX_ByteString str2; |
267 str2.Format("%02d:%02u", std::abs(static_cast<int>(dt.tzHour)), dt.tzMinute); | 275 str2.Format("%02d:%02u", std::abs(static_cast<int>(m_tzHour)), m_tzMinute); |
268 return str1 + str2; | 276 return str1 + str2; |
269 } | 277 } |
270 | 278 |
271 CFX_ByteString CPDFSDK_DateTime::ToPDFDateTimeString() { | 279 CFX_ByteString CPDFSDK_DateTime::ToPDFDateTimeString() { |
272 CFX_ByteString dtStr; | 280 CFX_ByteString dtStr; |
273 char tempStr[32]; | 281 char tempStr[32]; |
274 memset(tempStr, 0, sizeof(tempStr)); | 282 memset(tempStr, 0, sizeof(tempStr)); |
275 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "D:%04d%02u%02u%02u%02u%02u", | 283 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "D:%04d%02u%02u%02u%02u%02u", |
276 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second); | 284 m_year, m_month, m_day, m_hour, m_minute, m_second); |
277 dtStr = CFX_ByteString(tempStr); | 285 dtStr = CFX_ByteString(tempStr); |
278 if (dt.tzHour < 0) | 286 if (m_tzHour < 0) |
279 dtStr += CFX_ByteString("-"); | 287 dtStr += CFX_ByteString("-"); |
280 else | 288 else |
281 dtStr += CFX_ByteString("+"); | 289 dtStr += CFX_ByteString("+"); |
282 memset(tempStr, 0, sizeof(tempStr)); | 290 memset(tempStr, 0, sizeof(tempStr)); |
283 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "%02d'%02u'", | 291 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "%02d'%02u'", |
284 std::abs(static_cast<int>(dt.tzHour)), dt.tzMinute); | 292 std::abs(static_cast<int>(m_tzHour)), m_tzMinute); |
285 dtStr += CFX_ByteString(tempStr); | 293 dtStr += CFX_ByteString(tempStr); |
286 return dtStr; | 294 return dtStr; |
287 } | 295 } |
288 | 296 |
289 void CPDFSDK_DateTime::ToSystemTime(FX_SYSTEMTIME& st) { | 297 void CPDFSDK_DateTime::ToSystemTime(FX_SYSTEMTIME& st) { |
290 time_t t = this->ToTime_t(); | 298 time_t t = this->ToTime_t(); |
291 struct tm* pTime = localtime(&t); | 299 struct tm* pTime = localtime(&t); |
292 | 300 |
293 if (!pTime) | 301 if (!pTime) |
294 return; | 302 return; |
295 | 303 |
296 st.wYear = static_cast<uint16_t>(pTime->tm_year) + 1900; | 304 st.wYear = static_cast<uint16_t>(pTime->tm_year) + 1900; |
297 st.wMonth = static_cast<uint16_t>(pTime->tm_mon) + 1; | 305 st.wMonth = static_cast<uint16_t>(pTime->tm_mon) + 1; |
298 st.wDay = static_cast<uint16_t>(pTime->tm_mday); | 306 st.wDay = static_cast<uint16_t>(pTime->tm_mday); |
299 st.wDayOfWeek = static_cast<uint16_t>(pTime->tm_wday); | 307 st.wDayOfWeek = static_cast<uint16_t>(pTime->tm_wday); |
300 st.wHour = static_cast<uint16_t>(pTime->tm_hour); | 308 st.wHour = static_cast<uint16_t>(pTime->tm_hour); |
301 st.wMinute = static_cast<uint16_t>(pTime->tm_min); | 309 st.wMinute = static_cast<uint16_t>(pTime->tm_min); |
302 st.wSecond = static_cast<uint16_t>(pTime->tm_sec); | 310 st.wSecond = static_cast<uint16_t>(pTime->tm_sec); |
303 st.wMilliseconds = 0; | 311 st.wMilliseconds = 0; |
304 } | 312 } |
305 | 313 |
306 CPDFSDK_DateTime CPDFSDK_DateTime::ToGMT() const { | 314 CPDFSDK_DateTime CPDFSDK_DateTime::ToGMT() const { |
307 CPDFSDK_DateTime new_dt = *this; | 315 CPDFSDK_DateTime new_dt = *this; |
308 new_dt.AddSeconds( | 316 new_dt.AddSeconds(-GetTimeZoneInSeconds(new_dt.m_tzHour, new_dt.m_tzMinute)); |
309 -GetTimeZoneInSeconds(new_dt.dt.tzHour, new_dt.dt.tzMinute)); | 317 new_dt.m_tzHour = 0; |
310 new_dt.dt.tzHour = 0; | 318 new_dt.m_tzMinute = 0; |
311 new_dt.dt.tzMinute = 0; | |
312 return new_dt; | 319 return new_dt; |
313 } | 320 } |
314 | 321 |
315 CPDFSDK_DateTime& CPDFSDK_DateTime::AddDays(short days) { | 322 CPDFSDK_DateTime& CPDFSDK_DateTime::AddDays(short days) { |
316 if (days == 0) | 323 if (days == 0) |
317 return *this; | 324 return *this; |
318 | 325 |
319 int16_t y = dt.year; | 326 int16_t y = m_year; |
320 uint8_t m = dt.month; | 327 uint8_t m = m_month; |
321 uint8_t d = dt.day; | 328 uint8_t d = m_day; |
322 | 329 |
323 int ldays = days; | 330 int ldays = days; |
324 if (ldays > 0) { | 331 if (ldays > 0) { |
325 int16_t yy = y; | 332 int16_t yy = y; |
326 if ((static_cast<uint16_t>(m) * 100 + d) > 300) | 333 if ((static_cast<uint16_t>(m) * 100 + d) > 300) |
327 yy++; | 334 yy++; |
328 int ydays = GetYearDays(yy); | 335 int ydays = GetYearDays(yy); |
329 int mdays; | 336 int mdays; |
330 while (ldays >= ydays) { | 337 while (ldays >= ydays) { |
331 y++; | 338 y++; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 ydays = GetYearDays(yy); | 371 ydays = GetYearDays(yy); |
365 } | 372 } |
366 while (ldays >= d) { | 373 while (ldays >= d) { |
367 ldays -= d; | 374 ldays -= d; |
368 m--; | 375 m--; |
369 d = GetMonthDays(y, m); | 376 d = GetMonthDays(y, m); |
370 } | 377 } |
371 d -= ldays; | 378 d -= ldays; |
372 } | 379 } |
373 | 380 |
374 dt.year = y; | 381 m_year = y; |
375 dt.month = m; | 382 m_month = m; |
376 dt.day = d; | 383 m_day = d; |
377 | 384 |
378 return *this; | 385 return *this; |
379 } | 386 } |
380 | 387 |
381 CPDFSDK_DateTime& CPDFSDK_DateTime::AddSeconds(int seconds) { | 388 CPDFSDK_DateTime& CPDFSDK_DateTime::AddSeconds(int seconds) { |
382 if (seconds == 0) | 389 if (seconds == 0) |
383 return *this; | 390 return *this; |
384 | 391 |
385 int n; | 392 int n; |
386 int days; | 393 int days; |
387 | 394 |
388 n = dt.hour * 3600 + dt.minute * 60 + dt.second + seconds; | 395 n = m_hour * 3600 + m_minute * 60 + m_second + seconds; |
389 if (n < 0) { | 396 if (n < 0) { |
390 days = (n - 86399) / 86400; | 397 days = (n - 86399) / 86400; |
391 n -= days * 86400; | 398 n -= days * 86400; |
392 } else { | 399 } else { |
393 days = n / 86400; | 400 days = n / 86400; |
394 n %= 86400; | 401 n %= 86400; |
395 } | 402 } |
396 dt.hour = static_cast<uint8_t>(n / 3600); | 403 m_hour = static_cast<uint8_t>(n / 3600); |
397 dt.hour %= 24; | 404 m_hour %= 24; |
398 n %= 3600; | 405 n %= 3600; |
399 dt.minute = static_cast<uint8_t>(n / 60); | 406 m_minute = static_cast<uint8_t>(n / 60); |
400 dt.second = static_cast<uint8_t>(n % 60); | 407 m_second = static_cast<uint8_t>(n % 60); |
401 if (days != 0) | 408 if (days != 0) |
402 AddDays(days); | 409 AddDays(days); |
403 | 410 |
404 return *this; | 411 return *this; |
405 } | 412 } |
OLD | NEW |