Back to index

glibc  2.9
tst-timezone.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998, 1999, 2000, 2005, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Andreas Jaeger <aj@suse.de>, 1998.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <time.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 
00026 int failed = 0;
00027 
00028 struct test_times
00029 {
00030   const char *name;
00031   int daylight;
00032   int timezone;
00033   const char *tzname[2];
00034 };
00035 
00036 static const struct test_times tests[] =
00037 {
00038   { "Europe/Amsterdam", 1, -3600, { "CET", "CEST" }},
00039   { "Europe/Berlin", 1, -3600, { "CET", "CEST" }},
00040   { "Europe/London", 1, 0, { "GMT", "BST" }},
00041   { "Universal", 0, 0, {"UTC", "UTC" }},
00042   { "Australia/Melbourne", 1, -36000, { "EST", "EST" }},
00043   { "America/Sao_Paulo", 1, 10800, {"BRT", "BRST" }},
00044   { "America/Chicago", 1, 21600, {"CST", "CDT" }},
00045   { "America/Indiana/Indianapolis", 1, 18000, {"EST", "EDT" }},
00046   { "America/Los_Angeles", 1, 28800, {"PST", "PDT" }},
00047   { "Pacific/Auckland", 1, -43200, { "NZST", "NZDT" }},
00048   { NULL, 0, 0 }
00049 };
00050 
00051 /* This string will be used for `putenv' calls.  */
00052 char envstring[100];
00053 
00054 static void
00055 print_tzvars (void)
00056 {
00057   printf ("tzname[0]: %s\n", tzname[0]);
00058   printf ("tzname[1]: %s\n", tzname[1]);
00059   printf ("daylight: %d\n", daylight);
00060   printf ("timezone: %ld\n", timezone);
00061 }
00062 
00063 
00064 static void
00065 check_tzvars (const char *name, int dayl, int timez, const char *const tznam[])
00066 {
00067   int i;
00068 
00069   if (daylight != dayl)
00070     {
00071       printf ("*** Timezone: %s, daylight is: %d but should be: %d\n",
00072              name, daylight, dayl);
00073       ++failed;
00074     }
00075   if (timezone != timez)
00076     {
00077       printf ("*** Timezone: %s, timezone is: %ld but should be: %d\n",
00078              name, timezone, timez);
00079       ++failed;
00080     }
00081   for (i = 0; i <= 1; ++i)
00082     if (strcmp (tzname[i], tznam[i]) != 0)
00083       {
00084        printf ("*** Timezone: %s, tzname[%d] is: %s but should be: %s\n",
00085               name, i, tzname[i], tznam[i]);
00086        ++failed;
00087       }
00088 }
00089 
00090 
00091 int
00092 main (int argc, char ** argv)
00093 {
00094   time_t t;
00095   const struct test_times *pt;
00096   char buf[BUFSIZ];
00097 
00098   /* This should be: Fri May 15 01:02:16 1998 (UTC).  */
00099   t = 895194136;
00100   printf ("We use this date: %s\n", asctime (gmtime (&t)));
00101 
00102   for (pt = tests; pt->name != NULL; ++pt)
00103     {
00104       /* Start with a known state */
00105       printf ("Checking timezone %s\n", pt->name);
00106       sprintf (buf, "TZ=%s", pt->name);
00107       if (putenv (buf))
00108        {
00109          puts ("putenv failed.");
00110          failed = 1;
00111        }
00112       tzset ();
00113       print_tzvars ();
00114       check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
00115 
00116       /* calling localtime shouldn't make a difference */
00117       localtime (&t);
00118       print_tzvars ();
00119       check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
00120     }
00121 
00122   /* From a post of Scott Harrington <seh4@ix.netcom.com> to the timezone
00123      mailing list.  */
00124   {
00125     struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
00126     char buf[200];
00127     strcpy (envstring, "TZ=Europe/London");
00128     putenv (envstring);
00129     t = mktime (&tmBuf);
00130     snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
00131              getenv ("TZ"), t,
00132              tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
00133              tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
00134              tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
00135     fputs (buf, stdout);
00136     puts (" should be");
00137     puts ("TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1");
00138     if (strcmp (buf, "TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1") != 0)
00139       {
00140        failed = 1;
00141        fputs (" FAILED ***", stdout);
00142       }
00143   }
00144 
00145   printf("\n");
00146 
00147   {
00148     struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
00149     char buf[200];
00150     strcpy (envstring, "TZ=GMT");
00151     /* No putenv call needed!  */
00152     t = mktime (&tmBuf);
00153     snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
00154              getenv ("TZ"), t,
00155              tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
00156              tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
00157              tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
00158     fputs (buf, stdout);
00159     puts (" should be");
00160     puts ("TZ=GMT 892166400 0 0 0 10 3 98 5 99 0");
00161     if (strcmp (buf, "TZ=GMT 892166400 0 0 0 10 3 98 5 99 0") != 0)
00162       {
00163        failed = 1;
00164        fputs (" FAILED ***", stdout);
00165       }
00166   }
00167 
00168   return failed ? EXIT_FAILURE : EXIT_SUCCESS;
00169 }