Back to index

glibc  2.9
tst-mktime2.c
Go to the documentation of this file.
00001 /* Test program from Paul Eggert and Tony Leneis.  */
00002 #include <time.h>
00003 #include <stdlib.h>
00004 #include <unistd.h>
00005 
00006 static time_t time_t_max;
00007 static time_t time_t_min;
00008 
00009 /* Values we'll use to set the TZ environment variable.  */
00010 static const char *tz_strings[] =
00011   {
00012     (const char *) 0, "GMT0", "JST-9",
00013     "EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
00014   };
00015 #define N_STRINGS ((int) (sizeof (tz_strings) / sizeof (tz_strings[0])))
00016 
00017 /* Fail if mktime fails to convert a date in the spring-forward gap.
00018    Based on a problem report from Andreas Jaeger.  */
00019 static void
00020 spring_forward_gap (void)
00021 {
00022   /* glibc (up to about 1998-10-07) failed this test. */
00023   struct tm tm;
00024 
00025   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
00026      instead of "TZ=America/Vancouver" in order to detect the bug even
00027      on systems that don't support the Olson extension, or don't have the
00028      full zoneinfo tables installed.  */
00029   setenv ("TZ", "PST8PDT,M4.1.0,M10.5.0", 1);
00030 
00031   tm.tm_year = 98;
00032   tm.tm_mon = 3;
00033   tm.tm_mday = 5;
00034   tm.tm_hour = 2;
00035   tm.tm_min = 0;
00036   tm.tm_sec = 0;
00037   tm.tm_isdst = -1;
00038   if (mktime (&tm) == (time_t)-1)
00039     exit (1);
00040 }
00041 
00042 static void
00043 mktime_test1 (time_t now)
00044 {
00045   struct tm *lt = localtime (&now);
00046   if (lt && mktime (lt) != now)
00047     exit (2);
00048 }
00049 
00050 static void
00051 mktime_test (time_t now)
00052 {
00053   mktime_test1 (now);
00054   mktime_test1 ((time_t) (time_t_max - now));
00055   mktime_test1 ((time_t) (time_t_min + now));
00056 }
00057 
00058 static void
00059 irix_6_4_bug (void)
00060 {
00061   /* Based on code from Ariel Faigon.  */
00062   struct tm tm;
00063   tm.tm_year = 96;
00064   tm.tm_mon = 3;
00065   tm.tm_mday = 0;
00066   tm.tm_hour = 0;
00067   tm.tm_min = 0;
00068   tm.tm_sec = 0;
00069   tm.tm_isdst = -1;
00070   mktime (&tm);
00071   if (tm.tm_mon != 2 || tm.tm_mday != 31)
00072     exit (3);
00073 }
00074 
00075 static void
00076 bigtime_test (int j)
00077 {
00078   struct tm tm;
00079   time_t now;
00080   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
00081   tm.tm_isdst = -1;
00082   now = mktime (&tm);
00083   if (now != (time_t) -1)
00084     {
00085       struct tm *lt = localtime (&now);
00086       if (! (lt
00087             && lt->tm_year == tm.tm_year
00088             && lt->tm_mon == tm.tm_mon
00089             && lt->tm_mday == tm.tm_mday
00090             && lt->tm_hour == tm.tm_hour
00091             && lt->tm_min == tm.tm_min
00092             && lt->tm_sec == tm.tm_sec
00093             && lt->tm_yday == tm.tm_yday
00094             && lt->tm_wday == tm.tm_wday
00095             && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
00096                 == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
00097        exit (4);
00098     }
00099 }
00100 
00101 static int
00102 do_test (void)
00103 {
00104   time_t t, delta;
00105   int i;
00106   unsigned int j;
00107 
00108   setenv ("TZ", "America/Sao_Paulo", 1);
00109   /* This test makes some buggy mktime implementations loop.
00110      Give up after 60 seconds; a mktime slower than that
00111      isn't worth using anyway.  */
00112   alarm (60);
00113 
00114   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
00115     continue;
00116   time_t_max--;
00117   if ((time_t) -1 < 0)
00118     for (time_t_min = -1; (time_t) (time_t_min * 2) < 0; time_t_min *= 2)
00119       continue;
00120   delta = time_t_max / 997; /* a suitable prime number */
00121   for (i = 0; i < N_STRINGS; i++)
00122     {
00123       if (tz_strings[i])
00124        setenv ("TZ", tz_strings[i], 1);
00125 
00126       for (t = 0; t <= time_t_max - delta; t += delta)
00127        mktime_test (t);
00128       mktime_test ((time_t) 1);
00129       mktime_test ((time_t) (60 * 60));
00130       mktime_test ((time_t) (60 * 60 * 24));
00131 
00132       for (j = 1; j <= INT_MAX; j *= 2)
00133        bigtime_test (j);
00134       bigtime_test (j - 1);
00135     }
00136   irix_6_4_bug ();
00137   spring_forward_gap ();
00138   return 0;
00139 }
00140 
00141 #define TEST_FUNCTION do_test ()
00142 #include "../test-skeleton.c"