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