tst-posixtz.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. struct
  6. {
  7. time_t when;
  8. const char *tz;
  9. const char *result;
  10. } tests[] =
  11. {
  12. { 909312849L, "AEST-10AEDST-11,M10.5.0,M3.5.0",
  13. "1998/10/25 21:54:09 dst=1 zone=AEDST" },
  14. { 924864849L, "AEST-10AEDST-11,M10.5.0,M3.5.0",
  15. "1999/04/23 20:54:09 dst=0 zone=AEST" },
  16. { 919973892L, "AEST-10AEDST-11,M10.5.0,M3.5.0",
  17. "1999/02/26 07:18:12 dst=1 zone=AEDST" },
  18. { 909312849L, "EST+5EDT,M4.1.0/2,M10.5.0/2",
  19. "1998/10/25 05:54:09 dst=0 zone=EST" },
  20. { 924864849L, "EST+5EDT,M4.1.0/2,M10.5.0/2",
  21. "1999/04/23 06:54:09 dst=1 zone=EDT" },
  22. { 919973892L, "EST+5EDT,M4.1.0/2,M10.5.0/2",
  23. "1999/02/25 15:18:12 dst=0 zone=EST" },
  24. };
  25. int
  26. main (void)
  27. {
  28. int result = 0;
  29. size_t cnt;
  30. for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt)
  31. {
  32. char buf[100];
  33. struct tm *tmp;
  34. printf ("TZ = \"%s\", time = %ld => ", tests[cnt].tz, tests[cnt].when);
  35. fflush (stdout);
  36. setenv ("TZ", tests[cnt].tz, 1);
  37. tmp = localtime (&tests[cnt].when);
  38. snprintf (buf, sizeof (buf),
  39. "%04d/%02d/%02d %02d:%02d:%02d dst=%d zone=%s",
  40. tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
  41. tmp->tm_hour, tmp->tm_min, tmp->tm_sec, tmp->tm_isdst,
  42. tzname[tmp->tm_isdst ? 1 : 0]);
  43. fputs (buf, stdout);
  44. if (strcmp (buf, tests[cnt].result) == 0)
  45. puts (", OK");
  46. else
  47. {
  48. result = 1;
  49. puts (", FAIL");
  50. }
  51. }
  52. setenv ("TZ", "Universal", 1);
  53. localtime (&tests[0].when);
  54. printf ("TZ = \"Universal\" daylight %d tzname = { \"%s\", \"%s\" }",
  55. daylight, tzname[0], tzname[1]);
  56. if (! daylight)
  57. puts (", OK");
  58. else
  59. {
  60. result = 1;
  61. puts (", FAIL");
  62. }
  63. setenv ("TZ", "AEST-10AEDST-11,M10.5.0,M3.5.0", 1);
  64. tzset ();
  65. printf ("TZ = \"AEST-10AEDST-11,M10.5.0,M3.5.0\" daylight %d"
  66. " tzname = { \"%s\", \"%s\" }", daylight, tzname[0], tzname[1]);
  67. if (daylight
  68. && strcmp (tzname[0], "AEST") == 0 && strcmp (tzname[1], "AEDST") == 0)
  69. puts (", OK");
  70. else
  71. {
  72. result = 1;
  73. puts (", FAIL");
  74. }
  75. return result;
  76. }