From 57fa34b52e7c26f780c6bcae7a8ea863dfe710e1 Mon Sep 17 00:00:00 2001 From: joseph Date: Sun, 18 Mar 2007 18:01:40 +0000 Subject: Merge changes between r1382 and r1748 from /fsf/trunk. git-svn-id: svn://svn.eglibc.org/trunk@1749 7b3dc134-2b1b-0410-93df-9e9f96275f8d --- libc/time/Makefile | 5 +++-- libc/time/strptime_l.c | 11 +++++++--- libc/time/tst-mktime2.c | 5 +++-- libc/time/tst-strptime3.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ libc/time/tzfile.c | 53 ++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 libc/time/tst-strptime3.c (limited to 'libc/time') diff --git a/libc/time/Makefile b/libc/time/Makefile index d93b84bb2..8ce34e456 100644 --- a/libc/time/Makefile +++ b/libc/time/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 1991-2003, 2004, 2005 Free Software Foundation, Inc. +# Copyright (C) 1991-2003, 2004, 2005, 2007 Free Software Foundation, Inc. # This file is part of the GNU C Library. # The GNU C Library is free software; you can redistribute it and/or @@ -35,7 +35,8 @@ distribute := datemsk tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \ tst-getdate tst-mktime tst-mktime2 tst-ftime_l tst-strftime \ - tst-mktime3 tst-strptime2 bug-asctime bug-asctime_r bug-mktime1 + tst-mktime3 tst-strptime2 bug-asctime bug-asctime_r bug-mktime1 \ + tst-strptime3 include ../Rules diff --git a/libc/time/strptime_l.c b/libc/time/strptime_l.c index dc0cc686f..443a6fa88 100644 --- a/libc/time/strptime_l.c +++ b/libc/time/strptime_l.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. +/* Copyright (C) 2002, 2004, 2005, 2007 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -400,6 +400,7 @@ __strptime_internal (rp, fmt, tm, decided, era_cnt LOCALE_PARAM) /* Does not match a month name. */ return NULL; tm->tm_mon = cnt; + have_mon = 1; want_xday = 1; break; case 'c': @@ -1085,11 +1086,15 @@ __strptime_internal (rp, fmt, tm, decided, era_cnt LOCALE_PARAM) tm->tm_mday = (tm->tm_yday - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1); + have_mon = 1; + have_mday = 1; } - day_of_the_week (tm); + /* Don't crash in day_of_the_week if tm_mon is uninitialized. */ + if (have_mon || (unsigned) tm->tm_mon <= 11) + day_of_the_week (tm); } - if (want_xday && !have_yday) + if (want_xday && !have_yday && (have_mon || (unsigned) tm->tm_mon <= 11)) day_of_the_year (tm); if ((have_uweek || have_wweek) && have_wday) diff --git a/libc/time/tst-mktime2.c b/libc/time/tst-mktime2.c index 6279218cb..0e4fd1e78 100644 --- a/libc/time/tst-mktime2.c +++ b/libc/time/tst-mktime2.c @@ -102,7 +102,8 @@ static int do_test (void) { time_t t, delta; - int i, j; + int i; + unsigned int j; setenv ("TZ", "America/Sao_Paulo", 1); /* This test makes some buggy mktime implementations loop. @@ -128,7 +129,7 @@ do_test (void) mktime_test ((time_t) (60 * 60)); mktime_test ((time_t) (60 * 60 * 24)); - for (j = 1; 0 < j; j *= 2) + for (j = 1; j <= INT_MAX; j *= 2) bigtime_test (j); bigtime_test (j - 1); } diff --git a/libc/time/tst-strptime3.c b/libc/time/tst-strptime3.c new file mode 100644 index 000000000..9a8c6485e --- /dev/null +++ b/libc/time/tst-strptime3.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include + + +int +main (void) +{ + int result = 0; + struct tm tm; + + memset (&tm, 0xaa, sizeof (tm)); + + /* Test we don't crash on uninitialized struct tm. + Some fields might contain bogus values until everything + needed is initialized, but we shouldn't crash. */ + if (strptime ("2007", "%Y", &tm) == NULL + || strptime ("12", "%d", &tm) == NULL + || strptime ("Feb", "%b", &tm) == NULL + || strptime ("13", "%M", &tm) == NULL + || strptime ("21", "%S", &tm) == NULL + || strptime ("16", "%H", &tm) == NULL) + { + puts ("strptimes failed"); + result = 1; + } + + if (tm.tm_sec != 21 || tm.tm_min != 13 || tm.tm_hour != 16 + || tm.tm_mday != 12 || tm.tm_mon != 1 || tm.tm_year != 107 + || tm.tm_wday != 1 || tm.tm_yday != 42) + { + puts ("unexpected tm content"); + result = 1; + } + + if (strptime ("8", "%d", &tm) == NULL) + { + puts ("strptime failed"); + result = 1; + } + + if (tm.tm_sec != 21 || tm.tm_min != 13 || tm.tm_hour != 16 + || tm.tm_mday != 8 || tm.tm_mon != 1 || tm.tm_year != 107 + || tm.tm_wday != 4 || tm.tm_yday != 38) + { + puts ("unexpected tm content"); + result = 1; + } + + if (result == 0) + puts ("all OK"); + + return 0; +} diff --git a/libc/time/tzfile.c b/libc/time/tzfile.c index ea2d7cae4..0d48c8ca0 100644 --- a/libc/time/tzfile.c +++ b/libc/time/tzfile.c @@ -548,13 +548,60 @@ find_transition (time_t timer) if (i == num_types) i = 0; } + else if (timer >= transitions[num_transitions - 1]) + i = type_idxs[num_transitions - 1]; else { /* Find the first transition after TIMER, and then pick the type of the transition before it. */ - for (i = 1; i < num_transitions; ++i) - if (timer < transitions[i]) - break; + size_t lo = 0; + size_t hi = num_transitions - 1; + /* Assume that DST is changing twice a year and guess initial + search spot from it. + Half of a gregorian year has on average 365.2425 * 86400 / 2 + = 15778476 seconds. */ + i = (transitions[num_transitions - 1] - timer) / 15778476; + if (i < num_transitions) + { + i = num_transitions - 1 - i; + if (timer < transitions[i]) + { + if (i < 10 || timer >= transitions[i - 10]) + { + /* Linear search. */ + while (timer < transitions[i - 1]) + --i; + goto found; + } + hi = i - 10; + } + else + { + if (i + 10 >= num_transitions || timer < transitions[i + 10]) + { + /* Linear search. */ + while (timer >= transitions[i]) + ++i; + goto found; + } + lo = i + 10; + } + } + + /* Binary search. */ + /* assert (timer >= transitions[lo] && timer < transitions[hi]); */ + while (lo + 1 < hi) + { + i = (lo + hi) / 2; + if (timer < transitions[i]) + hi = i; + else + lo = i; + } + i = hi; + + found: + /* assert (timer >= transitions[i - 1] && timer < transitions[i]); */ i = type_idxs[i - 1]; } -- cgit v1.2.3