/src/nspr/pr/src/misc/prtime.c
Line | Count | Source |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | /* |
6 | | * prtime.c -- |
7 | | * |
8 | | * NSPR date and time functions |
9 | | * |
10 | | */ |
11 | | |
12 | | #include "prinit.h" |
13 | | #include "prtime.h" |
14 | | #include "prlock.h" |
15 | | #include "prprf.h" |
16 | | #include "prlog.h" |
17 | | |
18 | | #include <string.h> |
19 | | #include <ctype.h> |
20 | | #include <errno.h> /* for EINVAL */ |
21 | | #include <time.h> |
22 | | |
23 | | /* |
24 | | * The COUNT_LEAPS macro counts the number of leap years passed by |
25 | | * till the start of the given year Y. At the start of the year 4 |
26 | | * A.D. the number of leap years passed by is 0, while at the start of |
27 | | * the year 5 A.D. this count is 1. The number of years divisible by |
28 | | * 100 but not divisible by 400 (the non-leap years) is deducted from |
29 | | * the count to get the correct number of leap years. |
30 | | * |
31 | | * The COUNT_DAYS macro counts the number of days since 01/01/01 till the |
32 | | * start of the given year Y. The number of days at the start of the year |
33 | | * 1 is 0 while the number of days at the start of the year 2 is 365 |
34 | | * (which is ((2)-1) * 365) and so on. The reference point is 01/01/01 |
35 | | * midnight 00:00:00. |
36 | | */ |
37 | | |
38 | 85.9k | #define COUNT_LEAPS(Y) (((Y) - 1) / 4 - ((Y) - 1) / 100 + ((Y) - 1) / 400) |
39 | 85.9k | #define COUNT_DAYS(Y) (((Y) - 1) * 365 + COUNT_LEAPS(Y)) |
40 | 42.9k | #define DAYS_BETWEEN_YEARS(A, B) (COUNT_DAYS(B) - COUNT_DAYS(A)) |
41 | | |
42 | | /* |
43 | | * Static variables used by functions in this file |
44 | | */ |
45 | | |
46 | | /* |
47 | | * The following array contains the day of year for the last day of |
48 | | * each month, where index 1 is January, and day 0 is January 1. |
49 | | */ |
50 | | |
51 | | static const PRInt16 lastDayOfMonth[2][13] = { |
52 | | {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364}, |
53 | | {-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}}; |
54 | | |
55 | | /* |
56 | | * The number of days in a month |
57 | | */ |
58 | | |
59 | | static const PRInt8 nDays[2][12] = { |
60 | | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, |
61 | | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; |
62 | | |
63 | | /* |
64 | | * Declarations for internal functions defined later in this file. |
65 | | */ |
66 | | |
67 | | static void ComputeGMT(PRTime time, PRExplodedTime* gmt); |
68 | | static int IsLeapYear(PRInt16 year); |
69 | | static void ApplySecOffset(PRExplodedTime* time, PRInt32 secOffset); |
70 | | |
71 | | /* |
72 | | *------------------------------------------------------------------------ |
73 | | * |
74 | | * ComputeGMT -- |
75 | | * |
76 | | * Caveats: |
77 | | * - we ignore leap seconds |
78 | | * |
79 | | *------------------------------------------------------------------------ |
80 | | */ |
81 | | |
82 | 0 | static void ComputeGMT(PRTime time, PRExplodedTime* gmt) { |
83 | 0 | PRInt32 tmp, rem; |
84 | 0 | PRInt32 numDays; |
85 | 0 | PRInt64 numDays64, rem64; |
86 | 0 | int isLeap; |
87 | 0 | PRInt64 sec; |
88 | 0 | PRInt64 usec; |
89 | 0 | PRInt64 usecPerSec; |
90 | 0 | PRInt64 secPerDay; |
91 | | |
92 | | /* |
93 | | * We first do the usec, sec, min, hour thing so that we do not |
94 | | * have to do LL arithmetic. |
95 | | */ |
96 | |
|
97 | 0 | LL_I2L(usecPerSec, 1000000L); |
98 | 0 | LL_DIV(sec, time, usecPerSec); |
99 | 0 | LL_MOD(usec, time, usecPerSec); |
100 | 0 | LL_L2I(gmt->tm_usec, usec); |
101 | | /* Correct for weird mod semantics so the remainder is always positive */ |
102 | 0 | if (gmt->tm_usec < 0) { |
103 | 0 | PRInt64 one; |
104 | |
|
105 | 0 | LL_I2L(one, 1L); |
106 | 0 | LL_SUB(sec, sec, one); |
107 | 0 | gmt->tm_usec += 1000000L; |
108 | 0 | } |
109 | |
|
110 | 0 | LL_I2L(secPerDay, 86400L); |
111 | 0 | LL_DIV(numDays64, sec, secPerDay); |
112 | 0 | LL_MOD(rem64, sec, secPerDay); |
113 | | /* We are sure both of these numbers can fit into PRInt32 */ |
114 | 0 | LL_L2I(numDays, numDays64); |
115 | 0 | LL_L2I(rem, rem64); |
116 | 0 | if (rem < 0) { |
117 | 0 | numDays--; |
118 | 0 | rem += 86400L; |
119 | 0 | } |
120 | | |
121 | | /* Compute day of week. Epoch started on a Thursday. */ |
122 | |
|
123 | 0 | gmt->tm_wday = (numDays + 4) % 7; |
124 | 0 | if (gmt->tm_wday < 0) { |
125 | 0 | gmt->tm_wday += 7; |
126 | 0 | } |
127 | | |
128 | | /* Compute the time of day. */ |
129 | |
|
130 | 0 | gmt->tm_hour = rem / 3600; |
131 | 0 | rem %= 3600; |
132 | 0 | gmt->tm_min = rem / 60; |
133 | 0 | gmt->tm_sec = rem % 60; |
134 | | |
135 | | /* |
136 | | * Compute the year by finding the 400 year period, then working |
137 | | * down from there. |
138 | | * |
139 | | * Since numDays is originally the number of days since January 1, 1970, |
140 | | * we must change it to be the number of days from January 1, 0001. |
141 | | */ |
142 | |
|
143 | 0 | numDays += 719162; /* 719162 = days from year 1 up to 1970 */ |
144 | 0 | tmp = numDays / 146097; /* 146097 = days in 400 years */ |
145 | 0 | rem = numDays % 146097; |
146 | 0 | gmt->tm_year = tmp * 400 + 1; |
147 | | |
148 | | /* Compute the 100 year period. */ |
149 | |
|
150 | 0 | tmp = rem / 36524; /* 36524 = days in 100 years */ |
151 | 0 | rem %= 36524; |
152 | 0 | if (tmp == 4) { /* the 400th year is a leap year */ |
153 | 0 | tmp = 3; |
154 | 0 | rem = 36524; |
155 | 0 | } |
156 | 0 | gmt->tm_year += tmp * 100; |
157 | | |
158 | | /* Compute the 4 year period. */ |
159 | |
|
160 | 0 | tmp = rem / 1461; /* 1461 = days in 4 years */ |
161 | 0 | rem %= 1461; |
162 | 0 | gmt->tm_year += tmp * 4; |
163 | | |
164 | | /* Compute which year in the 4. */ |
165 | |
|
166 | 0 | tmp = rem / 365; |
167 | 0 | rem %= 365; |
168 | 0 | if (tmp == 4) { /* the 4th year is a leap year */ |
169 | 0 | tmp = 3; |
170 | 0 | rem = 365; |
171 | 0 | } |
172 | |
|
173 | 0 | gmt->tm_year += tmp; |
174 | 0 | gmt->tm_yday = rem; |
175 | 0 | isLeap = IsLeapYear(gmt->tm_year); |
176 | | |
177 | | /* Compute the month and day of month. */ |
178 | |
|
179 | 0 | for (tmp = 1; lastDayOfMonth[isLeap][tmp] < gmt->tm_yday; tmp++) { |
180 | 0 | } |
181 | 0 | gmt->tm_month = --tmp; |
182 | 0 | gmt->tm_mday = gmt->tm_yday - lastDayOfMonth[isLeap][tmp]; |
183 | |
|
184 | 0 | gmt->tm_params.tp_gmt_offset = 0; |
185 | 0 | gmt->tm_params.tp_dst_offset = 0; |
186 | 0 | } |
187 | | |
188 | | /* |
189 | | *------------------------------------------------------------------------ |
190 | | * |
191 | | * PR_ExplodeTime -- |
192 | | * |
193 | | * Cf. struct tm *gmtime(const time_t *tp) and |
194 | | * struct tm *localtime(const time_t *tp) |
195 | | * |
196 | | *------------------------------------------------------------------------ |
197 | | */ |
198 | | |
199 | | PR_IMPLEMENT(void) |
200 | 0 | PR_ExplodeTime(PRTime usecs, PRTimeParamFn params, PRExplodedTime* exploded) { |
201 | 0 | ComputeGMT(usecs, exploded); |
202 | 0 | exploded->tm_params = params(exploded); |
203 | 0 | ApplySecOffset(exploded, exploded->tm_params.tp_gmt_offset + |
204 | 0 | exploded->tm_params.tp_dst_offset); |
205 | 0 | } |
206 | | |
207 | | /* |
208 | | *------------------------------------------------------------------------ |
209 | | * |
210 | | * PR_ImplodeTime -- |
211 | | * |
212 | | * Cf. time_t mktime(struct tm *tp) |
213 | | * Note that 1 year has < 2^25 seconds. So an PRInt32 is large enough. |
214 | | * |
215 | | *------------------------------------------------------------------------ |
216 | | */ |
217 | | PR_IMPLEMENT(PRTime) |
218 | 21.4k | PR_ImplodeTime(const PRExplodedTime* exploded) { |
219 | 21.4k | PRExplodedTime copy; |
220 | 21.4k | PRTime retVal; |
221 | 21.4k | PRInt64 secPerDay, usecPerSec; |
222 | 21.4k | PRInt64 temp; |
223 | 21.4k | PRInt64 numSecs64; |
224 | 21.4k | PRInt32 numDays; |
225 | 21.4k | PRInt32 numSecs; |
226 | | |
227 | | /* Normalize first. Do this on our copy */ |
228 | 21.4k | copy = *exploded; |
229 | 21.4k | PR_NormalizeTime(©, PR_GMTParameters); |
230 | | |
231 | 21.4k | numDays = DAYS_BETWEEN_YEARS(1970, copy.tm_year); |
232 | | |
233 | 21.4k | numSecs = copy.tm_yday * 86400 + copy.tm_hour * 3600 + copy.tm_min * 60 + |
234 | 21.4k | copy.tm_sec; |
235 | | |
236 | 21.4k | LL_I2L(temp, numDays); |
237 | 21.4k | LL_I2L(secPerDay, 86400); |
238 | 21.4k | LL_MUL(temp, temp, secPerDay); |
239 | 21.4k | LL_I2L(numSecs64, numSecs); |
240 | 21.4k | LL_ADD(numSecs64, numSecs64, temp); |
241 | | |
242 | | /* apply the GMT and DST offsets */ |
243 | 21.4k | LL_I2L(temp, copy.tm_params.tp_gmt_offset); |
244 | 21.4k | LL_SUB(numSecs64, numSecs64, temp); |
245 | 21.4k | LL_I2L(temp, copy.tm_params.tp_dst_offset); |
246 | 21.4k | LL_SUB(numSecs64, numSecs64, temp); |
247 | | |
248 | 21.4k | LL_I2L(usecPerSec, 1000000L); |
249 | 21.4k | LL_MUL(temp, numSecs64, usecPerSec); |
250 | 21.4k | LL_I2L(retVal, copy.tm_usec); |
251 | 21.4k | LL_ADD(retVal, retVal, temp); |
252 | | |
253 | 21.4k | return retVal; |
254 | 21.4k | } |
255 | | |
256 | | /* |
257 | | *------------------------------------------------------------------------- |
258 | | * |
259 | | * IsLeapYear -- |
260 | | * |
261 | | * Returns 1 if the year is a leap year, 0 otherwise. |
262 | | * |
263 | | *------------------------------------------------------------------------- |
264 | | */ |
265 | | |
266 | 43.8k | static int IsLeapYear(PRInt16 year) { |
267 | 43.8k | if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { |
268 | 5.61k | return 1; |
269 | 5.61k | } |
270 | 38.2k | return 0; |
271 | 43.8k | } |
272 | | |
273 | | /* |
274 | | * 'secOffset' should be less than 86400 (i.e., a day). |
275 | | * 'time' should point to a normalized PRExplodedTime. |
276 | | */ |
277 | | |
278 | 21.4k | static void ApplySecOffset(PRExplodedTime* time, PRInt32 secOffset) { |
279 | 21.4k | time->tm_sec += secOffset; |
280 | | |
281 | | /* Note that in this implementation we do not count leap seconds */ |
282 | 21.4k | if (time->tm_sec < 0 || time->tm_sec >= 60) { |
283 | 0 | time->tm_min += time->tm_sec / 60; |
284 | 0 | time->tm_sec %= 60; |
285 | 0 | if (time->tm_sec < 0) { |
286 | 0 | time->tm_sec += 60; |
287 | 0 | time->tm_min--; |
288 | 0 | } |
289 | 0 | } |
290 | | |
291 | 21.4k | if (time->tm_min < 0 || time->tm_min >= 60) { |
292 | 0 | time->tm_hour += time->tm_min / 60; |
293 | 0 | time->tm_min %= 60; |
294 | 0 | if (time->tm_min < 0) { |
295 | 0 | time->tm_min += 60; |
296 | 0 | time->tm_hour--; |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | 21.4k | if (time->tm_hour < 0) { |
301 | | /* Decrement mday, yday, and wday */ |
302 | 0 | time->tm_hour += 24; |
303 | 0 | time->tm_mday--; |
304 | 0 | time->tm_yday--; |
305 | 0 | if (time->tm_mday < 1) { |
306 | 0 | time->tm_month--; |
307 | 0 | if (time->tm_month < 0) { |
308 | 0 | time->tm_month = 11; |
309 | 0 | time->tm_year--; |
310 | 0 | if (IsLeapYear(time->tm_year)) { |
311 | 0 | time->tm_yday = 365; |
312 | 0 | } else { |
313 | 0 | time->tm_yday = 364; |
314 | 0 | } |
315 | 0 | } |
316 | 0 | time->tm_mday = nDays[IsLeapYear(time->tm_year)][time->tm_month]; |
317 | 0 | } |
318 | 0 | time->tm_wday--; |
319 | 0 | if (time->tm_wday < 0) { |
320 | 0 | time->tm_wday = 6; |
321 | 0 | } |
322 | 21.4k | } else if (time->tm_hour > 23) { |
323 | | /* Increment mday, yday, and wday */ |
324 | 0 | time->tm_hour -= 24; |
325 | 0 | time->tm_mday++; |
326 | 0 | time->tm_yday++; |
327 | 0 | if (time->tm_mday > nDays[IsLeapYear(time->tm_year)][time->tm_month]) { |
328 | 0 | time->tm_mday = 1; |
329 | 0 | time->tm_month++; |
330 | 0 | if (time->tm_month > 11) { |
331 | 0 | time->tm_month = 0; |
332 | 0 | time->tm_year++; |
333 | 0 | time->tm_yday = 0; |
334 | 0 | } |
335 | 0 | } |
336 | 0 | time->tm_wday++; |
337 | 0 | if (time->tm_wday > 6) { |
338 | 0 | time->tm_wday = 0; |
339 | 0 | } |
340 | 0 | } |
341 | 21.4k | } |
342 | | |
343 | | PR_IMPLEMENT(void) |
344 | 21.4k | PR_NormalizeTime(PRExplodedTime* time, PRTimeParamFn params) { |
345 | 21.4k | int daysInMonth; |
346 | 21.4k | PRInt32 numDays; |
347 | | |
348 | | /* Get back to GMT */ |
349 | 21.4k | time->tm_sec -= time->tm_params.tp_gmt_offset + time->tm_params.tp_dst_offset; |
350 | 21.4k | time->tm_params.tp_gmt_offset = 0; |
351 | 21.4k | time->tm_params.tp_dst_offset = 0; |
352 | | |
353 | | /* Now normalize GMT */ |
354 | | |
355 | 21.4k | if (time->tm_usec < 0 || time->tm_usec >= 1000000) { |
356 | 0 | time->tm_sec += time->tm_usec / 1000000; |
357 | 0 | time->tm_usec %= 1000000; |
358 | 0 | if (time->tm_usec < 0) { |
359 | 0 | time->tm_usec += 1000000; |
360 | 0 | time->tm_sec--; |
361 | 0 | } |
362 | 0 | } |
363 | | |
364 | | /* Note that we do not count leap seconds in this implementation */ |
365 | 21.4k | if (time->tm_sec < 0 || time->tm_sec >= 60) { |
366 | 0 | time->tm_min += time->tm_sec / 60; |
367 | 0 | time->tm_sec %= 60; |
368 | 0 | if (time->tm_sec < 0) { |
369 | 0 | time->tm_sec += 60; |
370 | 0 | time->tm_min--; |
371 | 0 | } |
372 | 0 | } |
373 | | |
374 | 21.4k | if (time->tm_min < 0 || time->tm_min >= 60) { |
375 | 0 | time->tm_hour += time->tm_min / 60; |
376 | 0 | time->tm_min %= 60; |
377 | 0 | if (time->tm_min < 0) { |
378 | 0 | time->tm_min += 60; |
379 | 0 | time->tm_hour--; |
380 | 0 | } |
381 | 0 | } |
382 | | |
383 | 21.4k | if (time->tm_hour < 0 || time->tm_hour >= 24) { |
384 | 0 | time->tm_mday += time->tm_hour / 24; |
385 | 0 | time->tm_hour %= 24; |
386 | 0 | if (time->tm_hour < 0) { |
387 | 0 | time->tm_hour += 24; |
388 | 0 | time->tm_mday--; |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | | /* Normalize month and year before mday */ |
393 | 21.4k | if (time->tm_month < 0 || time->tm_month >= 12) { |
394 | 0 | time->tm_year += time->tm_month / 12; |
395 | 0 | time->tm_month %= 12; |
396 | 0 | if (time->tm_month < 0) { |
397 | 0 | time->tm_month += 12; |
398 | 0 | time->tm_year--; |
399 | 0 | } |
400 | 0 | } |
401 | | |
402 | | /* Now that month and year are in proper range, normalize mday */ |
403 | | |
404 | 21.4k | if (time->tm_mday < 1) { |
405 | | /* mday too small */ |
406 | 0 | do { |
407 | | /* the previous month */ |
408 | 0 | time->tm_month--; |
409 | 0 | if (time->tm_month < 0) { |
410 | 0 | time->tm_month = 11; |
411 | 0 | time->tm_year--; |
412 | 0 | } |
413 | 0 | time->tm_mday += nDays[IsLeapYear(time->tm_year)][time->tm_month]; |
414 | 0 | } while (time->tm_mday < 1); |
415 | 21.4k | } else { |
416 | 21.4k | daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month]; |
417 | 22.3k | while (time->tm_mday > daysInMonth) { |
418 | | /* mday too large */ |
419 | 848 | time->tm_mday -= daysInMonth; |
420 | 848 | time->tm_month++; |
421 | 848 | if (time->tm_month > 11) { |
422 | 0 | time->tm_month = 0; |
423 | 0 | time->tm_year++; |
424 | 0 | } |
425 | 848 | daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month]; |
426 | 848 | } |
427 | 21.4k | } |
428 | | |
429 | | /* Recompute yday and wday */ |
430 | 21.4k | time->tm_yday = |
431 | 21.4k | (PRInt16)time->tm_mday + lastDayOfMonth[IsLeapYear(time->tm_year)][time->tm_month]; |
432 | | |
433 | 21.4k | numDays = DAYS_BETWEEN_YEARS(1970, time->tm_year) + time->tm_yday; |
434 | 21.4k | time->tm_wday = (numDays + 4) % 7; |
435 | 21.4k | if (time->tm_wday < 0) { |
436 | 2.06k | time->tm_wday += 7; |
437 | 2.06k | } |
438 | | |
439 | | /* Recompute time parameters */ |
440 | | |
441 | 21.4k | time->tm_params = params(time); |
442 | | |
443 | 21.4k | ApplySecOffset(time, |
444 | 21.4k | time->tm_params.tp_gmt_offset + time->tm_params.tp_dst_offset); |
445 | 21.4k | } |
446 | | |
447 | | /* |
448 | | *------------------------------------------------------------------------- |
449 | | * |
450 | | * PR_LocalTimeParameters -- |
451 | | * |
452 | | * returns the time parameters for the local time zone |
453 | | * |
454 | | * The following uses localtime() from the standard C library. |
455 | | * (time.h) This is our fallback implementation. Unix, PC, and BeOS |
456 | | * use this version. A platform may have its own machine-dependent |
457 | | * implementation of this function. |
458 | | * |
459 | | *------------------------------------------------------------------------- |
460 | | */ |
461 | | |
462 | | #if defined(HAVE_INT_LOCALTIME_R) |
463 | | |
464 | | /* |
465 | | * In this case we could define the macro as |
466 | | * #define MT_safe_localtime(timer, result) \ |
467 | | * (localtime_r(timer, result) == 0 ? result : NULL) |
468 | | * I chose to compare the return value of localtime_r with -1 so |
469 | | * that I can catch the cases where localtime_r returns a pointer |
470 | | * to struct tm. The macro definition above would not be able to |
471 | | * detect such mistakes because it is legal to compare a pointer |
472 | | * with 0. |
473 | | */ |
474 | | |
475 | | # define MT_safe_localtime(timer, result) \ |
476 | | (localtime_r(timer, result) == -1 ? NULL : result) |
477 | | |
478 | | #elif defined(HAVE_POINTER_LOCALTIME_R) |
479 | | |
480 | 0 | # define MT_safe_localtime localtime_r |
481 | | |
482 | | #elif defined(_MSC_VER) |
483 | | |
484 | | /* Visual C++ has had localtime_s() since Visual C++ 2005. */ |
485 | | |
486 | | static struct tm* MT_safe_localtime(const time_t* clock, struct tm* result) { |
487 | | errno_t err = localtime_s(result, clock); |
488 | | if (err != 0) { |
489 | | errno = err; |
490 | | return NULL; |
491 | | } |
492 | | return result; |
493 | | } |
494 | | |
495 | | #else |
496 | | |
497 | | # define HAVE_LOCALTIME_MONITOR \ |
498 | | 1 /* We use 'monitor' to serialize our calls \ |
499 | | * to localtime(). */ |
500 | | static PRLock* monitor = NULL; |
501 | | |
502 | | static struct tm* MT_safe_localtime(const time_t* clock, struct tm* result) { |
503 | | struct tm* tmPtr; |
504 | | int needLock = PR_Initialized(); /* We need to use a lock to protect |
505 | | * against NSPR threads only when the |
506 | | * NSPR thread system is activated. */ |
507 | | |
508 | | if (needLock) { |
509 | | PR_Lock(monitor); |
510 | | } |
511 | | |
512 | | /* |
513 | | * Microsoft (all flavors) localtime() returns a NULL pointer if 'clock' |
514 | | * represents a time before midnight January 1, 1970. In |
515 | | * that case, we also return a NULL pointer and the struct tm |
516 | | * object pointed to by 'result' is not modified. |
517 | | * |
518 | | */ |
519 | | |
520 | | tmPtr = localtime(clock); |
521 | | |
522 | | if (tmPtr) { |
523 | | *result = *tmPtr; |
524 | | } else { |
525 | | result = NULL; |
526 | | } |
527 | | |
528 | | if (needLock) { |
529 | | PR_Unlock(monitor); |
530 | | } |
531 | | |
532 | | return result; |
533 | | } |
534 | | |
535 | | #endif /* definition of MT_safe_localtime() */ |
536 | | |
537 | 15 | void _PR_InitTime(void) { |
538 | | #ifdef HAVE_LOCALTIME_MONITOR |
539 | | monitor = PR_NewLock(); |
540 | | #endif |
541 | | #ifdef WINCE |
542 | | _MD_InitTime(); |
543 | | #endif |
544 | 15 | } |
545 | | |
546 | 0 | void _PR_CleanupTime(void) { |
547 | | #ifdef HAVE_LOCALTIME_MONITOR |
548 | | if (monitor) { |
549 | | PR_DestroyLock(monitor); |
550 | | monitor = NULL; |
551 | | } |
552 | | #endif |
553 | | #ifdef WINCE |
554 | | _MD_CleanupTime(); |
555 | | #endif |
556 | 0 | } |
557 | | |
558 | | #if defined(XP_UNIX) || defined(XP_PC) |
559 | | |
560 | | PR_IMPLEMENT(PRTimeParameters) |
561 | 0 | PR_LocalTimeParameters(const PRExplodedTime* gmt) { |
562 | 0 | PRTimeParameters retVal; |
563 | 0 | struct tm localTime; |
564 | 0 | struct tm* localTimeResult; |
565 | 0 | time_t secs; |
566 | 0 | PRTime secs64; |
567 | 0 | PRInt64 usecPerSec; |
568 | 0 | PRInt64 usecPerSec_1; |
569 | 0 | PRInt64 maxInt32; |
570 | 0 | PRInt64 minInt32; |
571 | 0 | PRInt32 dayOffset; |
572 | 0 | PRInt32 offset2Jan1970; |
573 | 0 | PRInt32 offsetNew; |
574 | 0 | int isdst2Jan1970; |
575 | | |
576 | | /* |
577 | | * Calculate the GMT offset. First, figure out what is |
578 | | * 00:00:00 Jan. 2, 1970 GMT (which is exactly a day, or 86400 |
579 | | * seconds, since the epoch) in local time. Then we calculate |
580 | | * the difference between local time and GMT in seconds: |
581 | | * gmt_offset = local_time - GMT |
582 | | * |
583 | | * Caveat: the validity of this calculation depends on two |
584 | | * assumptions: |
585 | | * 1. Daylight saving time was not in effect on Jan. 2, 1970. |
586 | | * 2. The time zone of the geographic location has not changed |
587 | | * since Jan. 2, 1970. |
588 | | */ |
589 | |
|
590 | 0 | secs = 86400L; |
591 | 0 | localTimeResult = MT_safe_localtime(&secs, &localTime); |
592 | 0 | PR_ASSERT(localTimeResult != NULL); |
593 | 0 | if (localTimeResult == NULL) { |
594 | | /* Shouldn't happen. Use safe fallback for optimized builds. */ |
595 | 0 | return PR_GMTParameters(gmt); |
596 | 0 | } |
597 | | |
598 | | /* GMT is 00:00:00, 2nd of Jan. */ |
599 | | |
600 | 0 | offset2Jan1970 = (PRInt32)localTime.tm_sec + 60L * (PRInt32)localTime.tm_min + |
601 | 0 | 3600L * (PRInt32)localTime.tm_hour + |
602 | 0 | 86400L * (PRInt32)((PRInt32)localTime.tm_mday - 2L); |
603 | |
|
604 | 0 | isdst2Jan1970 = localTime.tm_isdst; |
605 | | |
606 | | /* |
607 | | * Now compute DST offset. We calculate the overall offset |
608 | | * of local time from GMT, similar to above. The overall |
609 | | * offset has two components: gmt offset and dst offset. |
610 | | * We subtract gmt offset from the overall offset to get |
611 | | * the dst offset. |
612 | | * overall_offset = local_time - GMT |
613 | | * overall_offset = gmt_offset + dst_offset |
614 | | * ==> dst_offset = local_time - GMT - gmt_offset |
615 | | */ |
616 | |
|
617 | 0 | secs64 = PR_ImplodeTime(gmt); /* This is still in microseconds */ |
618 | 0 | LL_I2L(usecPerSec, PR_USEC_PER_SEC); |
619 | 0 | LL_I2L(usecPerSec_1, PR_USEC_PER_SEC - 1); |
620 | | /* Convert to seconds, truncating down (3.1 -> 3 and -3.1 -> -4) */ |
621 | 0 | if (LL_GE_ZERO(secs64)) { |
622 | 0 | LL_DIV(secs64, secs64, usecPerSec); |
623 | 0 | } else { |
624 | 0 | LL_NEG(secs64, secs64); |
625 | 0 | LL_ADD(secs64, secs64, usecPerSec_1); |
626 | 0 | LL_DIV(secs64, secs64, usecPerSec); |
627 | 0 | LL_NEG(secs64, secs64); |
628 | 0 | } |
629 | 0 | LL_I2L(maxInt32, PR_INT32_MAX); |
630 | 0 | LL_I2L(minInt32, PR_INT32_MIN); |
631 | 0 | if (LL_CMP(secs64, >, maxInt32) || LL_CMP(secs64, <, minInt32)) { |
632 | | /* secs64 is too large or too small for time_t (32-bit integer) */ |
633 | 0 | retVal.tp_gmt_offset = offset2Jan1970; |
634 | 0 | retVal.tp_dst_offset = 0; |
635 | 0 | return retVal; |
636 | 0 | } |
637 | 0 | LL_L2I(secs, secs64); |
638 | | |
639 | | /* |
640 | | * On Windows, localtime() (and our MT_safe_localtime() too) |
641 | | * returns a NULL pointer for time before midnight January 1, |
642 | | * 1970 GMT. In that case, we just use the GMT offset for |
643 | | * Jan 2, 1970 and assume that DST was not in effect. |
644 | | */ |
645 | |
|
646 | 0 | if (MT_safe_localtime(&secs, &localTime) == NULL) { |
647 | 0 | retVal.tp_gmt_offset = offset2Jan1970; |
648 | 0 | retVal.tp_dst_offset = 0; |
649 | 0 | return retVal; |
650 | 0 | } |
651 | | |
652 | | /* |
653 | | * dayOffset is the offset between local time and GMT in |
654 | | * the day component, which can only be -1, 0, or 1. We |
655 | | * use the day of the week to compute dayOffset. |
656 | | */ |
657 | | |
658 | 0 | dayOffset = (PRInt32)localTime.tm_wday - gmt->tm_wday; |
659 | | |
660 | | /* |
661 | | * Need to adjust for wrapping around of day of the week from |
662 | | * 6 back to 0. |
663 | | */ |
664 | |
|
665 | 0 | if (dayOffset == -6) { |
666 | | /* Local time is Sunday (0) and GMT is Saturday (6) */ |
667 | 0 | dayOffset = 1; |
668 | 0 | } else if (dayOffset == 6) { |
669 | | /* Local time is Saturday (6) and GMT is Sunday (0) */ |
670 | 0 | dayOffset = -1; |
671 | 0 | } |
672 | |
|
673 | 0 | offsetNew = (PRInt32)localTime.tm_sec - gmt->tm_sec + |
674 | 0 | 60L * ((PRInt32)localTime.tm_min - gmt->tm_min) + |
675 | 0 | 3600L * ((PRInt32)localTime.tm_hour - gmt->tm_hour) + |
676 | 0 | 86400L * (PRInt32)dayOffset; |
677 | |
|
678 | 0 | if (localTime.tm_isdst <= 0) { |
679 | | /* DST is not in effect */ |
680 | 0 | retVal.tp_gmt_offset = offsetNew; |
681 | 0 | retVal.tp_dst_offset = 0; |
682 | 0 | } else { |
683 | | /* DST is in effect */ |
684 | 0 | if (isdst2Jan1970 <= 0) { |
685 | | /* |
686 | | * DST was not in effect back in 2 Jan. 1970. |
687 | | * Use the offset back then as the GMT offset, |
688 | | * assuming the time zone has not changed since then. |
689 | | */ |
690 | 0 | retVal.tp_gmt_offset = offset2Jan1970; |
691 | 0 | retVal.tp_dst_offset = offsetNew - offset2Jan1970; |
692 | 0 | } else { |
693 | | /* |
694 | | * DST was also in effect back in 2 Jan. 1970. |
695 | | * Then our clever trick (or rather, ugly hack) fails. |
696 | | * We will just assume DST offset is an hour. |
697 | | */ |
698 | 0 | retVal.tp_gmt_offset = offsetNew - 3600; |
699 | 0 | retVal.tp_dst_offset = 3600; |
700 | 0 | } |
701 | 0 | } |
702 | |
|
703 | 0 | return retVal; |
704 | 0 | } |
705 | | |
706 | | #endif /* defined(XP_UNIX) || defined(XP_PC) */ |
707 | | |
708 | | /* |
709 | | *------------------------------------------------------------------------ |
710 | | * |
711 | | * PR_USPacificTimeParameters -- |
712 | | * |
713 | | * The time parameters function for the US Pacific Time Zone. |
714 | | * |
715 | | *------------------------------------------------------------------------ |
716 | | */ |
717 | | |
718 | | /* |
719 | | * Returns the mday of the first sunday of the month, where |
720 | | * mday and wday are for a given day in the month. |
721 | | * mdays start with 1 (e.g. 1..31). |
722 | | * wdays start with 0 and are in the range 0..6. 0 = Sunday. |
723 | | */ |
724 | 0 | #define firstSunday(mday, wday) (((mday - wday + 7 - 1) % 7) + 1) |
725 | | |
726 | | /* |
727 | | * Returns the mday for the N'th Sunday of the month, where |
728 | | * mday and wday are for a given day in the month. |
729 | | * mdays start with 1 (e.g. 1..31). |
730 | | * wdays start with 0 and are in the range 0..6. 0 = Sunday. |
731 | | * N has the following values: 0 = first, 1 = second (etc), -1 = last. |
732 | | * ndays is the number of days in that month, the same value as the |
733 | | * mday of the last day of the month. |
734 | | */ |
735 | 0 | static PRInt32 NthSunday(PRInt32 mday, PRInt32 wday, PRInt32 N, PRInt32 ndays) { |
736 | 0 | PRInt32 firstSun = firstSunday(mday, wday); |
737 | |
|
738 | 0 | if (N < 0) { |
739 | 0 | N = (ndays - firstSun) / 7; |
740 | 0 | } |
741 | 0 | return firstSun + (7 * N); |
742 | 0 | } |
743 | | |
744 | | typedef struct DSTParams { |
745 | | PRInt8 dst_start_month; /* 0 = January */ |
746 | | PRInt8 dst_start_Nth_Sunday; /* N as defined above */ |
747 | | PRInt8 dst_start_month_ndays; /* ndays as defined above */ |
748 | | PRInt8 dst_end_month; /* 0 = January */ |
749 | | PRInt8 dst_end_Nth_Sunday; /* N as defined above */ |
750 | | PRInt8 dst_end_month_ndays; /* ndays as defined above */ |
751 | | } DSTParams; |
752 | | |
753 | | static const DSTParams dstParams[2] = { |
754 | | /* year < 2007: First April Sunday - Last October Sunday */ |
755 | | {3, 0, 30, 9, -1, 31}, |
756 | | /* year >= 2007: Second March Sunday - First November Sunday */ |
757 | | {2, 1, 31, 10, 0, 30}}; |
758 | | |
759 | | PR_IMPLEMENT(PRTimeParameters) |
760 | 0 | PR_USPacificTimeParameters(const PRExplodedTime* gmt) { |
761 | 0 | const DSTParams* dst; |
762 | 0 | PRTimeParameters retVal; |
763 | 0 | PRExplodedTime st; |
764 | | |
765 | | /* |
766 | | * Based on geographic location and GMT, figure out offset of |
767 | | * standard time from GMT. In this example implementation, we |
768 | | * assume the local time zone is US Pacific Time. |
769 | | */ |
770 | |
|
771 | 0 | retVal.tp_gmt_offset = -8L * 3600L; |
772 | | |
773 | | /* |
774 | | * Make a copy of GMT. Note that the tm_params field of this copy |
775 | | * is ignored. |
776 | | */ |
777 | |
|
778 | 0 | st.tm_usec = gmt->tm_usec; |
779 | 0 | st.tm_sec = gmt->tm_sec; |
780 | 0 | st.tm_min = gmt->tm_min; |
781 | 0 | st.tm_hour = gmt->tm_hour; |
782 | 0 | st.tm_mday = gmt->tm_mday; |
783 | 0 | st.tm_month = gmt->tm_month; |
784 | 0 | st.tm_year = gmt->tm_year; |
785 | 0 | st.tm_wday = gmt->tm_wday; |
786 | 0 | st.tm_yday = gmt->tm_yday; |
787 | | |
788 | | /* Apply the offset to GMT to obtain the local standard time */ |
789 | 0 | ApplySecOffset(&st, retVal.tp_gmt_offset); |
790 | |
|
791 | 0 | if (st.tm_year < 2007) { /* first April Sunday - Last October Sunday */ |
792 | 0 | dst = &dstParams[0]; |
793 | 0 | } else { /* Second March Sunday - First November Sunday */ |
794 | 0 | dst = &dstParams[1]; |
795 | 0 | } |
796 | | |
797 | | /* |
798 | | * Apply the rules on standard time or GMT to obtain daylight saving |
799 | | * time offset. In this implementation, we use the US DST rule. |
800 | | */ |
801 | 0 | if (st.tm_month < dst->dst_start_month) { |
802 | 0 | retVal.tp_dst_offset = 0L; |
803 | 0 | } else if (st.tm_month == dst->dst_start_month) { |
804 | 0 | int NthSun = NthSunday(st.tm_mday, st.tm_wday, dst->dst_start_Nth_Sunday, |
805 | 0 | dst->dst_start_month_ndays); |
806 | 0 | if (st.tm_mday < NthSun) { /* Before starting Sunday */ |
807 | 0 | retVal.tp_dst_offset = 0L; |
808 | 0 | } else if (st.tm_mday == NthSun) { /* Starting Sunday */ |
809 | | /* 01:59:59 PST -> 03:00:00 PDT */ |
810 | 0 | if (st.tm_hour < 2) { |
811 | 0 | retVal.tp_dst_offset = 0L; |
812 | 0 | } else { |
813 | 0 | retVal.tp_dst_offset = 3600L; |
814 | 0 | } |
815 | 0 | } else { /* After starting Sunday */ |
816 | 0 | retVal.tp_dst_offset = 3600L; |
817 | 0 | } |
818 | 0 | } else if (st.tm_month < dst->dst_end_month) { |
819 | 0 | retVal.tp_dst_offset = 3600L; |
820 | 0 | } else if (st.tm_month == dst->dst_end_month) { |
821 | 0 | int NthSun = NthSunday(st.tm_mday, st.tm_wday, dst->dst_end_Nth_Sunday, |
822 | 0 | dst->dst_end_month_ndays); |
823 | 0 | if (st.tm_mday < NthSun) { /* Before ending Sunday */ |
824 | 0 | retVal.tp_dst_offset = 3600L; |
825 | 0 | } else if (st.tm_mday == NthSun) { /* Ending Sunday */ |
826 | | /* 01:59:59 PDT -> 01:00:00 PST */ |
827 | 0 | if (st.tm_hour < 1) { |
828 | 0 | retVal.tp_dst_offset = 3600L; |
829 | 0 | } else { |
830 | 0 | retVal.tp_dst_offset = 0L; |
831 | 0 | } |
832 | 0 | } else { /* After ending Sunday */ |
833 | 0 | retVal.tp_dst_offset = 0L; |
834 | 0 | } |
835 | 0 | } else { |
836 | 0 | retVal.tp_dst_offset = 0L; |
837 | 0 | } |
838 | 0 | return retVal; |
839 | 0 | } |
840 | | |
841 | | /* |
842 | | *------------------------------------------------------------------------ |
843 | | * |
844 | | * PR_GMTParameters -- |
845 | | * |
846 | | * Returns the PRTimeParameters for Greenwich Mean Time. |
847 | | * Trivially, both the tp_gmt_offset and tp_dst_offset fields are 0. |
848 | | * |
849 | | *------------------------------------------------------------------------ |
850 | | */ |
851 | | |
852 | | PR_IMPLEMENT(PRTimeParameters) |
853 | 21.4k | PR_GMTParameters(const PRExplodedTime* gmt) { |
854 | 21.4k | PRTimeParameters retVal = {0, 0}; |
855 | 21.4k | return retVal; |
856 | 21.4k | } |
857 | | |
858 | | /* |
859 | | * The following code implements PR_ParseTimeString(). It is based on |
860 | | * ns/lib/xp/xp_time.c, revision 1.25, by Jamie Zawinski <jwz@netscape.com>. |
861 | | */ |
862 | | |
863 | | /* |
864 | | * We only recognize the abbreviations of a small subset of time zones |
865 | | * in North America, Europe, and Japan. |
866 | | * |
867 | | * PST/PDT: Pacific Standard/Daylight Time |
868 | | * MST/MDT: Mountain Standard/Daylight Time |
869 | | * CST/CDT: Central Standard/Daylight Time |
870 | | * EST/EDT: Eastern Standard/Daylight Time |
871 | | * AST: Atlantic Standard Time |
872 | | * NST: Newfoundland Standard Time |
873 | | * GMT: Greenwich Mean Time |
874 | | * BST: British Summer Time |
875 | | * MET: Middle Europe Time |
876 | | * EET: Eastern Europe Time |
877 | | * JST: Japan Standard Time |
878 | | */ |
879 | | |
880 | | typedef enum { |
881 | | TT_UNKNOWN, |
882 | | |
883 | | TT_SUN, |
884 | | TT_MON, |
885 | | TT_TUE, |
886 | | TT_WED, |
887 | | TT_THU, |
888 | | TT_FRI, |
889 | | TT_SAT, |
890 | | |
891 | | TT_JAN, |
892 | | TT_FEB, |
893 | | TT_MAR, |
894 | | TT_APR, |
895 | | TT_MAY, |
896 | | TT_JUN, |
897 | | TT_JUL, |
898 | | TT_AUG, |
899 | | TT_SEP, |
900 | | TT_OCT, |
901 | | TT_NOV, |
902 | | TT_DEC, |
903 | | |
904 | | TT_PST, |
905 | | TT_PDT, |
906 | | TT_MST, |
907 | | TT_MDT, |
908 | | TT_CST, |
909 | | TT_CDT, |
910 | | TT_EST, |
911 | | TT_EDT, |
912 | | TT_AST, |
913 | | TT_NST, |
914 | | TT_GMT, |
915 | | TT_BST, |
916 | | TT_MET, |
917 | | TT_EET, |
918 | | TT_JST |
919 | | } TIME_TOKEN; |
920 | | |
921 | | /* |
922 | | * This parses a time/date string into a PRTime |
923 | | * (microseconds after "1-Jan-1970 00:00:00 GMT"). |
924 | | * It returns PR_SUCCESS on success, and PR_FAILURE |
925 | | * if the time/date string can't be parsed. |
926 | | * |
927 | | * Many formats are handled, including: |
928 | | * |
929 | | * 14 Apr 89 03:20:12 |
930 | | * 14 Apr 89 03:20 GMT |
931 | | * Fri, 17 Mar 89 4:01:33 |
932 | | * Fri, 17 Mar 89 4:01 GMT |
933 | | * Mon Jan 16 16:12 PDT 1989 |
934 | | * Mon Jan 16 16:12 +0130 1989 |
935 | | * 6 May 1992 16:41-JST (Wednesday) |
936 | | * 22-AUG-1993 10:59:12.82 |
937 | | * 22-AUG-1993 10:59pm |
938 | | * 22-AUG-1993 12:59am |
939 | | * 22-AUG-1993 12:59 PM |
940 | | * Friday, August 04, 1995 3:54 PM |
941 | | * 06/21/95 04:24:34 PM |
942 | | * 20/06/95 21:07 |
943 | | * 95-06-08 19:32:48 EDT |
944 | | * |
945 | | * If the input string doesn't contain a description of the timezone, |
946 | | * we consult the `default_to_gmt' to decide whether the string should |
947 | | * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE). |
948 | | * The correct value for this argument depends on what standard specified |
949 | | * the time string which you are parsing. |
950 | | */ |
951 | | |
952 | | PR_IMPLEMENT(PRStatus) |
953 | | PR_ParseTimeStringToExplodedTime(const char* string, PRBool default_to_gmt, |
954 | 0 | PRExplodedTime* result) { |
955 | 0 | TIME_TOKEN dotw = TT_UNKNOWN; |
956 | 0 | TIME_TOKEN month = TT_UNKNOWN; |
957 | 0 | TIME_TOKEN zone = TT_UNKNOWN; |
958 | 0 | int zone_offset = -1; |
959 | 0 | int dst_offset = 0; |
960 | 0 | int date = -1; |
961 | 0 | PRInt32 year = -1; |
962 | 0 | int hour = -1; |
963 | 0 | int min = -1; |
964 | 0 | int sec = -1; |
965 | 0 | struct tm* localTimeResult; |
966 | |
|
967 | 0 | const char* rest = string; |
968 | |
|
969 | 0 | int iterations = 0; |
970 | |
|
971 | 0 | PR_ASSERT(string && result); |
972 | 0 | if (!string || !result) { |
973 | 0 | return PR_FAILURE; |
974 | 0 | } |
975 | | |
976 | 0 | while (*rest) { |
977 | 0 | if (iterations++ > 1000) { |
978 | 0 | return PR_FAILURE; |
979 | 0 | } |
980 | | |
981 | 0 | switch (*rest) { |
982 | 0 | case 'a': |
983 | 0 | case 'A': |
984 | 0 | if (month == TT_UNKNOWN && (rest[1] == 'p' || rest[1] == 'P') && |
985 | 0 | (rest[2] == 'r' || rest[2] == 'R')) { |
986 | 0 | month = TT_APR; |
987 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 's' || rest[1] == 'S') && |
988 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
989 | 0 | zone = TT_AST; |
990 | 0 | } else if (month == TT_UNKNOWN && (rest[1] == 'u' || rest[1] == 'U') && |
991 | 0 | (rest[2] == 'g' || rest[2] == 'G')) { |
992 | 0 | month = TT_AUG; |
993 | 0 | } |
994 | 0 | break; |
995 | 0 | case 'b': |
996 | 0 | case 'B': |
997 | 0 | if (zone == TT_UNKNOWN && (rest[1] == 's' || rest[1] == 'S') && |
998 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
999 | 0 | zone = TT_BST; |
1000 | 0 | } |
1001 | 0 | break; |
1002 | 0 | case 'c': |
1003 | 0 | case 'C': |
1004 | 0 | if (zone == TT_UNKNOWN && (rest[1] == 'd' || rest[1] == 'D') && |
1005 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1006 | 0 | zone = TT_CDT; |
1007 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 's' || rest[1] == 'S') && |
1008 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1009 | 0 | zone = TT_CST; |
1010 | 0 | } |
1011 | 0 | break; |
1012 | 0 | case 'd': |
1013 | 0 | case 'D': |
1014 | 0 | if (month == TT_UNKNOWN && (rest[1] == 'e' || rest[1] == 'E') && |
1015 | 0 | (rest[2] == 'c' || rest[2] == 'C')) { |
1016 | 0 | month = TT_DEC; |
1017 | 0 | } |
1018 | 0 | break; |
1019 | 0 | case 'e': |
1020 | 0 | case 'E': |
1021 | 0 | if (zone == TT_UNKNOWN && (rest[1] == 'd' || rest[1] == 'D') && |
1022 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1023 | 0 | zone = TT_EDT; |
1024 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 'e' || rest[1] == 'E') && |
1025 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1026 | 0 | zone = TT_EET; |
1027 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 's' || rest[1] == 'S') && |
1028 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1029 | 0 | zone = TT_EST; |
1030 | 0 | } |
1031 | 0 | break; |
1032 | 0 | case 'f': |
1033 | 0 | case 'F': |
1034 | 0 | if (month == TT_UNKNOWN && (rest[1] == 'e' || rest[1] == 'E') && |
1035 | 0 | (rest[2] == 'b' || rest[2] == 'B')) { |
1036 | 0 | month = TT_FEB; |
1037 | 0 | } else if (dotw == TT_UNKNOWN && (rest[1] == 'r' || rest[1] == 'R') && |
1038 | 0 | (rest[2] == 'i' || rest[2] == 'I')) { |
1039 | 0 | dotw = TT_FRI; |
1040 | 0 | } |
1041 | 0 | break; |
1042 | 0 | case 'g': |
1043 | 0 | case 'G': |
1044 | 0 | if (zone == TT_UNKNOWN && (rest[1] == 'm' || rest[1] == 'M') && |
1045 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1046 | 0 | zone = TT_GMT; |
1047 | 0 | } |
1048 | 0 | break; |
1049 | 0 | case 'j': |
1050 | 0 | case 'J': |
1051 | 0 | if (month == TT_UNKNOWN && (rest[1] == 'a' || rest[1] == 'A') && |
1052 | 0 | (rest[2] == 'n' || rest[2] == 'N')) { |
1053 | 0 | month = TT_JAN; |
1054 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 's' || rest[1] == 'S') && |
1055 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1056 | 0 | zone = TT_JST; |
1057 | 0 | } else if (month == TT_UNKNOWN && (rest[1] == 'u' || rest[1] == 'U') && |
1058 | 0 | (rest[2] == 'l' || rest[2] == 'L')) { |
1059 | 0 | month = TT_JUL; |
1060 | 0 | } else if (month == TT_UNKNOWN && (rest[1] == 'u' || rest[1] == 'U') && |
1061 | 0 | (rest[2] == 'n' || rest[2] == 'N')) { |
1062 | 0 | month = TT_JUN; |
1063 | 0 | } |
1064 | 0 | break; |
1065 | 0 | case 'm': |
1066 | 0 | case 'M': |
1067 | 0 | if (month == TT_UNKNOWN && (rest[1] == 'a' || rest[1] == 'A') && |
1068 | 0 | (rest[2] == 'r' || rest[2] == 'R')) { |
1069 | 0 | month = TT_MAR; |
1070 | 0 | } else if (month == TT_UNKNOWN && (rest[1] == 'a' || rest[1] == 'A') && |
1071 | 0 | (rest[2] == 'y' || rest[2] == 'Y')) { |
1072 | 0 | month = TT_MAY; |
1073 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 'd' || rest[1] == 'D') && |
1074 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1075 | 0 | zone = TT_MDT; |
1076 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 'e' || rest[1] == 'E') && |
1077 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1078 | 0 | zone = TT_MET; |
1079 | 0 | } else if (dotw == TT_UNKNOWN && (rest[1] == 'o' || rest[1] == 'O') && |
1080 | 0 | (rest[2] == 'n' || rest[2] == 'N')) { |
1081 | 0 | dotw = TT_MON; |
1082 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 's' || rest[1] == 'S') && |
1083 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1084 | 0 | zone = TT_MST; |
1085 | 0 | } |
1086 | 0 | break; |
1087 | 0 | case 'n': |
1088 | 0 | case 'N': |
1089 | 0 | if (month == TT_UNKNOWN && (rest[1] == 'o' || rest[1] == 'O') && |
1090 | 0 | (rest[2] == 'v' || rest[2] == 'V')) { |
1091 | 0 | month = TT_NOV; |
1092 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 's' || rest[1] == 'S') && |
1093 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1094 | 0 | zone = TT_NST; |
1095 | 0 | } |
1096 | 0 | break; |
1097 | 0 | case 'o': |
1098 | 0 | case 'O': |
1099 | 0 | if (month == TT_UNKNOWN && (rest[1] == 'c' || rest[1] == 'C') && |
1100 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1101 | 0 | month = TT_OCT; |
1102 | 0 | } |
1103 | 0 | break; |
1104 | 0 | case 'p': |
1105 | 0 | case 'P': |
1106 | 0 | if (zone == TT_UNKNOWN && (rest[1] == 'd' || rest[1] == 'D') && |
1107 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1108 | 0 | zone = TT_PDT; |
1109 | 0 | } else if (zone == TT_UNKNOWN && (rest[1] == 's' || rest[1] == 'S') && |
1110 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1111 | 0 | zone = TT_PST; |
1112 | 0 | } |
1113 | 0 | break; |
1114 | 0 | case 's': |
1115 | 0 | case 'S': |
1116 | 0 | if (dotw == TT_UNKNOWN && (rest[1] == 'a' || rest[1] == 'A') && |
1117 | 0 | (rest[2] == 't' || rest[2] == 'T')) { |
1118 | 0 | dotw = TT_SAT; |
1119 | 0 | } else if (month == TT_UNKNOWN && (rest[1] == 'e' || rest[1] == 'E') && |
1120 | 0 | (rest[2] == 'p' || rest[2] == 'P')) { |
1121 | 0 | month = TT_SEP; |
1122 | 0 | } else if (dotw == TT_UNKNOWN && (rest[1] == 'u' || rest[1] == 'U') && |
1123 | 0 | (rest[2] == 'n' || rest[2] == 'N')) { |
1124 | 0 | dotw = TT_SUN; |
1125 | 0 | } |
1126 | 0 | break; |
1127 | 0 | case 't': |
1128 | 0 | case 'T': |
1129 | 0 | if (dotw == TT_UNKNOWN && (rest[1] == 'h' || rest[1] == 'H') && |
1130 | 0 | (rest[2] == 'u' || rest[2] == 'U')) { |
1131 | 0 | dotw = TT_THU; |
1132 | 0 | } else if (dotw == TT_UNKNOWN && (rest[1] == 'u' || rest[1] == 'U') && |
1133 | 0 | (rest[2] == 'e' || rest[2] == 'E')) { |
1134 | 0 | dotw = TT_TUE; |
1135 | 0 | } |
1136 | 0 | break; |
1137 | 0 | case 'u': |
1138 | 0 | case 'U': |
1139 | 0 | if (zone == TT_UNKNOWN && (rest[1] == 't' || rest[1] == 'T') && |
1140 | 0 | !(rest[2] >= 'A' && rest[2] <= 'Z') && |
1141 | 0 | !(rest[2] >= 'a' && rest[2] <= 'z')) |
1142 | | /* UT is the same as GMT but UTx is not. */ |
1143 | 0 | { |
1144 | 0 | zone = TT_GMT; |
1145 | 0 | } |
1146 | 0 | break; |
1147 | 0 | case 'w': |
1148 | 0 | case 'W': |
1149 | 0 | if (dotw == TT_UNKNOWN && (rest[1] == 'e' || rest[1] == 'E') && |
1150 | 0 | (rest[2] == 'd' || rest[2] == 'D')) { |
1151 | 0 | dotw = TT_WED; |
1152 | 0 | } |
1153 | 0 | break; |
1154 | | |
1155 | 0 | case '+': |
1156 | 0 | case '-': { |
1157 | 0 | const char* end; |
1158 | 0 | int sign; |
1159 | 0 | if (zone_offset != -1) { |
1160 | | /* already got one... */ |
1161 | 0 | rest++; |
1162 | 0 | break; |
1163 | 0 | } |
1164 | 0 | if (zone != TT_UNKNOWN && zone != TT_GMT) { |
1165 | | /* GMT+0300 is legal, but PST+0300 is not. */ |
1166 | 0 | rest++; |
1167 | 0 | break; |
1168 | 0 | } |
1169 | | |
1170 | 0 | sign = ((*rest == '+') ? 1 : -1); |
1171 | 0 | rest++; /* move over sign */ |
1172 | 0 | end = rest; |
1173 | 0 | while (*end >= '0' && *end <= '9') { |
1174 | 0 | end++; |
1175 | 0 | } |
1176 | 0 | if (rest == end) { /* no digits here */ |
1177 | 0 | break; |
1178 | 0 | } |
1179 | | |
1180 | 0 | if ((end - rest) == 4) /* offset in HHMM */ |
1181 | 0 | zone_offset = (((((rest[0] - '0') * 10) + (rest[1] - '0')) * 60) + |
1182 | 0 | (((rest[2] - '0') * 10) + (rest[3] - '0'))); |
1183 | 0 | else if ((end - rest) == 2) |
1184 | | /* offset in hours */ |
1185 | 0 | { |
1186 | 0 | zone_offset = (((rest[0] - '0') * 10) + (rest[1] - '0')) * 60; |
1187 | 0 | } else if ((end - rest) == 1) |
1188 | | /* offset in hours */ |
1189 | 0 | { |
1190 | 0 | zone_offset = (rest[0] - '0') * 60; |
1191 | 0 | } else |
1192 | | /* 3 or >4 */ |
1193 | 0 | { |
1194 | 0 | break; |
1195 | 0 | } |
1196 | | |
1197 | 0 | zone_offset *= sign; |
1198 | 0 | zone = TT_GMT; |
1199 | 0 | break; |
1200 | 0 | } |
1201 | | |
1202 | 0 | case '0': |
1203 | 0 | case '1': |
1204 | 0 | case '2': |
1205 | 0 | case '3': |
1206 | 0 | case '4': |
1207 | 0 | case '5': |
1208 | 0 | case '6': |
1209 | 0 | case '7': |
1210 | 0 | case '8': |
1211 | 0 | case '9': { |
1212 | 0 | int tmp_hour = -1; |
1213 | 0 | int tmp_min = -1; |
1214 | 0 | int tmp_sec = -1; |
1215 | 0 | const char* end = rest + 1; |
1216 | 0 | while (*end >= '0' && *end <= '9') { |
1217 | 0 | end++; |
1218 | 0 | } |
1219 | | |
1220 | | /* end is now the first character after a range of digits. */ |
1221 | |
|
1222 | 0 | if (*end == ':') { |
1223 | 0 | if (hour >= 0 && min >= 0) { /* already got it */ |
1224 | 0 | break; |
1225 | 0 | } |
1226 | | |
1227 | | /* We have seen "[0-9]+:", so this is probably HH:MM[:SS] */ |
1228 | 0 | if ((end - rest) > 2) |
1229 | | /* it is [0-9][0-9][0-9]+: */ |
1230 | 0 | { |
1231 | 0 | break; |
1232 | 0 | } |
1233 | 0 | if ((end - rest) == 2) |
1234 | 0 | tmp_hour = ((rest[0] - '0') * 10 + (rest[1] - '0')); |
1235 | 0 | else { |
1236 | 0 | tmp_hour = (rest[0] - '0'); |
1237 | 0 | } |
1238 | | |
1239 | | /* move over the colon, and parse minutes */ |
1240 | |
|
1241 | 0 | rest = ++end; |
1242 | 0 | while (*end >= '0' && *end <= '9') { |
1243 | 0 | end++; |
1244 | 0 | } |
1245 | |
|
1246 | 0 | if (end == rest) |
1247 | | /* no digits after first colon? */ |
1248 | 0 | { |
1249 | 0 | break; |
1250 | 0 | } |
1251 | 0 | if ((end - rest) > 2) |
1252 | | /* it is [0-9][0-9][0-9]+: */ |
1253 | 0 | { |
1254 | 0 | break; |
1255 | 0 | } |
1256 | 0 | if ((end - rest) == 2) |
1257 | 0 | tmp_min = ((rest[0] - '0') * 10 + (rest[1] - '0')); |
1258 | 0 | else { |
1259 | 0 | tmp_min = (rest[0] - '0'); |
1260 | 0 | } |
1261 | | |
1262 | | /* now go for seconds */ |
1263 | 0 | rest = end; |
1264 | 0 | if (*rest == ':') { |
1265 | 0 | rest++; |
1266 | 0 | } |
1267 | 0 | end = rest; |
1268 | 0 | while (*end >= '0' && *end <= '9') { |
1269 | 0 | end++; |
1270 | 0 | } |
1271 | |
|
1272 | 0 | if (end == rest) /* no digits after second colon - that's ok. */ |
1273 | 0 | ; |
1274 | 0 | else if ((end - rest) > 2) |
1275 | | /* it is [0-9][0-9][0-9]+: */ |
1276 | 0 | { |
1277 | 0 | break; |
1278 | 0 | } |
1279 | 0 | else if ((end - rest) == 2) |
1280 | 0 | tmp_sec = ((rest[0] - '0') * 10 + (rest[1] - '0')); |
1281 | 0 | else { |
1282 | 0 | tmp_sec = (rest[0] - '0'); |
1283 | 0 | } |
1284 | | |
1285 | | /* If we made it here, we've parsed hour and min, |
1286 | | and possibly sec, so it worked as a unit. */ |
1287 | | |
1288 | | /* skip over whitespace and see if there's an AM or PM |
1289 | | directly following the time. |
1290 | | */ |
1291 | 0 | if (tmp_hour <= 12) { |
1292 | 0 | const char* s = end; |
1293 | 0 | while (*s && (*s == ' ' || *s == '\t')) { |
1294 | 0 | s++; |
1295 | 0 | } |
1296 | 0 | if ((s[0] == 'p' || s[0] == 'P') && (s[1] == 'm' || s[1] == 'M')) |
1297 | | /* 10:05pm == 22:05, and 12:05pm == 12:05 */ |
1298 | 0 | { |
1299 | 0 | tmp_hour = (tmp_hour == 12 ? 12 : tmp_hour + 12); |
1300 | 0 | } else if (tmp_hour == 12 && (s[0] == 'a' || s[0] == 'A') && |
1301 | 0 | (s[1] == 'm' || s[1] == 'M')) |
1302 | | /* 12:05am == 00:05 */ |
1303 | 0 | { |
1304 | 0 | tmp_hour = 0; |
1305 | 0 | } |
1306 | 0 | } |
1307 | |
|
1308 | 0 | hour = tmp_hour; |
1309 | 0 | min = tmp_min; |
1310 | 0 | sec = tmp_sec; |
1311 | 0 | rest = end; |
1312 | 0 | break; |
1313 | 0 | } |
1314 | 0 | if ((*end == '/' || *end == '-') && end[1] >= '0' && end[1] <= '9') { |
1315 | | /* Perhaps this is 6/16/95, 16/6/95, 6-16-95, or 16-6-95 |
1316 | | or even 95-06-05... |
1317 | | #### But it doesn't handle 1995-06-22. |
1318 | | */ |
1319 | 0 | int n1, n2, n3; |
1320 | 0 | const char* s; |
1321 | |
|
1322 | 0 | if (month != TT_UNKNOWN) |
1323 | | /* if we saw a month name, this can't be. */ |
1324 | 0 | { |
1325 | 0 | break; |
1326 | 0 | } |
1327 | | |
1328 | 0 | s = rest; |
1329 | |
|
1330 | 0 | n1 = (*s++ - '0'); /* first 1 or 2 digits */ |
1331 | 0 | if (*s >= '0' && *s <= '9') { |
1332 | 0 | n1 = n1 * 10 + (*s++ - '0'); |
1333 | 0 | } |
1334 | |
|
1335 | 0 | if (*s != '/' && *s != '-') { /* slash */ |
1336 | 0 | break; |
1337 | 0 | } |
1338 | 0 | s++; |
1339 | |
|
1340 | 0 | if (*s < '0' || *s > '9') { /* second 1 or 2 digits */ |
1341 | 0 | break; |
1342 | 0 | } |
1343 | 0 | n2 = (*s++ - '0'); |
1344 | 0 | if (*s >= '0' && *s <= '9') { |
1345 | 0 | n2 = n2 * 10 + (*s++ - '0'); |
1346 | 0 | } |
1347 | |
|
1348 | 0 | if (*s != '/' && *s != '-') { /* slash */ |
1349 | 0 | break; |
1350 | 0 | } |
1351 | 0 | s++; |
1352 | |
|
1353 | 0 | if (*s < '0' || *s > '9') { /* third 1, 2, 4, or 5 digits */ |
1354 | 0 | break; |
1355 | 0 | } |
1356 | 0 | n3 = (*s++ - '0'); |
1357 | 0 | if (*s >= '0' && *s <= '9') { |
1358 | 0 | n3 = n3 * 10 + (*s++ - '0'); |
1359 | 0 | } |
1360 | |
|
1361 | 0 | if (*s >= '0' && *s <= '9') /* optional digits 3, 4, and 5 */ |
1362 | 0 | { |
1363 | 0 | n3 = n3 * 10 + (*s++ - '0'); |
1364 | 0 | if (*s < '0' || *s > '9') { |
1365 | 0 | break; |
1366 | 0 | } |
1367 | 0 | n3 = n3 * 10 + (*s++ - '0'); |
1368 | 0 | if (*s >= '0' && *s <= '9') { |
1369 | 0 | n3 = n3 * 10 + (*s++ - '0'); |
1370 | 0 | } |
1371 | 0 | } |
1372 | | |
1373 | 0 | if ((*s >= '0' && *s <= '9') || /* followed by non-alphanum */ |
1374 | 0 | (*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z')) { |
1375 | 0 | break; |
1376 | 0 | } |
1377 | | |
1378 | | /* Ok, we parsed three 1-2 digit numbers, with / or - |
1379 | | between them. Now decide what the hell they are |
1380 | | (DD/MM/YY or MM/DD/YY or YY/MM/DD.) |
1381 | | */ |
1382 | | |
1383 | 0 | if (n1 > 31 || n1 == 0) /* must be YY/MM/DD */ |
1384 | 0 | { |
1385 | 0 | if (n2 > 12) { |
1386 | 0 | break; |
1387 | 0 | } |
1388 | 0 | if (n3 > 31) { |
1389 | 0 | break; |
1390 | 0 | } |
1391 | 0 | year = n1; |
1392 | 0 | if (year < 70) { |
1393 | 0 | year += 2000; |
1394 | 0 | } else if (year < 100) { |
1395 | 0 | year += 1900; |
1396 | 0 | } |
1397 | 0 | month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1); |
1398 | 0 | date = n3; |
1399 | 0 | rest = s; |
1400 | 0 | break; |
1401 | 0 | } |
1402 | | |
1403 | 0 | if (n1 > 12 && n2 > 12) /* illegal */ |
1404 | 0 | { |
1405 | 0 | rest = s; |
1406 | 0 | break; |
1407 | 0 | } |
1408 | | |
1409 | 0 | if (n3 < 70) { |
1410 | 0 | n3 += 2000; |
1411 | 0 | } else if (n3 < 100) { |
1412 | 0 | n3 += 1900; |
1413 | 0 | } |
1414 | |
|
1415 | 0 | if (n1 > 12) /* must be DD/MM/YY */ |
1416 | 0 | { |
1417 | 0 | date = n1; |
1418 | 0 | month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1); |
1419 | 0 | year = n3; |
1420 | 0 | } else /* assume MM/DD/YY */ |
1421 | 0 | { |
1422 | | /* #### In the ambiguous case, should we consult the |
1423 | | locale to find out the local default? */ |
1424 | 0 | month = (TIME_TOKEN)(n1 + ((int)TT_JAN) - 1); |
1425 | 0 | date = n2; |
1426 | 0 | year = n3; |
1427 | 0 | } |
1428 | 0 | rest = s; |
1429 | 0 | } else if ((*end >= 'A' && *end <= 'Z') || (*end >= 'a' && *end <= 'z')) |
1430 | | /* Digits followed by non-punctuation - what's that? */ |
1431 | 0 | ; |
1432 | 0 | else if ((end - rest) == 5) /* five digits is a year */ |
1433 | 0 | year = (year < 0 ? ((rest[0] - '0') * 10000L + |
1434 | 0 | (rest[1] - '0') * 1000L + (rest[2] - '0') * 100L + |
1435 | 0 | (rest[3] - '0') * 10L + (rest[4] - '0')) |
1436 | 0 | : year); |
1437 | 0 | else if ((end - rest) == 4) /* four digits is a year */ |
1438 | 0 | year = (year < 0 ? ((rest[0] - '0') * 1000L + (rest[1] - '0') * 100L + |
1439 | 0 | (rest[2] - '0') * 10L + (rest[3] - '0')) |
1440 | 0 | : year); |
1441 | 0 | else if ((end - rest) == 2) /* two digits - date or year */ |
1442 | 0 | { |
1443 | 0 | int n = ((rest[0] - '0') * 10 + (rest[1] - '0')); |
1444 | | /* If we don't have a date (day of the month) and we see a number |
1445 | | less than 32, then assume that is the date. |
1446 | | |
1447 | | Otherwise, if we have a date and not a year, assume this is |
1448 | | the year. If it is less than 70, then assume it refers to the 21st |
1449 | | century. If it is two digits (>= 70), assume it refers to |
1450 | | this century. Otherwise, assume it refers to an unambiguous year. |
1451 | | |
1452 | | The world will surely end soon. |
1453 | | */ |
1454 | 0 | if (date < 0 && n < 32) { |
1455 | 0 | date = n; |
1456 | 0 | } else if (year < 0) { |
1457 | 0 | if (n < 70) { |
1458 | 0 | year = 2000 + n; |
1459 | 0 | } else if (n < 100) { |
1460 | 0 | year = 1900 + n; |
1461 | 0 | } else { |
1462 | 0 | year = n; |
1463 | 0 | } |
1464 | 0 | } |
1465 | | /* else what the hell is this. */ |
1466 | 0 | } else if ((end - rest) == 1) { /* one digit - date */ |
1467 | 0 | date = (date < 0 ? (rest[0] - '0') : date); |
1468 | 0 | } |
1469 | | /* else, three or more than five digits - what's that? */ |
1470 | | |
1471 | 0 | break; |
1472 | 0 | } |
1473 | 0 | } |
1474 | | |
1475 | | /* Skip to the end of this token, whether we parsed it or not. |
1476 | | Tokens are delimited by whitespace, or ,;-/ |
1477 | | But explicitly not :+-. |
1478 | | */ |
1479 | 0 | while (*rest && *rest != ' ' && *rest != '\t' && *rest != ',' && |
1480 | 0 | *rest != ';' && *rest != '-' && *rest != '+' && *rest != '/' && |
1481 | 0 | *rest != '(' && *rest != ')' && *rest != '[' && *rest != ']') { |
1482 | 0 | rest++; |
1483 | 0 | } |
1484 | | /* skip over uninteresting chars. */ |
1485 | 0 | SKIP_MORE: |
1486 | 0 | while (*rest && (*rest == ' ' || *rest == '\t' || *rest == ',' || |
1487 | 0 | *rest == ';' || *rest == '/' || *rest == '(' || |
1488 | 0 | *rest == ')' || *rest == '[' || *rest == ']')) { |
1489 | 0 | rest++; |
1490 | 0 | } |
1491 | | |
1492 | | /* "-" is ignored at the beginning of a token if we have not yet |
1493 | | parsed a year (e.g., the second "-" in "30-AUG-1966"), or if |
1494 | | the character after the dash is not a digit. */ |
1495 | 0 | if (*rest == '-' && |
1496 | 0 | ((rest > string && isalpha((unsigned char)rest[-1]) && year < 0) || |
1497 | 0 | rest[1] < '0' || rest[1] > '9')) { |
1498 | 0 | rest++; |
1499 | 0 | goto SKIP_MORE; |
1500 | 0 | } |
1501 | 0 | } |
1502 | | |
1503 | 0 | if (zone != TT_UNKNOWN && zone_offset == -1) { |
1504 | 0 | switch (zone) { |
1505 | 0 | case TT_PST: |
1506 | 0 | zone_offset = -8 * 60; |
1507 | 0 | break; |
1508 | 0 | case TT_PDT: |
1509 | 0 | zone_offset = -8 * 60; |
1510 | 0 | dst_offset = 1 * 60; |
1511 | 0 | break; |
1512 | 0 | case TT_MST: |
1513 | 0 | zone_offset = -7 * 60; |
1514 | 0 | break; |
1515 | 0 | case TT_MDT: |
1516 | 0 | zone_offset = -7 * 60; |
1517 | 0 | dst_offset = 1 * 60; |
1518 | 0 | break; |
1519 | 0 | case TT_CST: |
1520 | 0 | zone_offset = -6 * 60; |
1521 | 0 | break; |
1522 | 0 | case TT_CDT: |
1523 | 0 | zone_offset = -6 * 60; |
1524 | 0 | dst_offset = 1 * 60; |
1525 | 0 | break; |
1526 | 0 | case TT_EST: |
1527 | 0 | zone_offset = -5 * 60; |
1528 | 0 | break; |
1529 | 0 | case TT_EDT: |
1530 | 0 | zone_offset = -5 * 60; |
1531 | 0 | dst_offset = 1 * 60; |
1532 | 0 | break; |
1533 | 0 | case TT_AST: |
1534 | 0 | zone_offset = -4 * 60; |
1535 | 0 | break; |
1536 | 0 | case TT_NST: |
1537 | 0 | zone_offset = -3 * 60 - 30; |
1538 | 0 | break; |
1539 | 0 | case TT_GMT: |
1540 | 0 | zone_offset = 0 * 60; |
1541 | 0 | break; |
1542 | 0 | case TT_BST: |
1543 | 0 | zone_offset = 0 * 60; |
1544 | 0 | dst_offset = 1 * 60; |
1545 | 0 | break; |
1546 | 0 | case TT_MET: |
1547 | 0 | zone_offset = 1 * 60; |
1548 | 0 | break; |
1549 | 0 | case TT_EET: |
1550 | 0 | zone_offset = 2 * 60; |
1551 | 0 | break; |
1552 | 0 | case TT_JST: |
1553 | 0 | zone_offset = 9 * 60; |
1554 | 0 | break; |
1555 | 0 | default: |
1556 | 0 | PR_ASSERT(0); |
1557 | 0 | break; |
1558 | 0 | } |
1559 | 0 | } |
1560 | | |
1561 | | /* If we didn't find a year, month, or day-of-the-month, we can't |
1562 | | possibly parse this, and in fact, mktime() will do something random |
1563 | | (I'm seeing it return "Tue Feb 5 06:28:16 2036", which is no doubt |
1564 | | a numerologically significant date... */ |
1565 | 0 | if (month == TT_UNKNOWN || date == -1 || year == -1 || year > PR_INT16_MAX) { |
1566 | 0 | return PR_FAILURE; |
1567 | 0 | } |
1568 | | |
1569 | 0 | memset(result, 0, sizeof(*result)); |
1570 | 0 | if (sec != -1) { |
1571 | 0 | result->tm_sec = sec; |
1572 | 0 | } |
1573 | 0 | if (min != -1) { |
1574 | 0 | result->tm_min = min; |
1575 | 0 | } |
1576 | 0 | if (hour != -1) { |
1577 | 0 | result->tm_hour = hour; |
1578 | 0 | } |
1579 | 0 | if (date != -1) { |
1580 | 0 | result->tm_mday = date; |
1581 | 0 | } |
1582 | 0 | if (month != TT_UNKNOWN) { |
1583 | 0 | result->tm_month = (((int)month) - ((int)TT_JAN)); |
1584 | 0 | } |
1585 | 0 | if (year != -1) { |
1586 | 0 | result->tm_year = (PRInt16)year; |
1587 | 0 | } |
1588 | 0 | if (dotw != TT_UNKNOWN) { |
1589 | 0 | result->tm_wday = (PRInt8)(((int)dotw) - ((int)TT_SUN)); |
1590 | 0 | } |
1591 | | /* |
1592 | | * Mainly to compute wday and yday, but normalized time is also required |
1593 | | * by the check below that works around a Visual C++ 2005 mktime problem. |
1594 | | */ |
1595 | 0 | PR_NormalizeTime(result, PR_GMTParameters); |
1596 | | /* The remaining work is to set the gmt and dst offsets in tm_params. */ |
1597 | |
|
1598 | 0 | if (zone == TT_UNKNOWN && default_to_gmt) { |
1599 | | /* No zone was specified, so pretend the zone was GMT. */ |
1600 | 0 | zone = TT_GMT; |
1601 | 0 | zone_offset = 0; |
1602 | 0 | } |
1603 | |
|
1604 | 0 | if (zone_offset == -1) { |
1605 | | /* no zone was specified, and we're to assume that everything |
1606 | | is local. */ |
1607 | 0 | struct tm localTime; |
1608 | 0 | time_t secs; |
1609 | |
|
1610 | 0 | PR_ASSERT(result->tm_month > -1 && result->tm_mday > 0 && |
1611 | 0 | result->tm_hour > -1 && result->tm_min > -1 && |
1612 | 0 | result->tm_sec > -1); |
1613 | | |
1614 | | /* |
1615 | | * To obtain time_t from a tm structure representing the local |
1616 | | * time, we call mktime(). However, we need to see if we are |
1617 | | * on 1-Jan-1970 or before. If we are, we can't call mktime() |
1618 | | * because mktime() will crash on win16. In that case, we |
1619 | | * calculate zone_offset based on the zone offset at |
1620 | | * 00:00:00, 2 Jan 1970 GMT, and subtract zone_offset from the |
1621 | | * date we are parsing to transform the date to GMT. We also |
1622 | | * do so if mktime() returns (time_t) -1 (time out of range). |
1623 | | */ |
1624 | | |
1625 | | /* month, day, hours, mins and secs are always non-negative |
1626 | | so we dont need to worry about them. */ |
1627 | 0 | if (result->tm_year >= 1970) { |
1628 | 0 | PRInt64 usec_per_sec; |
1629 | |
|
1630 | 0 | localTime.tm_sec = result->tm_sec; |
1631 | 0 | localTime.tm_min = result->tm_min; |
1632 | 0 | localTime.tm_hour = result->tm_hour; |
1633 | 0 | localTime.tm_mday = result->tm_mday; |
1634 | 0 | localTime.tm_mon = result->tm_month; |
1635 | 0 | localTime.tm_year = result->tm_year - 1900; |
1636 | | /* Set this to -1 to tell mktime "I don't care". If you set |
1637 | | it to 0 or 1, you are making assertions about whether the |
1638 | | date you are handing it is in daylight savings mode or not; |
1639 | | and if you're wrong, it will "fix" it for you. */ |
1640 | 0 | localTime.tm_isdst = -1; |
1641 | |
|
1642 | | #if _MSC_VER == 1400 /* 1400 = Visual C++ 2005 (8.0) */ |
1643 | | /* |
1644 | | * mktime will return (time_t) -1 if the input is a date |
1645 | | * after 23:59:59, December 31, 3000, US Pacific Time (not |
1646 | | * UTC as documented): |
1647 | | * http://msdn.microsoft.com/en-us/library/d1y53h2a(VS.80).aspx |
1648 | | * But if the year is 3001, mktime also invokes the invalid |
1649 | | * parameter handler, causing the application to crash. This |
1650 | | * problem has been reported in |
1651 | | * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=266036. |
1652 | | * We avoid this crash by not calling mktime if the date is |
1653 | | * out of range. To use a simple test that works in any time |
1654 | | * zone, we consider year 3000 out of range as well. (See |
1655 | | * bug 480740.) |
1656 | | */ |
1657 | | if (result->tm_year >= 3000) { |
1658 | | /* Emulate what mktime would have done. */ |
1659 | | errno = EINVAL; |
1660 | | secs = (time_t)-1; |
1661 | | } else { |
1662 | | secs = mktime(&localTime); |
1663 | | } |
1664 | | #else |
1665 | 0 | secs = mktime(&localTime); |
1666 | 0 | #endif |
1667 | 0 | if (secs != (time_t)-1) { |
1668 | 0 | PRTime usecs64; |
1669 | 0 | LL_I2L(usecs64, secs); |
1670 | 0 | LL_I2L(usec_per_sec, PR_USEC_PER_SEC); |
1671 | 0 | LL_MUL(usecs64, usecs64, usec_per_sec); |
1672 | 0 | PR_ExplodeTime(usecs64, PR_LocalTimeParameters, result); |
1673 | 0 | return PR_SUCCESS; |
1674 | 0 | } |
1675 | 0 | } |
1676 | | |
1677 | | /* So mktime() can't handle this case. We assume the |
1678 | | zone_offset for the date we are parsing is the same as |
1679 | | the zone offset on 00:00:00 2 Jan 1970 GMT. */ |
1680 | 0 | secs = 86400; |
1681 | 0 | localTimeResult = MT_safe_localtime(&secs, &localTime); |
1682 | 0 | PR_ASSERT(localTimeResult != NULL); |
1683 | 0 | if (localTimeResult == NULL) { |
1684 | 0 | return PR_FAILURE; |
1685 | 0 | } |
1686 | 0 | zone_offset = localTime.tm_min + 60 * localTime.tm_hour + |
1687 | 0 | 1440 * (localTime.tm_mday - 2); |
1688 | 0 | } |
1689 | | |
1690 | 0 | result->tm_params.tp_gmt_offset = zone_offset * 60; |
1691 | 0 | result->tm_params.tp_dst_offset = dst_offset * 60; |
1692 | |
|
1693 | 0 | return PR_SUCCESS; |
1694 | 0 | } |
1695 | | |
1696 | | PR_IMPLEMENT(PRStatus) |
1697 | 0 | PR_ParseTimeString(const char* string, PRBool default_to_gmt, PRTime* result) { |
1698 | 0 | PRExplodedTime tm; |
1699 | 0 | PRStatus rv; |
1700 | |
|
1701 | 0 | rv = PR_ParseTimeStringToExplodedTime(string, default_to_gmt, &tm); |
1702 | 0 | if (rv != PR_SUCCESS) { |
1703 | 0 | return rv; |
1704 | 0 | } |
1705 | | |
1706 | 0 | *result = PR_ImplodeTime(&tm); |
1707 | |
|
1708 | 0 | return PR_SUCCESS; |
1709 | 0 | } |
1710 | | |
1711 | | /* |
1712 | | ******************************************************************* |
1713 | | ******************************************************************* |
1714 | | ** |
1715 | | ** OLD COMPATIBILITY FUNCTIONS |
1716 | | ** |
1717 | | ******************************************************************* |
1718 | | ******************************************************************* |
1719 | | */ |
1720 | | |
1721 | | /* |
1722 | | *----------------------------------------------------------------------- |
1723 | | * |
1724 | | * PR_FormatTime -- |
1725 | | * |
1726 | | * Format a time value into a buffer. Same semantics as strftime(). |
1727 | | * |
1728 | | *----------------------------------------------------------------------- |
1729 | | */ |
1730 | | |
1731 | | PR_IMPLEMENT(PRUint32) |
1732 | | PR_FormatTime(char* buf, int buflen, const char* fmt, |
1733 | 0 | const PRExplodedTime* time) { |
1734 | 0 | size_t rv; |
1735 | 0 | struct tm a; |
1736 | 0 | struct tm* ap; |
1737 | |
|
1738 | 0 | if (time) { |
1739 | 0 | ap = &a; |
1740 | 0 | a.tm_sec = time->tm_sec; |
1741 | 0 | a.tm_min = time->tm_min; |
1742 | 0 | a.tm_hour = time->tm_hour; |
1743 | 0 | a.tm_mday = time->tm_mday; |
1744 | 0 | a.tm_mon = time->tm_month; |
1745 | 0 | a.tm_wday = time->tm_wday; |
1746 | 0 | a.tm_year = time->tm_year - 1900; |
1747 | 0 | a.tm_yday = time->tm_yday; |
1748 | 0 | a.tm_isdst = time->tm_params.tp_dst_offset ? 1 : 0; |
1749 | | |
1750 | | /* |
1751 | | * On some platforms, for example SunOS 4, struct tm has two |
1752 | | * additional fields: tm_zone and tm_gmtoff. |
1753 | | */ |
1754 | |
|
1755 | 0 | #if (__GLIBC__ >= 2) || defined(NETBSD) || defined(OPENBSD) || \ |
1756 | 0 | defined(FREEBSD) || defined(DARWIN) || defined(ANDROID) |
1757 | 0 | a.tm_zone = NULL; |
1758 | 0 | a.tm_gmtoff = time->tm_params.tp_gmt_offset + time->tm_params.tp_dst_offset; |
1759 | 0 | #endif |
1760 | 0 | } else { |
1761 | 0 | ap = NULL; |
1762 | 0 | } |
1763 | |
|
1764 | 0 | rv = strftime(buf, buflen, fmt, ap); |
1765 | 0 | if (!rv && buf && buflen > 0) { |
1766 | | /* |
1767 | | * When strftime fails, the contents of buf are indeterminate. |
1768 | | * Some callers don't check the return value from this function, |
1769 | | * so store an empty string in buf in case they try to print it. |
1770 | | */ |
1771 | 0 | buf[0] = '\0'; |
1772 | 0 | } |
1773 | 0 | return rv; |
1774 | 0 | } |
1775 | | |
1776 | | /* |
1777 | | * The following string arrays and macros are used by PR_FormatTimeUSEnglish(). |
1778 | | */ |
1779 | | |
1780 | | static const char* abbrevDays[] = {"Sun", "Mon", "Tue", "Wed", |
1781 | | "Thu", "Fri", "Sat"}; |
1782 | | |
1783 | | static const char* days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", |
1784 | | "Thursday", "Friday", "Saturday"}; |
1785 | | |
1786 | | static const char* abbrevMonths[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
1787 | | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
1788 | | |
1789 | | static const char* months[] = {"January", "February", "March", "April", |
1790 | | "May", "June", "July", "August", |
1791 | | "September", "October", "November", "December"}; |
1792 | | |
1793 | | /* |
1794 | | * Add a single character to the given buffer, incrementing the buffer pointer |
1795 | | * and decrementing the buffer size. Return 0 on error. |
1796 | | */ |
1797 | | #define ADDCHAR(buf, bufSize, ch) \ |
1798 | 0 | do { \ |
1799 | 0 | if (bufSize < 1) { \ |
1800 | 0 | *(--buf) = '\0'; \ |
1801 | 0 | return 0; \ |
1802 | 0 | } \ |
1803 | 0 | *buf++ = ch; \ |
1804 | 0 | bufSize--; \ |
1805 | 0 | } while (0) |
1806 | | |
1807 | | /* |
1808 | | * Add a string to the given buffer, incrementing the buffer pointer |
1809 | | * and decrementing the buffer size appropriately. Return 0 on error. |
1810 | | */ |
1811 | | #define ADDSTR(buf, bufSize, str) \ |
1812 | 0 | do { \ |
1813 | 0 | PRUint32 strSize = strlen(str); \ |
1814 | 0 | if (strSize > bufSize) { \ |
1815 | 0 | if (bufSize == 0) \ |
1816 | 0 | *(--buf) = '\0'; \ |
1817 | 0 | else \ |
1818 | 0 | *buf = '\0'; \ |
1819 | 0 | return 0; \ |
1820 | 0 | } \ |
1821 | 0 | memcpy(buf, str, strSize); \ |
1822 | 0 | buf += strSize; \ |
1823 | 0 | bufSize -= strSize; \ |
1824 | 0 | } while (0) |
1825 | | |
1826 | | /* Needed by PR_FormatTimeUSEnglish() */ |
1827 | | static unsigned int pr_WeekOfYear(const PRExplodedTime* time, |
1828 | | unsigned int firstDayOfWeek); |
1829 | | |
1830 | | /*********************************************************************************** |
1831 | | * |
1832 | | * Description: |
1833 | | * This is a dumbed down version of strftime that will format the date in US |
1834 | | * English regardless of the setting of the global locale. This functionality |
1835 | | *is needed to write things like MIME headers which must always be in US |
1836 | | *English. |
1837 | | * |
1838 | | **********************************************************************************/ |
1839 | | |
1840 | | PR_IMPLEMENT(PRUint32) |
1841 | | PR_FormatTimeUSEnglish(char* buf, PRUint32 bufSize, const char* format, |
1842 | 0 | const PRExplodedTime* time) { |
1843 | 0 | char* bufPtr = buf; |
1844 | 0 | const char* fmtPtr; |
1845 | 0 | char tmpBuf[40]; |
1846 | 0 | const int tmpBufSize = sizeof(tmpBuf); |
1847 | |
|
1848 | 0 | for (fmtPtr = format; *fmtPtr != '\0'; fmtPtr++) { |
1849 | 0 | if (*fmtPtr != '%') { |
1850 | 0 | ADDCHAR(bufPtr, bufSize, *fmtPtr); |
1851 | 0 | } else { |
1852 | 0 | switch (*(++fmtPtr)) { |
1853 | 0 | case '%': |
1854 | | /* escaped '%' character */ |
1855 | 0 | ADDCHAR(bufPtr, bufSize, '%'); |
1856 | 0 | break; |
1857 | | |
1858 | 0 | case 'a': |
1859 | | /* abbreviated weekday name */ |
1860 | 0 | ADDSTR(bufPtr, bufSize, abbrevDays[time->tm_wday]); |
1861 | 0 | break; |
1862 | | |
1863 | 0 | case 'A': |
1864 | | /* full weekday name */ |
1865 | 0 | ADDSTR(bufPtr, bufSize, days[time->tm_wday]); |
1866 | 0 | break; |
1867 | | |
1868 | 0 | case 'b': |
1869 | | /* abbreviated month name */ |
1870 | 0 | ADDSTR(bufPtr, bufSize, abbrevMonths[time->tm_month]); |
1871 | 0 | break; |
1872 | | |
1873 | 0 | case 'B': |
1874 | | /* full month name */ |
1875 | 0 | ADDSTR(bufPtr, bufSize, months[time->tm_month]); |
1876 | 0 | break; |
1877 | | |
1878 | 0 | case 'c': |
1879 | | /* Date and time. */ |
1880 | 0 | PR_FormatTimeUSEnglish(tmpBuf, tmpBufSize, "%a %b %d %H:%M:%S %Y", |
1881 | 0 | time); |
1882 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1883 | 0 | break; |
1884 | | |
1885 | 0 | case 'd': |
1886 | | /* day of month ( 01 - 31 ) */ |
1887 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.2ld", time->tm_mday); |
1888 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1889 | 0 | break; |
1890 | | |
1891 | 0 | case 'e': |
1892 | | /* day of month with space prefix for single digits ( 1 - 31 ) */ |
1893 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%2ld", time->tm_mday); |
1894 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1895 | 0 | break; |
1896 | | |
1897 | 0 | case 'H': |
1898 | | /* hour ( 00 - 23 ) */ |
1899 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.2ld", time->tm_hour); |
1900 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1901 | 0 | break; |
1902 | | |
1903 | 0 | case 'I': |
1904 | | /* hour ( 01 - 12 ) */ |
1905 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.2ld", |
1906 | 0 | (time->tm_hour % 12) ? time->tm_hour % 12 : (PRInt32)12); |
1907 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1908 | 0 | break; |
1909 | | |
1910 | 0 | case 'j': |
1911 | | /* day number of year ( 001 - 366 ) */ |
1912 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.3d", time->tm_yday + 1); |
1913 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1914 | 0 | break; |
1915 | | |
1916 | 0 | case 'm': |
1917 | | /* month number ( 01 - 12 ) */ |
1918 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.2ld", time->tm_month + 1); |
1919 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1920 | 0 | break; |
1921 | | |
1922 | 0 | case 'M': |
1923 | | /* minute ( 00 - 59 ) */ |
1924 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.2ld", time->tm_min); |
1925 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1926 | 0 | break; |
1927 | | |
1928 | 0 | case 'p': |
1929 | | /* locale's equivalent of either AM or PM */ |
1930 | 0 | ADDSTR(bufPtr, bufSize, (time->tm_hour < 12) ? "AM" : "PM"); |
1931 | 0 | break; |
1932 | | |
1933 | 0 | case 'S': |
1934 | | /* seconds ( 00 - 61 ), allows for leap seconds */ |
1935 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.2ld", time->tm_sec); |
1936 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1937 | 0 | break; |
1938 | | |
1939 | 0 | case 'U': |
1940 | | /* week number of year ( 00 - 53 ), Sunday is the first day of |
1941 | | * week 1 */ |
1942 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.2d", pr_WeekOfYear(time, 0)); |
1943 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1944 | 0 | break; |
1945 | | |
1946 | 0 | case 'w': |
1947 | | /* weekday number ( 0 - 6 ), Sunday = 0 */ |
1948 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%d", time->tm_wday); |
1949 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1950 | 0 | break; |
1951 | | |
1952 | 0 | case 'W': |
1953 | | /* Week number of year ( 00 - 53 ), Monday is the first day of |
1954 | | * week 1 */ |
1955 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.2d", pr_WeekOfYear(time, 1)); |
1956 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1957 | 0 | break; |
1958 | | |
1959 | 0 | case 'x': |
1960 | | /* Date representation */ |
1961 | 0 | PR_FormatTimeUSEnglish(tmpBuf, tmpBufSize, "%m/%d/%y", time); |
1962 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1963 | 0 | break; |
1964 | | |
1965 | 0 | case 'X': |
1966 | | /* Time representation. */ |
1967 | 0 | PR_FormatTimeUSEnglish(tmpBuf, tmpBufSize, "%H:%M:%S", time); |
1968 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1969 | 0 | break; |
1970 | | |
1971 | 0 | case 'y': |
1972 | | /* year within century ( 00 - 99 ) */ |
1973 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.2d", time->tm_year % 100); |
1974 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1975 | 0 | break; |
1976 | | |
1977 | 0 | case 'Y': |
1978 | | /* year as ccyy ( for example 1986 ) */ |
1979 | 0 | PR_snprintf(tmpBuf, tmpBufSize, "%.4d", time->tm_year); |
1980 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1981 | 0 | break; |
1982 | | |
1983 | 0 | case 'Z': |
1984 | | /* Time zone name or no characters if no time zone exists. |
1985 | | * Since time zone name is supposed to be independant of locale, we |
1986 | | * defer to PR_FormatTime() for this option. |
1987 | | */ |
1988 | 0 | PR_FormatTime(tmpBuf, tmpBufSize, "%Z", time); |
1989 | 0 | ADDSTR(bufPtr, bufSize, tmpBuf); |
1990 | 0 | break; |
1991 | | |
1992 | 0 | default: |
1993 | | /* Unknown format. Simply copy format into output buffer. */ |
1994 | 0 | ADDCHAR(bufPtr, bufSize, '%'); |
1995 | 0 | ADDCHAR(bufPtr, bufSize, *fmtPtr); |
1996 | 0 | break; |
1997 | 0 | } |
1998 | 0 | } |
1999 | 0 | } |
2000 | | |
2001 | 0 | ADDCHAR(bufPtr, bufSize, '\0'); |
2002 | 0 | return (PRUint32)(bufPtr - buf - 1); |
2003 | 0 | } |
2004 | | |
2005 | | /*********************************************************************************** |
2006 | | * |
2007 | | * Description: |
2008 | | * Returns the week number of the year (0-53) for the given time. |
2009 | | *firstDayOfWeek is the day on which the week is considered to start (0=Sun, |
2010 | | *1=Mon, ...). Week 1 starts the first time firstDayOfWeek occurs in the year. |
2011 | | *In other words, a partial week at the start of the year is considered week 0. |
2012 | | * |
2013 | | **********************************************************************************/ |
2014 | | |
2015 | | static unsigned int pr_WeekOfYear(const PRExplodedTime* time, |
2016 | 0 | unsigned int firstDayOfWeek) { |
2017 | 0 | int dayOfWeek; |
2018 | 0 | int dayOfYear; |
2019 | | |
2020 | | /* Get the day of the year for the given time then adjust it to represent the |
2021 | | * first day of the week containing the given time. |
2022 | | */ |
2023 | 0 | dayOfWeek = time->tm_wday - firstDayOfWeek; |
2024 | 0 | if (dayOfWeek < 0) { |
2025 | 0 | dayOfWeek += 7; |
2026 | 0 | } |
2027 | |
|
2028 | 0 | dayOfYear = time->tm_yday - dayOfWeek; |
2029 | |
|
2030 | 0 | if (dayOfYear <= 0) { |
2031 | | /* If dayOfYear is <= 0, it is in the first partial week of the year. */ |
2032 | 0 | return 0; |
2033 | 0 | } |
2034 | | |
2035 | | /* Count the number of full weeks ( dayOfYear / 7 ) then add a week if there |
2036 | | * are any days left over ( dayOfYear % 7 ). Because we are only counting to |
2037 | | * the first day of the week containing the given time, rather than to the |
2038 | | * actual day representing the given time, any days in week 0 will be |
2039 | | * "absorbed" as extra days in the given week. |
2040 | | */ |
2041 | 0 | return (dayOfYear / 7) + ((dayOfYear % 7) == 0 ? 0 : 1); |
2042 | 0 | } |