5588faaea481594d1f6576a355bdb6eff3782a30
[platform/upstream/glibc.git] / timezone / tst-timezone.c
1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Andreas Jaeger <aj@arthur.rhein-neckar.de>, 1998.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <time.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 int failed = 0;
27
28 struct test_times
29 {
30   const char *name;
31   int daylight;
32   int timezone;
33   const char *tzname[2];
34 };
35
36 static const struct test_times tests[] =
37 {
38   { "Europe/Berlin", 1, -3600, { "CET", "CEST" }},
39   { "Universal", 0, 0, {"UTC", "UTC" }},
40   { "Australia/Melbourne", 1, -36000, { "EST", "EST" }},
41   { "America/Sao_Paulo", 1, 10800, {"EST", "EDT" }},
42   { "America/Chicago", 1, 21600, {"CST", "CDT" }},
43   { "America/Los_Angeles", 1, 28800, {"PST", "PDT" }},
44   { "Asia/Tokyo", 0, -32400, {"JST", "JST" }},
45   { NULL, 0, 0 }
46 };
47
48
49 void
50 print_tzvars (void)
51 {
52   printf ("tzname[0]: %s\n", tzname[0]);
53   printf ("tzname[1]: %s\n", tzname[1]);
54   printf ("daylight: %d\n", daylight);
55   printf ("timezone: %ld\n", timezone);
56 }
57
58
59 void
60 check_tzvars (const char *name, int dayl, int timez, const char *const tznam[])
61 {
62   int i;
63
64   if (daylight != dayl)
65     {
66       printf ("Timezone: %s, daylight is: %d but should be: %d\n",
67               name, daylight, dayl);
68       ++failed;
69     }
70   if (timezone != timez)
71     {
72       printf ("Timezone: %s, timezone is: %ld but should be: %d\n",
73               name, timezone, timez);
74       ++failed;
75     }
76   for (i = 0; i <= 1; ++i)
77     if (strcmp (tzname[i], tznam[i]) != 0)
78       {
79         printf ("Timezone: %s, tzname[%d] is: %s but should be: %s\n",
80                 name, i, tzname[i], tznam[i]);
81         ++failed;
82       }
83 }
84
85
86 int
87 main (int argc, char ** argv)
88 {
89   time_t t;
90   const struct test_times *pt;
91   char buf[BUFSIZ];
92
93   /* This should be: Fri May 15 01:02:16 1998 (UTC).  */
94   t = 895194136;
95   printf ("We use this date: %s\n", asctime (gmtime (&t)));
96
97   for (pt = tests; pt->name != NULL; ++pt)
98     {
99       /* Start with a known state */
100       printf ("Checking timezone %s\n", pt->name);
101       sprintf (buf, "TZ=%s", pt->name);
102       if (putenv (buf))
103         {
104           puts ("putenv failed.");
105           failed = 1;
106         }
107       tzset ();
108       print_tzvars ();
109       check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
110
111       /* calling localtime shouldn't make a difference */
112       localtime (&t);
113       print_tzvars ();
114       check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
115     }
116
117   /* From a post of Scott Harrington <seh4@ix.netcom.com> to the timezone
118      mailing list.  */
119   {
120     struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
121     char buf[200];
122     putenv ("TZ=Europe/London");
123     t = mktime (&tmBuf);
124     snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
125               getenv ("TZ"), t,
126               tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
127               tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
128               tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
129     fputs (buf, stdout);
130     puts (" should be");
131     puts ("TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1");
132     failed |= strcmp (buf, "TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1") != 0;
133   }
134
135   printf("\n");
136
137   {
138     struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
139     char buf[200];
140     putenv ("TZ=GMT");
141     t = mktime (&tmBuf);
142     snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
143               getenv ("TZ"), t,
144               tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
145               tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
146               tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
147     fputs (buf, stdout);
148     puts (" should be");
149     puts ("TZ=GMT 892166400 0 0 0 10 3 98 5 99 0");
150     failed |= strcmp (buf, "TZ=GMT 892166400 0 0 0 10 3 98 5 99 0") != 0;
151   }
152
153   return failed ? EXIT_FAILURE : EXIT_SUCCESS;
154 }