comparison nspr/pr/src/misc/prtime.c @ 0:1e5118fa0cb1

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