lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1998, 1999, 2000, 2005 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Andreas Jaeger <aj@suse.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 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. |
| 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 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 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/Indiana/Indianapolis", 1, 18000, {"EST", "EDT" }}, |
| 46 | { "America/Los_Angeles", 1, 28800, {"PST", "PDT" }}, |
| 47 | { "Asia/Tokyo", 1, -32400, {"JST", "JDT" }}, |
| 48 | { "Pacific/Auckland", 1, -43200, { "NZST", "NZDT" }}, |
| 49 | { NULL, 0, 0 } |
| 50 | }; |
| 51 | |
| 52 | /* This string will be used for `putenv' calls. */ |
| 53 | char envstring[100]; |
| 54 | |
| 55 | static void |
| 56 | print_tzvars (void) |
| 57 | { |
| 58 | printf ("tzname[0]: %s\n", tzname[0]); |
| 59 | printf ("tzname[1]: %s\n", tzname[1]); |
| 60 | printf ("daylight: %d\n", daylight); |
| 61 | printf ("timezone: %ld\n", timezone); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static void |
| 66 | check_tzvars (const char *name, int dayl, int timez, const char *const tznam[]) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | if (daylight != dayl) |
| 71 | { |
| 72 | printf ("*** Timezone: %s, daylight is: %d but should be: %d\n", |
| 73 | name, daylight, dayl); |
| 74 | ++failed; |
| 75 | } |
| 76 | if (timezone != timez) |
| 77 | { |
| 78 | printf ("*** Timezone: %s, timezone is: %ld but should be: %d\n", |
| 79 | name, timezone, timez); |
| 80 | ++failed; |
| 81 | } |
| 82 | for (i = 0; i <= 1; ++i) |
| 83 | if (strcmp (tzname[i], tznam[i]) != 0) |
| 84 | { |
| 85 | printf ("*** Timezone: %s, tzname[%d] is: %s but should be: %s\n", |
| 86 | name, i, tzname[i], tznam[i]); |
| 87 | ++failed; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | int |
| 93 | main (int argc, char ** argv) |
| 94 | { |
| 95 | time_t t; |
| 96 | const struct test_times *pt; |
| 97 | char buf[BUFSIZ]; |
| 98 | |
| 99 | /* This should be: Fri May 15 01:02:16 1998 (UTC). */ |
| 100 | t = 895194136; |
| 101 | printf ("We use this date: %s\n", asctime (gmtime (&t))); |
| 102 | |
| 103 | for (pt = tests; pt->name != NULL; ++pt) |
| 104 | { |
| 105 | /* Start with a known state */ |
| 106 | printf ("Checking timezone %s\n", pt->name); |
| 107 | sprintf (buf, "TZ=%s", pt->name); |
| 108 | if (putenv (buf)) |
| 109 | { |
| 110 | puts ("putenv failed."); |
| 111 | failed = 1; |
| 112 | } |
| 113 | tzset (); |
| 114 | print_tzvars (); |
| 115 | check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname); |
| 116 | |
| 117 | /* calling localtime shouldn't make a difference */ |
| 118 | localtime (&t); |
| 119 | print_tzvars (); |
| 120 | check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname); |
| 121 | } |
| 122 | |
| 123 | /* From a post of Scott Harrington <seh4@ix.netcom.com> to the timezone |
| 124 | mailing list. */ |
| 125 | { |
| 126 | struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1}; |
| 127 | char buf[200]; |
| 128 | strcpy (envstring, "TZ=Europe/London"); |
| 129 | putenv (envstring); |
| 130 | t = mktime (&tmBuf); |
| 131 | snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d", |
| 132 | getenv ("TZ"), t, |
| 133 | tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour, |
| 134 | tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year, |
| 135 | tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst); |
| 136 | fputs (buf, stdout); |
| 137 | puts (" should be"); |
| 138 | puts ("TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1"); |
| 139 | if (strcmp (buf, "TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1") != 0) |
| 140 | { |
| 141 | failed = 1; |
| 142 | fputs (" FAILED ***", stdout); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | printf("\n"); |
| 147 | |
| 148 | { |
| 149 | struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1}; |
| 150 | char buf[200]; |
| 151 | strcpy (envstring, "TZ=GMT"); |
| 152 | /* No putenv call needed! */ |
| 153 | t = mktime (&tmBuf); |
| 154 | snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d", |
| 155 | getenv ("TZ"), t, |
| 156 | tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour, |
| 157 | tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year, |
| 158 | tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst); |
| 159 | fputs (buf, stdout); |
| 160 | puts (" should be"); |
| 161 | puts ("TZ=GMT 892166400 0 0 0 10 3 98 5 99 0"); |
| 162 | if (strcmp (buf, "TZ=GMT 892166400 0 0 0 10 3 98 5 99 0") != 0) |
| 163 | { |
| 164 | failed = 1; |
| 165 | fputs (" FAILED ***", stdout); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return failed ? EXIT_FAILURE : EXIT_SUCCESS; |
| 170 | } |