Lines 55-65
Link Here
|
55 |
#include "un-namespace.h" |
55 |
#include "un-namespace.h" |
56 |
#include "libc_private.h" |
56 |
#include "libc_private.h" |
57 |
#include "timelocal.h" |
57 |
#include "timelocal.h" |
|
|
58 |
#include "tzfile.h" |
58 |
|
59 |
|
59 |
static char * _strptime(const char *, const char *, struct tm *, int *, locale_t); |
60 |
static char * _strptime(const char *, const char *, struct tm *, int *, locale_t); |
60 |
|
61 |
|
61 |
#define asizeof(a) (sizeof (a) / sizeof ((a)[0])) |
62 |
#define asizeof(a) (sizeof(a) / sizeof((a)[0])) |
62 |
|
63 |
|
|
|
64 |
#define FLAG_NONE (1 << 0) |
65 |
#define FLAG_YEAR (1 << 1) |
66 |
#define FLAG_MONTH (1 << 2) |
67 |
#define FLAG_YDAY (1 << 3) |
68 |
#define FLAG_MDAY (1 << 4) |
69 |
#define FLAG_WDAY (1 << 5) |
70 |
|
71 |
/* |
72 |
* Calculate the week day of the first day of a year. Valid for |
73 |
* the Gregorian calendar, which began Sept 14, 1752 in the UK |
74 |
* and its colonies. Ref: |
75 |
* http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week/ |
76 |
*/ |
77 |
|
78 |
static int |
79 |
first_wday_of(int year) |
80 |
{ |
81 |
return (((2 * (3 - (year / 100) % 4)) + (year % 100) + |
82 |
((year % 100) / 4) + (isleap(year) ? 6 : 0) + 1) % 7); |
83 |
} |
84 |
|
63 |
static char * |
85 |
static char * |
64 |
_strptime(const char *buf, const char *fmt, struct tm *tm, int *GMTp, |
86 |
_strptime(const char *buf, const char *fmt, struct tm *tm, int *GMTp, |
65 |
locale_t locale) |
87 |
locale_t locale) |
Lines 66-75
Link Here
|
66 |
{ |
88 |
{ |
67 |
char c; |
89 |
char c; |
68 |
const char *ptr; |
90 |
const char *ptr; |
|
|
91 |
int day_offset = -1, wday_offset; |
69 |
int i, len; |
92 |
int i, len; |
|
|
93 |
int flags; |
70 |
int Ealternative, Oalternative; |
94 |
int Ealternative, Oalternative; |
71 |
struct lc_time_T *tptr = __get_current_time_locale(locale); |
95 |
const struct lc_time_T *tptr = __get_current_time_locale(locale); |
|
|
96 |
static int start_of_month[2][13] = { |
97 |
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, |
98 |
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366} |
99 |
}; |
72 |
|
100 |
|
|
|
101 |
flags = FLAG_NONE; |
102 |
|
73 |
ptr = fmt; |
103 |
ptr = fmt; |
74 |
while (*ptr != 0) { |
104 |
while (*ptr != 0) { |
75 |
if (*buf == 0) |
105 |
if (*buf == 0) |
Lines 119-125
Link Here
|
119 |
if (i < 19) |
149 |
if (i < 19) |
120 |
return (NULL); |
150 |
return (NULL); |
121 |
|
151 |
|
122 |
tm->tm_year = i * 100 - 1900; |
152 |
tm->tm_year = i * 100 - TM_YEAR_BASE; |
|
|
153 |
flags |= FLAG_YEAR; |
154 |
|
123 |
break; |
155 |
break; |
124 |
|
156 |
|
125 |
case 'c': |
157 |
case 'c': |
Lines 197-202
Link Here
|
197 |
return (NULL); |
229 |
return (NULL); |
198 |
|
230 |
|
199 |
tm->tm_yday = i - 1; |
231 |
tm->tm_yday = i - 1; |
|
|
232 |
flags |= FLAG_YDAY; |
233 |
|
200 |
break; |
234 |
break; |
201 |
|
235 |
|
202 |
case 'M': |
236 |
case 'M': |
Lines 303-309
Link Here
|
303 |
return (NULL); |
337 |
return (NULL); |
304 |
|
338 |
|
305 |
tm->tm_wday = i; |
339 |
tm->tm_wday = i; |
|
|
340 |
if (day_offset >= 0 && (i - day_offset) != 0) { |
341 |
tm->tm_yday += i - day_offset; |
342 |
i = 0; |
343 |
while (tm->tm_yday >= |
344 |
start_of_month[isleap(tm->tm_year + |
345 |
TM_YEAR_BASE)][i]) |
346 |
i++; |
347 |
if (i > 12) |
348 |
{ |
349 |
i = 1; |
350 |
tm->tm_yday -= |
351 |
start_of_month[isleap(tm->tm_year + |
352 |
TM_YEAR_BASE)] |
353 |
[12]; |
354 |
tm->tm_year++; |
355 |
} |
356 |
tm->tm_mon = i - 1; |
357 |
tm->tm_mday = tm->tm_yday - |
358 |
start_of_month[isleap(tm->tm_year + |
359 |
TM_YEAR_BASE)] |
360 |
[i - 1] + 1; |
361 |
} |
306 |
buf += len; |
362 |
buf += len; |
|
|
363 |
flags |= FLAG_YEAR | FLAG_MONTH | FLAG_YDAY | |
364 |
FLAG_MDAY | FLAG_WDAY; |
365 |
|
307 |
break; |
366 |
break; |
308 |
|
367 |
|
309 |
case 'U': |
368 |
case 'U': |
Lines 313-318
Link Here
|
313 |
* information present in the tm structure at this |
372 |
* information present in the tm structure at this |
314 |
* point to calculate a real value, so just check the |
373 |
* point to calculate a real value, so just check the |
315 |
* range for now. |
374 |
* range for now. |
|
|
375 |
* We expect that the year has already been |
376 |
* parsed. |
316 |
*/ |
377 |
*/ |
317 |
if (!isdigit_l((unsigned char)*buf, locale)) |
378 |
if (!isdigit_l((unsigned char)*buf, locale)) |
318 |
return (NULL); |
379 |
return (NULL); |
Lines 327-332
Link Here
|
327 |
if (i > 53) |
388 |
if (i > 53) |
328 |
return (NULL); |
389 |
return (NULL); |
329 |
|
390 |
|
|
|
391 |
/* Week numbers are 1-origin. So that we can always |
392 |
* return the date of a Sunday (or Monday), treat week |
393 |
* 0 as week 1. |
394 |
*/ |
395 |
|
396 |
if (i == 0) |
397 |
i = 1; |
398 |
|
399 |
if (c == 'U') |
400 |
day_offset = TM_SUNDAY; |
401 |
else |
402 |
day_offset = TM_MONDAY; |
403 |
|
404 |
/* Set the date to the first Sunday (or Monday) |
405 |
* of the specified week of the year. |
406 |
*/ |
407 |
|
408 |
tm->tm_yday = (7 - first_wday_of(tm->tm_year + |
409 |
TM_YEAR_BASE) + day_offset) % 7 + (i - 1) * 7; |
410 |
i = 0; |
411 |
while (tm->tm_yday >= |
412 |
start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)][i]) |
413 |
i++; |
414 |
if (i > 12) |
415 |
{ |
416 |
i = 1; |
417 |
tm->tm_yday -= |
418 |
start_of_month[isleap(tm->tm_year + |
419 |
TM_YEAR_BASE)][12]; |
420 |
tm->tm_year++; |
421 |
} |
422 |
tm->tm_mon = i - 1; |
423 |
tm->tm_mday = tm->tm_yday - |
424 |
start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)] |
425 |
[i - 1] + 1; |
426 |
tm->tm_wday = day_offset; |
427 |
flags |= FLAG_YEAR | FLAG_MONTH | FLAG_YDAY | |
428 |
FLAG_MDAY | FLAG_WDAY; |
429 |
|
330 |
break; |
430 |
break; |
331 |
|
431 |
|
332 |
case 'w': |
432 |
case 'w': |
Lines 338-343
Link Here
|
338 |
return (NULL); |
438 |
return (NULL); |
339 |
|
439 |
|
340 |
tm->tm_wday = i; |
440 |
tm->tm_wday = i; |
|
|
441 |
flags |= FLAG_WDAY; |
341 |
|
442 |
|
342 |
break; |
443 |
break; |
343 |
|
444 |
|
Lines 374-379
Link Here
|
374 |
return (NULL); |
475 |
return (NULL); |
375 |
|
476 |
|
376 |
tm->tm_mday = i; |
477 |
tm->tm_mday = i; |
|
|
478 |
flags |= FLAG_MDAY; |
377 |
|
479 |
|
378 |
break; |
480 |
break; |
379 |
|
481 |
|
Lines 413-418
Link Here
|
413 |
|
515 |
|
414 |
tm->tm_mon = i; |
516 |
tm->tm_mon = i; |
415 |
buf += len; |
517 |
buf += len; |
|
|
518 |
flags |= FLAG_MONTH; |
519 |
|
416 |
break; |
520 |
break; |
417 |
|
521 |
|
418 |
case 'm': |
522 |
case 'm': |
Lines 430-435
Link Here
|
430 |
return (NULL); |
534 |
return (NULL); |
431 |
|
535 |
|
432 |
tm->tm_mon = i - 1; |
536 |
tm->tm_mon = i - 1; |
|
|
537 |
flags |= FLAG_MONTH; |
433 |
|
538 |
|
434 |
break; |
539 |
break; |
435 |
|
540 |
|
Lines 471-477
Link Here
|
471 |
len--; |
576 |
len--; |
472 |
} |
577 |
} |
473 |
if (c == 'Y') |
578 |
if (c == 'Y') |
474 |
i -= 1900; |
579 |
i -= TM_YEAR_BASE; |
475 |
if (c == 'y' && i < 69) |
580 |
if (c == 'y' && i < 69) |
476 |
i += 100; |
581 |
i += 100; |
477 |
if (i < 0) |
582 |
if (i < 0) |
Lines 478-483
Link Here
|
478 |
return (NULL); |
583 |
return (NULL); |
479 |
|
584 |
|
480 |
tm->tm_year = i; |
585 |
tm->tm_year = i; |
|
|
586 |
flags |= FLAG_YEAR; |
481 |
|
587 |
|
482 |
break; |
588 |
break; |
483 |
|
589 |
|
Lines 543-552
Link Here
|
543 |
break; |
649 |
break; |
544 |
} |
650 |
} |
545 |
} |
651 |
} |
|
|
652 |
|
653 |
if (flags & (FLAG_YEAR | FLAG_MONTH)) { |
654 |
if (!(flags & FLAG_YDAY) && (flags & FLAG_MDAY)) |
655 |
tm->tm_yday = start_of_month[isleap(tm->tm_year |
656 |
+ TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1); |
657 |
if (!(flags & FLAG_WDAY)) { |
658 |
i = 0; |
659 |
wday_offset = first_wday_of(tm->tm_year); |
660 |
while (i++ <= tm->tm_yday) |
661 |
if (wday_offset++ >= 6) |
662 |
wday_offset = 0; |
663 |
|
664 |
tm->tm_wday = wday_offset; |
665 |
} |
666 |
} |
667 |
|
546 |
return ((char *)buf); |
668 |
return ((char *)buf); |
547 |
} |
669 |
} |
548 |
|
670 |
|
549 |
|
|
|
550 |
char * |
671 |
char * |
551 |
strptime_l(const char * __restrict buf, const char * __restrict fmt, |
672 |
strptime_l(const char * __restrict buf, const char * __restrict fmt, |
552 |
struct tm * __restrict tm, locale_t loc) |
673 |
struct tm * __restrict tm, locale_t loc) |