Back to index

glibc  2.9
tst-mktime.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <time.h>
00005 
00006 int
00007 main (void)
00008 {
00009   struct tm time_str, *tm;
00010   time_t t;
00011   char daybuf[20];
00012   int result;
00013 
00014   time_str.tm_year = 2001 - 1900;
00015   time_str.tm_mon = 7 - 1;
00016   time_str.tm_mday = 4;
00017   time_str.tm_hour = 0;
00018   time_str.tm_min = 0;
00019   time_str.tm_sec = 1;
00020   time_str.tm_isdst = -1;
00021 
00022   if (mktime (&time_str) == -1)
00023     {
00024       (void) puts ("-unknown-");
00025       result = 1;
00026     }
00027   else
00028     {
00029       (void) strftime (daybuf, sizeof (daybuf), "%A", &time_str);
00030       (void) puts (daybuf);
00031       result = strcmp (daybuf, "Wednesday") != 0;
00032     }
00033 
00034   setenv ("TZ", "EST+5", 1);
00035 #define EVENING69 1 * 60 * 60 + 2 * 60 + 29
00036   t = EVENING69;
00037   tm = localtime (&t);
00038   if (tm == NULL)
00039     {
00040       (void) puts ("localtime returned NULL");
00041       result = 1;
00042     }
00043   else
00044     {
00045       time_str = *tm;
00046       t = mktime (&time_str);
00047       if (t != EVENING69)
00048         {
00049           printf ("mktime returned %ld, expected %d\n",
00050                 (long) t, EVENING69);
00051          result = 1;
00052         }
00053       else
00054         (void) puts ("Dec 31 1969 EST test passed");
00055 
00056       setenv ("TZ", "CET-1", 1);
00057       t = mktime (&time_str);
00058 #define EVENING69_CET (EVENING69 - (5 - -1) * 60 * 60)
00059       if (t != EVENING69_CET)
00060         {
00061          printf ("mktime returned %ld, expected %ld\n",
00062                 (long) t, (long) EVENING69_CET);
00063          result = 1;
00064         }
00065       else
00066         (void) puts ("Dec 31 1969 CET test passed");
00067     }
00068 
00069   return result;
00070 }