tst-timezone.c 4.7 KB

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