tst-timezone.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. The GNU C Library is free software; you can redistribute it and/or
  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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <time.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.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. { "Asia/Tokyo", 1, -32400, {"JST", "JDT" }},
  40. { "Pacific/Auckland", 1, -43200, { "NZST", "NZDT" }},
  41. { NULL, 0, 0 }
  42. };
  43. /* This string will be used for `putenv' calls. */
  44. char envstring[100];
  45. static void
  46. print_tzvars (void)
  47. {
  48. printf ("tzname[0]: %s\n", tzname[0]);
  49. printf ("tzname[1]: %s\n", tzname[1]);
  50. printf ("daylight: %d\n", daylight);
  51. printf ("timezone: %ld\n", timezone);
  52. }
  53. static void
  54. check_tzvars (const char *name, int dayl, int timez, const char *const tznam[])
  55. {
  56. int i;
  57. if (daylight != dayl)
  58. {
  59. printf ("*** Timezone: %s, daylight is: %d but should be: %d\n",
  60. name, daylight, dayl);
  61. ++failed;
  62. }
  63. if (timezone != timez)
  64. {
  65. printf ("*** Timezone: %s, timezone is: %ld but should be: %d\n",
  66. name, timezone, timez);
  67. ++failed;
  68. }
  69. for (i = 0; i <= 1; ++i)
  70. if (strcmp (tzname[i], tznam[i]) != 0)
  71. {
  72. printf ("*** Timezone: %s, tzname[%d] is: %s but should be: %s\n",
  73. name, i, tzname[i], tznam[i]);
  74. ++failed;
  75. }
  76. }
  77. int
  78. main (int argc, char ** argv)
  79. {
  80. time_t t;
  81. const struct test_times *pt;
  82. char buf[BUFSIZ];
  83. /* This should be: Fri May 15 01:02:16 1998 (UTC). */
  84. t = 895194136;
  85. printf ("We use this date: %s\n", asctime (gmtime (&t)));
  86. for (pt = tests; pt->name != NULL; ++pt)
  87. {
  88. /* Start with a known state */
  89. printf ("Checking timezone %s\n", pt->name);
  90. sprintf (buf, "TZ=%s", pt->name);
  91. if (putenv (buf))
  92. {
  93. puts ("putenv failed.");
  94. failed = 1;
  95. }
  96. tzset ();
  97. print_tzvars ();
  98. check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
  99. /* calling localtime shouldn't make a difference */
  100. localtime (&t);
  101. print_tzvars ();
  102. check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
  103. }
  104. /* From a post of Scott Harrington <seh4@ix.netcom.com> to the timezone
  105. mailing list. */
  106. {
  107. struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
  108. char buf[200];
  109. strcpy (envstring, "TZ=Europe/London");
  110. putenv (envstring);
  111. t = mktime (&tmBuf);
  112. snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
  113. getenv ("TZ"), t,
  114. tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
  115. tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
  116. tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
  117. fputs (buf, stdout);
  118. puts (" should be");
  119. puts ("TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1");
  120. if (strcmp (buf, "TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1") != 0)
  121. {
  122. failed = 1;
  123. fputs (" FAILED ***", stdout);
  124. }
  125. }
  126. printf("\n");
  127. {
  128. struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
  129. char buf[200];
  130. strcpy (envstring, "TZ=GMT");
  131. /* No putenv call needed! */
  132. t = mktime (&tmBuf);
  133. snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
  134. getenv ("TZ"), t,
  135. tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
  136. tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
  137. tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
  138. fputs (buf, stdout);
  139. puts (" should be");
  140. puts ("TZ=GMT 892166400 0 0 0 10 3 98 5 99 0");
  141. if (strcmp (buf, "TZ=GMT 892166400 0 0 0 10 3 98 5 99 0") != 0)
  142. {
  143. failed = 1;
  144. fputs (" FAILED ***", stdout);
  145. }
  146. }
  147. return failed ? EXIT_FAILURE : EXIT_SUCCESS;
  148. }