123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738 |
- #ifndef _LIBC
- # include <config.h>
- #endif
- #ifndef WRAPV
- # if (__GNUC__ == 4 && 4 <= __GNUC_MINOR__) || 4 < __GNUC__
- # pragma GCC optimize ("wrapv")
- # define WRAPV 1
- # else
- # define WRAPV 0
- # endif
- #endif
- /* Assume that leap seconds are possible, unless told otherwise.
- If the host has a `zic' command with a `-L leapsecondfilename' option,
- then it supports leap seconds; otherwise it probably doesn't. */
- #ifndef LEAP_SECONDS_POSSIBLE
- # define LEAP_SECONDS_POSSIBLE 1
- #endif
- #include <time.h>
- #include <limits.h>
- #include <string.h>
- #if DEBUG
- # include <stdio.h>
- # include <stdlib.h>
- # undef mktime
- # define mktime my_mktime
- #endif
- #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
- #if INT_MAX <= LONG_MAX / 2
- typedef long int long_int;
- #else
- typedef long long int long_int;
- #endif
- verify (long_int_is_wide_enough, INT_MAX == INT_MAX * (long_int) 2 / 2);
- /* Shift A right by B bits portably, by dividing A by 2**B and
- truncating towards minus infinity. A and B should be free of side
- effects, and B should be in the range 0 <= B <= INT_BITS - 2, where
- INT_BITS is the number of useful bits in an int. GNU code can
- assume that INT_BITS is at least 32.
- ISO C99 says that A >> B is implementation-defined if A < 0. Some
- implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift
- right in the usual way when A < 0, so SHR falls back on division if
- ordinary A >> B doesn't seem to be the usual signed shift. */
- #define SHR(a, b) \
- ((-1 >> 1 == -1 \
- && (long_int) -1 >> 1 == -1 \
- && ((time_t) -1 >> 1 == -1 || ! TYPE_SIGNED (time_t))) \
- ? (a) >> (b) \
- : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
- /* The extra casts in the following macros work around compiler bugs,
- e.g., in Cray C 5.0.3.0. */
- /* True if the arithmetic type T is an integer type. bool counts as
- an integer. */
- #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
- /* True if negative values of the signed integer type T use two's
- complement, or if T is an unsigned integer type. */
- #define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
- #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
- /* The maximum and minimum values for the integer type T. These
- macros have undefined behavior if T is signed and has padding bits.
- If this is a problem for you, please let us know how to fix it for
- your host. */
- #define TYPE_MINIMUM(t) \
- ((t) (! TYPE_SIGNED (t) \
- ? (t) 0 \
- : ~ TYPE_MAXIMUM (t)))
- #define TYPE_MAXIMUM(t) \
- ((t) (! TYPE_SIGNED (t) \
- ? (t) -1 \
- : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
- #ifndef TIME_T_MIN
- # define TIME_T_MIN TYPE_MINIMUM (time_t)
- #endif
- #ifndef TIME_T_MAX
- # define TIME_T_MAX TYPE_MAXIMUM (time_t)
- #endif
- #define TIME_T_MIDPOINT (SHR (TIME_T_MIN + TIME_T_MAX, 1) + 1)
- verify (time_t_is_integer, TYPE_IS_INTEGER (time_t));
- verify (twos_complement_arithmetic,
- (TYPE_TWOS_COMPLEMENT (int)
- && TYPE_TWOS_COMPLEMENT (long_int)
- && TYPE_TWOS_COMPLEMENT (time_t)));
- #define EPOCH_YEAR 1970
- #define TM_YEAR_BASE 1900
- verify (base_year_is_a_multiple_of_100, TM_YEAR_BASE % 100 == 0);
- /* Return 1 if YEAR + TM_YEAR_BASE is a leap year. */
- static inline int
- leapyear (long_int year)
- {
- /* Don't add YEAR to TM_YEAR_BASE, as that might overflow.
- Also, work even if YEAR is negative. */
- return
- ((year & 3) == 0
- && (year % 100 != 0
- || ((year / 100) & 3) == (- (TM_YEAR_BASE / 100) & 3)));
- }
- /* How many days come before each month (0-12). */
- #ifndef _LIBC
- static
- #endif
- const unsigned short int __mon_yday[2][13] =
- {
- /* Normal years. */
- { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
- /* Leap years. */
- { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
- };
- #ifndef _LIBC
- /* Portable standalone applications should supply a <time.h> that
- declares a POSIX-compliant localtime_r, for the benefit of older
- implementations that lack localtime_r or have a nonstandard one.
- See the gnulib time_r module for one way to implement this. */
- # undef __localtime_r
- # define __localtime_r localtime_r
- # define __mktime_internal mktime_internal
- # include "mktime-internal.h"
- #endif
- static int
- isdst_differ (int a, int b)
- {
- return (!a != !b) & (0 <= a) & (0 <= b);
- }
- static inline time_t
- ydhms_diff (long_int year1, long_int yday1, int hour1, int min1, int sec1,
- int year0, int yday0, int hour0, int min0, int sec0)
- {
- verify (C99_integer_division, -1 / 2 == 0);
-
- int a4 = SHR (year1, 2) + SHR (TM_YEAR_BASE, 2) - ! (year1 & 3);
- int b4 = SHR (year0, 2) + SHR (TM_YEAR_BASE, 2) - ! (year0 & 3);
- int a100 = a4 / 25 - (a4 % 25 < 0);
- int b100 = b4 / 25 - (b4 % 25 < 0);
- int a400 = SHR (a100, 2);
- int b400 = SHR (b100, 2);
- int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
-
- time_t tyear1 = year1;
- time_t years = tyear1 - year0;
- time_t days = 365 * years + yday1 - yday0 + intervening_leap_days;
- time_t hours = 24 * days + hour1 - hour0;
- time_t minutes = 60 * hours + min1 - min0;
- time_t seconds = 60 * minutes + sec1 - sec0;
- return seconds;
- }
- static time_t
- time_t_avg (time_t a, time_t b)
- {
- return SHR (a, 1) + SHR (b, 1) + (a & b & 1);
- }
- static int
- time_t_add_ok (time_t a, time_t b)
- {
- if (! TYPE_SIGNED (time_t))
- {
- time_t sum = a + b;
- return (sum < a) == (TIME_T_MIDPOINT <= b);
- }
- else if (WRAPV)
- {
- time_t sum = a + b;
- return (sum < a) == (b < 0);
- }
- else
- {
- time_t avg = time_t_avg (a, b);
- return TIME_T_MIN / 2 <= avg && avg <= TIME_T_MAX / 2;
- }
- }
- static int
- time_t_int_add_ok (time_t a, int b)
- {
- verify (int_no_wider_than_time_t, INT_MAX <= TIME_T_MAX);
- if (WRAPV)
- {
- time_t sum = a + b;
- return (sum < a) == (b < 0);
- }
- else
- {
- int a_odd = a & 1;
- time_t avg = SHR (a, 1) + (SHR (b, 1) + (a_odd & b));
- return TIME_T_MIN / 2 <= avg && avg <= TIME_T_MAX / 2;
- }
- }
- static time_t
- guess_time_tm (long_int year, long_int yday, int hour, int min, int sec,
- const time_t *t, const struct tm *tp)
- {
- if (tp)
- {
- time_t d = ydhms_diff (year, yday, hour, min, sec,
- tp->tm_year, tp->tm_yday,
- tp->tm_hour, tp->tm_min, tp->tm_sec);
- if (time_t_add_ok (*t, d))
- return *t + d;
- }
-
- return (*t < TIME_T_MIDPOINT
- ? (*t <= TIME_T_MIN + 1 ? *t + 1 : TIME_T_MIN)
- : (TIME_T_MAX - 1 <= *t ? *t - 1 : TIME_T_MAX));
- }
- static struct tm *
- ranged_convert (struct tm *(*convert) (const time_t *, struct tm *),
- time_t *t, struct tm *tp)
- {
- struct tm *r = convert (t, tp);
- if (!r && *t)
- {
- time_t bad = *t;
- time_t ok = 0;
-
- while (bad != ok + (bad < 0 ? -1 : 1))
- {
- time_t mid = *t = time_t_avg (ok, bad);
- r = convert (t, tp);
- if (r)
- ok = mid;
- else
- bad = mid;
- }
- if (!r && ok)
- {
-
- *t = ok;
- r = convert (t, tp);
- }
- }
- return r;
- }
- time_t
- __mktime_internal (struct tm *tp,
- struct tm *(*convert) (const time_t *, struct tm *),
- time_t *offset)
- {
- time_t t, gt, t0, t1, t2;
- struct tm tm;
-
- int remaining_probes = 6;
-
- int sec = tp->tm_sec;
- int min = tp->tm_min;
- int hour = tp->tm_hour;
- int mday = tp->tm_mday;
- int mon = tp->tm_mon;
- int year_requested = tp->tm_year;
- int isdst = tp->tm_isdst;
-
- int dst2;
-
- int mon_remainder = mon % 12;
- int negative_mon_remainder = mon_remainder < 0;
- int mon_years = mon / 12 - negative_mon_remainder;
- long_int lyear_requested = year_requested;
- long_int year = lyear_requested + mon_years;
-
-
- int mon_yday = ((__mon_yday[leapyear (year)]
- [mon_remainder + 12 * negative_mon_remainder])
- - 1);
- long_int lmday = mday;
- long_int yday = mon_yday + lmday;
- time_t guessed_offset = *offset;
- int sec_requested = sec;
- if (LEAP_SECONDS_POSSIBLE)
- {
-
- if (sec < 0)
- sec = 0;
- if (59 < sec)
- sec = 59;
- }
-
- t0 = ydhms_diff (year, yday, hour, min, sec,
- EPOCH_YEAR - TM_YEAR_BASE, 0, 0, 0, - guessed_offset);
- if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3)
- {
-
-
- int ALOG2_SECONDS_PER_BIENNIUM = 26;
- int ALOG2_MINUTES_PER_BIENNIUM = 20;
- int ALOG2_HOURS_PER_BIENNIUM = 14;
- int ALOG2_DAYS_PER_BIENNIUM = 10;
- int LOG2_YEARS_PER_BIENNIUM = 1;
- int approx_requested_biennia =
- (SHR (year_requested, LOG2_YEARS_PER_BIENNIUM)
- - SHR (EPOCH_YEAR - TM_YEAR_BASE, LOG2_YEARS_PER_BIENNIUM)
- + SHR (mday, ALOG2_DAYS_PER_BIENNIUM)
- + SHR (hour, ALOG2_HOURS_PER_BIENNIUM)
- + SHR (min, ALOG2_MINUTES_PER_BIENNIUM)
- + (LEAP_SECONDS_POSSIBLE
- ? 0
- : SHR (sec, ALOG2_SECONDS_PER_BIENNIUM)));
- int approx_biennia = SHR (t0, ALOG2_SECONDS_PER_BIENNIUM);
- int diff = approx_biennia - approx_requested_biennia;
- int abs_diff = diff < 0 ? -1 - diff : diff;
-
- time_t time_t_max = TIME_T_MAX;
- time_t time_t_min = TIME_T_MIN;
- time_t overflow_threshold =
- (time_t_max / 3 - time_t_min / 3) >> ALOG2_SECONDS_PER_BIENNIUM;
- if (overflow_threshold < abs_diff)
- {
-
- time_t repaired_t0 = -1 - t0;
- approx_biennia = SHR (repaired_t0, ALOG2_SECONDS_PER_BIENNIUM);
- diff = approx_biennia - approx_requested_biennia;
- abs_diff = diff < 0 ? -1 - diff : diff;
- if (overflow_threshold < abs_diff)
- return -1;
- guessed_offset += repaired_t0 - t0;
- t0 = repaired_t0;
- }
- }
-
- for (t = t1 = t2 = t0, dst2 = 0;
- (gt = guess_time_tm (year, yday, hour, min, sec, &t,
- ranged_convert (convert, &t, &tm)),
- t != gt);
- t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0)
- if (t == t1 && t != t2
- && (tm.tm_isdst < 0
- || (isdst < 0
- ? dst2 <= (tm.tm_isdst != 0)
- : (isdst != 0) != (tm.tm_isdst != 0))))
-
- goto offset_found;
- else if (--remaining_probes == 0)
- return -1;
-
- if (isdst_differ (isdst, tm.tm_isdst))
- {
-
-
- int stride = 601200;
-
- int duration_max = 536454000;
-
- int delta_bound = duration_max / 2 + stride;
- int delta, direction;
- for (delta = stride; delta < delta_bound; delta += stride)
- for (direction = -1; direction <= 1; direction += 2)
- if (time_t_int_add_ok (t, delta * direction))
- {
- time_t ot = t + delta * direction;
- struct tm otm;
- ranged_convert (convert, &ot, &otm);
- if (! isdst_differ (isdst, otm.tm_isdst))
- {
-
- t = guess_time_tm (year, yday, hour, min, sec, &ot, &otm);
- ranged_convert (convert, &t, &tm);
- goto offset_found;
- }
- }
- }
- offset_found:
- *offset = guessed_offset + t - t0;
- if (LEAP_SECONDS_POSSIBLE && sec_requested != tm.tm_sec)
- {
-
- int sec_adjustment = (sec == 0 && tm.tm_sec == 60) - sec;
- if (! time_t_int_add_ok (t, sec_requested))
- return -1;
- t1 = t + sec_requested;
- if (! time_t_int_add_ok (t1, sec_adjustment))
- return -1;
- t2 = t1 + sec_adjustment;
- if (! convert (&t2, &tm))
- return -1;
- t = t2;
- }
- *tp = tm;
- return t;
- }
- static time_t localtime_offset;
- time_t
- mktime (struct tm *tp)
- {
- #ifdef _LIBC
-
- __tzset ();
- #endif
- return __mktime_internal (tp, __localtime_r, &localtime_offset);
- }
- #ifdef weak_alias
- weak_alias (mktime, timelocal)
- #endif
- #ifdef _LIBC
- libc_hidden_def (mktime)
- libc_hidden_weak (timelocal)
- #endif
- #if DEBUG
- static int
- not_equal_tm (const struct tm *a, const struct tm *b)
- {
- return ((a->tm_sec ^ b->tm_sec)
- | (a->tm_min ^ b->tm_min)
- | (a->tm_hour ^ b->tm_hour)
- | (a->tm_mday ^ b->tm_mday)
- | (a->tm_mon ^ b->tm_mon)
- | (a->tm_year ^ b->tm_year)
- | (a->tm_yday ^ b->tm_yday)
- | isdst_differ (a->tm_isdst, b->tm_isdst));
- }
- static void
- print_tm (const struct tm *tp)
- {
- if (tp)
- printf ("%04d-%02d-%02d %02d:%02d:%02d yday %03d wday %d isdst %d",
- tp->tm_year + TM_YEAR_BASE, tp->tm_mon + 1, tp->tm_mday,
- tp->tm_hour, tp->tm_min, tp->tm_sec,
- tp->tm_yday, tp->tm_wday, tp->tm_isdst);
- else
- printf ("0");
- }
- static int
- check_result (time_t tk, struct tm tmk, time_t tl, const struct tm *lt)
- {
- if (tk != tl || !lt || not_equal_tm (&tmk, lt))
- {
- printf ("mktime (");
- print_tm (lt);
- printf (")\nyields (");
- print_tm (&tmk);
- printf (") == %ld, should be %ld\n", (long int) tk, (long int) tl);
- return 1;
- }
- return 0;
- }
- int
- main (int argc, char **argv)
- {
- int status = 0;
- struct tm tm, tmk, tml;
- struct tm *lt;
- time_t tk, tl, tl1;
- char trailer;
- if ((argc == 3 || argc == 4)
- && (sscanf (argv[1], "%d-%d-%d%c",
- &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &trailer)
- == 3)
- && (sscanf (argv[2], "%d:%d:%d%c",
- &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &trailer)
- == 3))
- {
- tm.tm_year -= TM_YEAR_BASE;
- tm.tm_mon--;
- tm.tm_isdst = argc == 3 ? -1 : atoi (argv[3]);
- tmk = tm;
- tl = mktime (&tmk);
- lt = localtime (&tl);
- if (lt)
- {
- tml = *lt;
- lt = &tml;
- }
- printf ("mktime returns %ld == ", (long int) tl);
- print_tm (&tmk);
- printf ("\n");
- status = check_result (tl, tmk, tl, lt);
- }
- else if (argc == 4 || (argc == 5 && strcmp (argv[4], "-") == 0))
- {
- time_t from = atol (argv[1]);
- time_t by = atol (argv[2]);
- time_t to = atol (argv[3]);
- if (argc == 4)
- for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1)
- {
- lt = localtime (&tl);
- if (lt)
- {
- tmk = tml = *lt;
- tk = mktime (&tmk);
- status |= check_result (tk, tmk, tl, &tml);
- }
- else
- {
- printf ("localtime (%ld) yields 0\n", (long int) tl);
- status = 1;
- }
- tl1 = tl + by;
- if ((tl1 < tl) != (by < 0))
- break;
- }
- else
- for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1)
- {
-
- lt = localtime (&tl);
- if (lt)
- {
- tmk = tml = *lt;
- tk = tl;
- status |= check_result (tk, tmk, tl, &tml);
- }
- else
- {
- printf ("localtime (%ld) yields 0\n", (long int) tl);
- status = 1;
- }
- tl1 = tl + by;
- if ((tl1 < tl) != (by < 0))
- break;
- }
- }
- else
- printf ("Usage:\
- \t%s YYYY-MM-DD HH:MM:SS [ISDST] # Test given time.\n\
- \t%s FROM BY TO # Test values FROM, FROM+BY, ..., TO.\n\
- \t%s FROM BY TO - # Do not test those values (for benchmark).\n",
- argv[0], argv[0], argv[0]);
- return status;
- }
- #endif
|