tst-timezone.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <time.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. int failed = 0;
  22. struct test_times
  23. {
  24. const char *name;
  25. int daylight;
  26. int timezone;
  27. const char *tzname[2];
  28. };
  29. static const struct test_times tests[] =
  30. {
  31. { "Europe/Amsterdam", 1, -3600, { "CET", "CEST" }},
  32. { "Europe/Berlin", 1, -3600, { "CET", "CEST" }},
  33. { "Europe/London", 1, 0, { "GMT", "BST" }},
  34. { "Universal", 0, 0, {"UTC", "UTC" }},
  35. { "Australia/Melbourne", 1, -36000, { "EST", "EST" }},
  36. { "America/Sao_Paulo", 1, 10800, {"BRT", "BRST" }},
  37. { "America/Chicago", 1, 21600, {"CST", "CDT" }},
  38. { "America/Indiana/Indianapolis", 1, 18000, {"EST", "EDT" }},
  39. { "America/Los_Angeles", 1, 28800, {"PST", "PDT" }},
  40. { "Asia/Tokyo", 1, -32400, {"JST", "JDT" }},
  41. { "Pacific/Auckland", 1, -43200, { "NZST", "NZDT" }},
  42. { NULL, 0, 0 }
  43. };
  44. /* This string will be used for `putenv' calls. */
  45. char envstring[100];
  46. static void
  47. print_tzvars (void)
  48. {
  49. printf ("tzname[0]: %s\n", tzname[0]);
  50. printf ("tzname[1]: %s\n", tzname[1]);
  51. printf ("daylight: %d\n", daylight);
  52. printf ("timezone: %ld\n", timezone);
  53. }
  54. static void
  55. check_tzvars (const char *name, int dayl, int timez, const char *const tznam[])
  56. {
  57. int i;
  58. if (daylight != dayl)
  59. {
  60. printf ("*** Timezone: %s, daylight is: %d but should be: %d\n",
  61. name, daylight, dayl);
  62. ++failed;
  63. }
  64. if (timezone != timez)
  65. {
  66. printf ("*** Timezone: %s, timezone is: %ld but should be: %d\n",
  67. name, timezone, timez);
  68. ++failed;
  69. }
  70. for (i = 0; i <= 1; ++i)
  71. if (strcmp (tzname[i], tznam[i]) != 0)
  72. {
  73. printf ("*** Timezone: %s, tzname[%d] is: %s but should be: %s\n",
  74. name, i, tzname[i], tznam[i]);
  75. ++failed;
  76. }
  77. }
  78. int
  79. main (int argc, char ** argv)
  80. {
  81. time_t t;
  82. const struct test_times *pt;
  83. char buf[BUFSIZ];
  84. /* This should be: Fri May 15 01:02:16 1998 (UTC). */
  85. t = 895194136;
  86. printf ("We use this date: %s\n", asctime (gmtime (&t)));
  87. for (pt = tests; pt->name != NULL; ++pt)
  88. {
  89. /* Start with a known state */
  90. printf ("Checking timezone %s\n", pt->name);
  91. sprintf (buf, "TZ=%s", pt->name);
  92. if (putenv (buf))
  93. {
  94. puts ("putenv failed.");
  95. failed = 1;
  96. }
  97. tzset ();
  98. print_tzvars ();
  99. check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
  100. /* calling localtime shouldn't make a difference */
  101. localtime (&t);
  102. print_tzvars ();
  103. check_tzvars (pt->name, pt->daylight, pt->timezone, pt->tzname);
  104. }
  105. /* From a post of Scott Harrington <seh4@ix.netcom.com> to the timezone
  106. mailing list. */
  107. {
  108. struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
  109. char buf[200];
  110. strcpy (envstring, "TZ=Europe/London");
  111. putenv (envstring);
  112. t = mktime (&tmBuf);
  113. snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
  114. getenv ("TZ"), t,
  115. tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
  116. tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
  117. tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
  118. fputs (buf, stdout);
  119. puts (" should be");
  120. puts ("TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1");
  121. if (strcmp (buf, "TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1") != 0)
  122. {
  123. failed = 1;
  124. fputs (" FAILED ***", stdout);
  125. }
  126. }
  127. printf("\n");
  128. {
  129. struct tm tmBuf = {0, 0, 0, 10, 3, 98, 0, 0, -1};
  130. char buf[200];
  131. strcpy (envstring, "TZ=GMT");
  132. /* No putenv call needed! */
  133. t = mktime (&tmBuf);
  134. snprintf (buf, sizeof (buf), "TZ=%s %ld %d %d %d %d %d %d %d %d %d",
  135. getenv ("TZ"), t,
  136. tmBuf.tm_sec, tmBuf.tm_min, tmBuf.tm_hour,
  137. tmBuf.tm_mday, tmBuf.tm_mon, tmBuf.tm_year,
  138. tmBuf.tm_wday, tmBuf.tm_yday, tmBuf.tm_isdst);
  139. fputs (buf, stdout);
  140. puts (" should be");
  141. puts ("TZ=GMT 892166400 0 0 0 10 3 98 5 99 0");
  142. if (strcmp (buf, "TZ=GMT 892166400 0 0 0 10 3 98 5 99 0") != 0)
  143. {
  144. failed = 1;
  145. fputs (" FAILED ***", stdout);
  146. }
  147. }
  148. return failed ? EXIT_FAILURE : EXIT_SUCCESS;
  149. }