Lines 56-65
Link Here
|
56 |
#include "libc_private.h" |
56 |
#include "libc_private.h" |
57 |
#include "timelocal.h" |
57 |
#include "timelocal.h" |
58 |
|
58 |
|
|
|
59 |
#include "tzfile.h" |
60 |
|
59 |
static char * _strptime(const char *, const char *, struct tm *, int *, locale_t); |
61 |
static char * _strptime(const char *, const char *, struct tm *, int *, locale_t); |
60 |
|
62 |
|
61 |
#define asizeof(a) (sizeof (a) / sizeof ((a)[0])) |
63 |
#define asizeof(a) (sizeof(a) / sizeof((a)[0])) |
62 |
|
64 |
|
|
|
65 |
/* |
66 |
* Calculate the week day of the first day of a year. Valid for |
67 |
* the Gregorian calendar, which began Sept 14, 1752 in the UK |
68 |
* and its colonies. Ref: |
69 |
* http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week/ |
70 |
*/ |
71 |
|
72 |
static int |
73 |
first_wday_of(int year) |
74 |
{ |
75 |
return (((2 * (3 - (year / 100) % 4)) + (year % 100) + |
76 |
((year % 100) / 4) + (isleap(year) ? 6 : 0) + 1) % 7); |
77 |
} |
78 |
|
79 |
|
63 |
static char * |
80 |
static char * |
64 |
_strptime(const char *buf, const char *fmt, struct tm *tm, int *GMTp, |
81 |
_strptime(const char *buf, const char *fmt, struct tm *tm, int *GMTp, |
65 |
locale_t locale) |
82 |
locale_t locale) |
Lines 66-74
Link Here
|
66 |
{ |
83 |
{ |
67 |
char c; |
84 |
char c; |
68 |
const char *ptr; |
85 |
const char *ptr; |
|
|
86 |
int day_offset = -1; |
69 |
int i, len; |
87 |
int i, len; |
70 |
int Ealternative, Oalternative; |
88 |
int Ealternative, Oalternative; |
71 |
struct lc_time_T *tptr = __get_current_time_locale(locale); |
89 |
const struct lc_time_T *tptr = __get_current_time_locale(locale); |
|
|
90 |
static int start_of_month[2][13] = { |
91 |
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, |
92 |
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}}; |
72 |
|
93 |
|
73 |
ptr = fmt; |
94 |
ptr = fmt; |
74 |
while (*ptr != 0) { |
95 |
while (*ptr != 0) { |
Lines 119-125
Link Here
|
119 |
if (i < 19) |
140 |
if (i < 19) |
120 |
return (NULL); |
141 |
return (NULL); |
121 |
|
142 |
|
122 |
tm->tm_year = i * 100 - 1900; |
143 |
tm->tm_year = i * 100 - TM_YEAR_BASE; |
123 |
break; |
144 |
break; |
124 |
|
145 |
|
125 |
case 'c': |
146 |
case 'c': |
Lines 303-308
Link Here
|
303 |
return (NULL); |
324 |
return (NULL); |
304 |
|
325 |
|
305 |
tm->tm_wday = i; |
326 |
tm->tm_wday = i; |
|
|
327 |
if (day_offset >= 0 && (i - day_offset) != 0) { |
328 |
tm->tm_yday += i - day_offset; |
329 |
i = 0; |
330 |
while (tm->tm_yday >= |
331 |
start_of_month[isleap(tm->tm_year + |
332 |
TM_YEAR_BASE)][i]) |
333 |
i++; |
334 |
if (i > 12) |
335 |
{ |
336 |
i = 1; |
337 |
tm->tm_yday -= |
338 |
start_of_month[ |
339 |
isleap(tm->tm_year + TM_YEAR_BASE)] |
340 |
[12]; |
341 |
tm->tm_year++; |
342 |
} |
343 |
tm->tm_mon = i - 1; |
344 |
tm->tm_mday = tm->tm_yday - |
345 |
start_of_month[isleap(tm->tm_year + |
346 |
TM_YEAR_BASE)][i - 1] + 1; |
347 |
} |
306 |
buf += len; |
348 |
buf += len; |
307 |
break; |
349 |
break; |
308 |
|
350 |
|
Lines 309-318
Link Here
|
309 |
case 'U': |
351 |
case 'U': |
310 |
case 'W': |
352 |
case 'W': |
311 |
/* |
353 |
/* |
312 |
* XXX This is bogus, as we can not assume any valid |
354 |
* We expect that the year has already been |
313 |
* information present in the tm structure at this |
355 |
* parsed. |
314 |
* point to calculate a real value, so just check the |
|
|
315 |
* range for now. |
316 |
*/ |
356 |
*/ |
317 |
if (!isdigit_l((unsigned char)*buf, locale)) |
357 |
if (!isdigit_l((unsigned char)*buf, locale)) |
318 |
return (NULL); |
358 |
return (NULL); |
Lines 327-332
Link Here
|
327 |
if (i > 53) |
367 |
if (i > 53) |
328 |
return (NULL); |
368 |
return (NULL); |
329 |
|
369 |
|
|
|
370 |
/* Week numbers are 1-origin. So that we can always |
371 |
* return the date of a Sunday (or Monday), treat week |
372 |
* 0 as week 1. |
373 |
*/ |
374 |
|
375 |
if (i == 0) |
376 |
i = 1; |
377 |
|
378 |
if (c == 'U') |
379 |
day_offset = TM_SUNDAY; |
380 |
else |
381 |
day_offset = TM_MONDAY; |
382 |
|
383 |
/* |
384 |
* Set the date to the first Sunday (or Monday) |
385 |
* of the specified week of the year. |
386 |
*/ |
387 |
|
388 |
tm->tm_yday = (7 - first_wday_of(tm->tm_year + |
389 |
TM_YEAR_BASE) + day_offset) % 7 + (i - 1) * 7; |
390 |
i = 0; |
391 |
while (tm->tm_yday >= |
392 |
start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)][i]) |
393 |
i++; |
394 |
if (i > 12) |
395 |
{ |
396 |
i = 1; |
397 |
tm->tm_yday -= |
398 |
start_of_month[ |
399 |
isleap(tm->tm_year + TM_YEAR_BASE)][12]; |
400 |
tm->tm_year++; |
401 |
} |
402 |
tm->tm_mon = i - 1; |
403 |
tm->tm_mday = tm->tm_yday - |
404 |
start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)][i - 1] + 1; |
405 |
tm->tm_wday = day_offset; |
406 |
|
330 |
break; |
407 |
break; |
331 |
|
408 |
|
332 |
case 'w': |
409 |
case 'w': |
Lines 471-477
Link Here
|
471 |
len--; |
548 |
len--; |
472 |
} |
549 |
} |
473 |
if (c == 'Y') |
550 |
if (c == 'Y') |
474 |
i -= 1900; |
551 |
i -= TM_YEAR_BASE; |
475 |
if (c == 'y' && i < 69) |
552 |
if (c == 'y' && i < 69) |
476 |
i += 100; |
553 |
i += 100; |
477 |
if (i < 0) |
554 |
if (i < 0) |
Lines 546-552
Link Here
|
546 |
return ((char *)buf); |
623 |
return ((char *)buf); |
547 |
} |
624 |
} |
548 |
|
625 |
|
549 |
|
|
|
550 |
char * |
626 |
char * |
551 |
strptime_l(const char * __restrict buf, const char * __restrict fmt, |
627 |
strptime_l(const char * __restrict buf, const char * __restrict fmt, |
552 |
struct tm * __restrict tm, locale_t loc) |
628 |
struct tm * __restrict tm, locale_t loc) |
Lines 564-569
Link Here
|
564 |
|
640 |
|
565 |
return (ret); |
641 |
return (ret); |
566 |
} |
642 |
} |
|
|
643 |
|
567 |
char * |
644 |
char * |
568 |
strptime(const char * __restrict buf, const char * __restrict fmt, |
645 |
strptime(const char * __restrict buf, const char * __restrict fmt, |
569 |
struct tm * __restrict tm) |
646 |
struct tm * __restrict tm) |