]> git.ipfire.org Git - thirdparty/glibc.git/blob - timezone/tst-timezone.c
Update.
[thirdparty/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/Amsterdam", 1, -3600, { "CET", "CEST" }},
39 { "Europe/Berlin", 1, -3600, { "CET", "CEST" }},
40 { "Europe/London", 1, 0, { "GMT", "BST" }},
41 { "Universal", 0, 0, {"UTC", "UTC" }},
42 { "Australia/Melbourne", 1, -36000, { "EST", "EST" }},
43 { "America/Sao_Paulo", 1, 10800, {"BRT", "BRST" }},
44 { "America/Chicago", 1, 21600, {"CST", "CDT" }},
45 { "America/Indianapolis", 1, 18000, {"EST", "EDT" }},
46 { "America/Los_Angeles", 1, 28800, {"PST", "PDT" }},
47 { "Asia/Tokyo", 0, -32400, {"JST", "JST" }},
48 { "Pacific/Auckland", 1, -43200, { "NZST", "NZDT" }},
49 { NULL, 0, 0 }
50 };
51
52
53 void
54 print_tzvars (void)
55 {
56 printf ("tzname[0]: %s\n", tzname[0]);
57 printf ("tzname[1]: %s\n", tzname[1]);
58 printf ("daylight: %d\n", daylight);
59 printf ("timezone: %ld\n", timezone);
60 }
61
62
63 void
64 check_tzvars (const char *name, int dayl, int timez, const char *const tznam[])
65 {
66 int i;
67
68 if (daylight != dayl)
69 {
70 printf ("*** Timezone: %s, daylight is: %d but should be: %d\n",
71 name, daylight, dayl);
72 ++failed;
73 }
74 if (timezone != timez)
75 {
76 printf ("*** Timezone: %s, timezone is: %ld but should be: %d\n",
77 name, timezone, timez);
78 ++failed;
79 }
80 for (i = 0; i <= 1; ++i)
81 if (strcmp (tzname[i], tznam[i]) != 0)
82 {
83 printf ("*** Timezone: %s, tzname[%d] is: %s but should be: %s\n",
84 name, i, tzname[i], tznam[i]);
85 ++failed;
86 }
87 }
88
89
90 int
91 main (int argc, char ** argv)
92 {
93 time_t t;
94 const struct test_times *pt;
95 char buf[BUFSIZ];
96
97 /* This should be: Fri May 15 01:02:16 1998 (UTC). */
98 t = 895194136;
99 printf ("We use this date: %s\n", asctime (gmtime (&t)));
100
101 for (pt = tests; pt->name != NULL; ++pt)
102 {
103 /* Start with a known state */
104 printf ("Checking timezone %s\n", pt->name);
105 sprintf (buf, "TZ=%s", pt->name);
106 if (putenv (buf))
107 {
108 puts ("putenv failed.");
109 failed = 1;
110 }
111 tzset ();
112 print_tzvars ();
113 check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
114
115 /* calling localtime shouldn't make a difference */
116 localtime (&t);
117 print_tzvars ();
118 check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
119 }
120
121 /* From a post of Scott Harrington <seh4@ix.netcom.com> to the timezone
122 mailing list. */
123 {
124 struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
125 char buf[200];
126 putenv ("TZ=Europe/London");
127 t = mktime (&tmBuf);
128 snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
129 getenv ("TZ"), t,
130 tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
131 tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
132 tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
133 fputs (buf, stdout);
134 puts (" should be");
135 puts ("TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1");
136 if (strcmp (buf, "TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1") != 0)
137 {
138 failed = 1;
139 fputs (" FAILED ***", stdout);
140 }
141 }
142
143 printf("\n");
144
145 {
146 struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
147 char buf[200];
148 putenv ("TZ=GMT");
149 t = mktime (&tmBuf);
150 snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
151 getenv ("TZ"), t,
152 tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
153 tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
154 tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
155 fputs (buf, stdout);
156 puts (" should be");
157 puts ("TZ=GMT 892166400 0 0 0 10 3 98 5 99 0");
158 if (strcmp (buf, "TZ=GMT 892166400 0 0 0 10 3 98 5 99 0") != 0)
159 {
160 failed = 1;
161 fputs (" FAILED ***", stdout);
162 }
163 }
164
165 return failed ? EXIT_FAILURE : EXIT_SUCCESS;
166 }