]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 1998-2019 Free Software Foundation, Inc. |
ff152e3f | 2 | This file is part of the GNU C Library. |
de149cdb | 3 | Contributed by Andreas Jaeger <aj@suse.de>, 1998. |
ff152e3f UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
ff152e3f UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
ff152e3f | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
ff152e3f UD |
18 | |
19 | #include <time.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
ba9234d9 | 22 | #include <string.h> |
ceaa9889 | 23 | #include <stdint.h> |
ff152e3f UD |
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; | |
ba9234d9 | 33 | const char *tzname[2]; |
ff152e3f UD |
34 | }; |
35 | ||
36 | static const struct test_times tests[] = | |
37 | { | |
72188776 | 38 | { "Europe/Amsterdam", 1, -3600, { "CET", "CEST" }}, |
ba9234d9 | 39 | { "Europe/Berlin", 1, -3600, { "CET", "CEST" }}, |
72188776 | 40 | { "Europe/London", 1, 0, { "GMT", "BST" }}, |
ba9234d9 UD |
41 | { "Universal", 0, 0, {"UTC", "UTC" }}, |
42 | { "Australia/Melbourne", 1, -36000, { "EST", "EST" }}, | |
b43f26bb | 43 | { "America/Sao_Paulo", 1, 10800, {"BRT", "BRST" }}, |
a3e0e9ae | 44 | { "America/Chicago", 1, 21600, {"CST", "CDT" }}, |
3aeb7ee1 | 45 | { "America/Indiana/Indianapolis", 1, 18000, {"EST", "EDT" }}, |
ba9234d9 | 46 | { "America/Los_Angeles", 1, 28800, {"PST", "PDT" }}, |
72188776 | 47 | { "Pacific/Auckland", 1, -43200, { "NZST", "NZDT" }}, |
ff152e3f UD |
48 | { NULL, 0, 0 } |
49 | }; | |
50 | ||
1e275b9e UD |
51 | /* This string will be used for `putenv' calls. */ |
52 | char envstring[100]; | |
ff152e3f | 53 | |
de149cdb | 54 | static void |
ff152e3f UD |
55 | print_tzvars (void) |
56 | { | |
57 | printf ("tzname[0]: %s\n", tzname[0]); | |
58 | printf ("tzname[1]: %s\n", tzname[1]); | |
59 | printf ("daylight: %d\n", daylight); | |
60 | printf ("timezone: %ld\n", timezone); | |
61 | } | |
62 | ||
63 | ||
de149cdb | 64 | static void |
ba9234d9 | 65 | check_tzvars (const char *name, int dayl, int timez, const char *const tznam[]) |
ff152e3f | 66 | { |
ba9234d9 UD |
67 | int i; |
68 | ||
ff152e3f UD |
69 | if (daylight != dayl) |
70 | { | |
72188776 | 71 | printf ("*** Timezone: %s, daylight is: %d but should be: %d\n", |
ff152e3f UD |
72 | name, daylight, dayl); |
73 | ++failed; | |
74 | } | |
75 | if (timezone != timez) | |
76 | { | |
72188776 | 77 | printf ("*** Timezone: %s, timezone is: %ld but should be: %d\n", |
ff152e3f UD |
78 | name, timezone, timez); |
79 | ++failed; | |
80 | } | |
ba9234d9 UD |
81 | for (i = 0; i <= 1; ++i) |
82 | if (strcmp (tzname[i], tznam[i]) != 0) | |
83 | { | |
72188776 | 84 | printf ("*** Timezone: %s, tzname[%d] is: %s but should be: %s\n", |
ba9234d9 UD |
85 | name, i, tzname[i], tznam[i]); |
86 | ++failed; | |
87 | } | |
ff152e3f UD |
88 | } |
89 | ||
90 | ||
c1f41083 AS |
91 | static int |
92 | do_test (void) | |
ff152e3f UD |
93 | { |
94 | time_t t; | |
95 | const struct test_times *pt; | |
96 | char buf[BUFSIZ]; | |
97 | ||
7ef90c15 | 98 | /* This should be: Fri May 15 01:02:16 1998 (UTC). */ |
ff152e3f | 99 | t = 895194136; |
7ef90c15 | 100 | printf ("We use this date: %s\n", asctime (gmtime (&t))); |
ff152e3f UD |
101 | |
102 | for (pt = tests; pt->name != NULL; ++pt) | |
103 | { | |
104 | /* Start with a known state */ | |
105 | printf ("Checking timezone %s\n", pt->name); | |
106 | sprintf (buf, "TZ=%s", pt->name); | |
107 | if (putenv (buf)) | |
108 | { | |
109 | puts ("putenv failed."); | |
110 | failed = 1; | |
111 | } | |
112 | tzset (); | |
113 | print_tzvars (); | |
ba9234d9 | 114 | check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname); |
ff152e3f UD |
115 | |
116 | /* calling localtime shouldn't make a difference */ | |
117 | localtime (&t); | |
118 | print_tzvars (); | |
ba9234d9 | 119 | check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname); |
ff152e3f UD |
120 | } |
121 | ||
390955cb | 122 | /* From a post of Scott Harrington <seh4@ix.netcom.com> to the timezone |
a3e0e9ae UD |
123 | mailing list. */ |
124 | { | |
125 | struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1}; | |
126 | char buf[200]; | |
1e275b9e UD |
127 | strcpy (envstring, "TZ=Europe/London"); |
128 | putenv (envstring); | |
a3e0e9ae | 129 | t = mktime (&tmBuf); |
126f6c72 L |
130 | snprintf (buf, sizeof (buf), "TZ=%s %jd %d %d %d %d %d %d %d %d %d", |
131 | getenv ("TZ"), (intmax_t) t, | |
a3e0e9ae UD |
132 | tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour, |
133 | tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year, | |
134 | tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst); | |
135 | fputs (buf, stdout); | |
136 | puts (" should be"); | |
390955cb | 137 | puts ("TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1"); |
72188776 UD |
138 | if (strcmp (buf, "TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1") != 0) |
139 | { | |
140 | failed = 1; | |
141 | fputs (" FAILED ***", stdout); | |
142 | } | |
a3e0e9ae UD |
143 | } |
144 | ||
145 | printf("\n"); | |
146 | ||
147 | { | |
148 | struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1}; | |
149 | char buf[200]; | |
1e275b9e UD |
150 | strcpy (envstring, "TZ=GMT"); |
151 | /* No putenv call needed! */ | |
a3e0e9ae | 152 | t = mktime (&tmBuf); |
126f6c72 L |
153 | snprintf (buf, sizeof (buf), "TZ=%s %jd %d %d %d %d %d %d %d %d %d", |
154 | getenv ("TZ"), (intmax_t) t, | |
a3e0e9ae UD |
155 | tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour, |
156 | tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year, | |
157 | tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst); | |
158 | fputs (buf, stdout); | |
159 | puts (" should be"); | |
160 | puts ("TZ=GMT 892166400 0 0 0 10 3 98 5 99 0"); | |
72188776 UD |
161 | if (strcmp (buf, "TZ=GMT 892166400 0 0 0 10 3 98 5 99 0") != 0) |
162 | { | |
163 | failed = 1; | |
164 | fputs (" FAILED ***", stdout); | |
165 | } | |
a3e0e9ae | 166 | } |
a3e0e9ae | 167 | |
ff152e3f UD |
168 | return failed ? EXIT_FAILURE : EXIT_SUCCESS; |
169 | } | |
c1f41083 AS |
170 | |
171 | #define TEST_FUNCTION do_test () | |
172 | #include "../test-skeleton.c" |