tst-mktime.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. int
  6. main (void)
  7. {
  8. struct tm time_str, *tm;
  9. time_t t;
  10. char daybuf[20];
  11. int result;
  12. time_str.tm_year = 2001 - 1900;
  13. time_str.tm_mon = 7 - 1;
  14. time_str.tm_mday = 4;
  15. time_str.tm_hour = 0;
  16. time_str.tm_min = 0;
  17. time_str.tm_sec = 1;
  18. time_str.tm_isdst = -1;
  19. if (mktime (&time_str) == -1)
  20. {
  21. (void) puts ("-unknown-");
  22. result = 1;
  23. }
  24. else
  25. {
  26. (void) strftime (daybuf, sizeof (daybuf), "%A", &time_str);
  27. (void) puts (daybuf);
  28. result = strcmp (daybuf, "Wednesday") != 0;
  29. }
  30. setenv ("TZ", "EST+5", 1);
  31. #define EVENING69 1 * 60 * 60 + 2 * 60 + 29
  32. t = EVENING69;
  33. tm = localtime (&t);
  34. if (tm == NULL)
  35. {
  36. (void) puts ("localtime returned NULL");
  37. result = 1;
  38. }
  39. else
  40. {
  41. time_str = *tm;
  42. t = mktime (&time_str);
  43. if (t != EVENING69)
  44. {
  45. printf ("mktime returned %ld, expected %d\n",
  46. (long) t, EVENING69);
  47. result = 1;
  48. }
  49. else
  50. (void) puts ("Dec 31 1969 EST test passed");
  51. setenv ("TZ", "CET-1", 1);
  52. t = mktime (&time_str);
  53. #define EVENING69_CET (EVENING69 - (5 - -1) * 60 * 60)
  54. if (t != EVENING69_CET)
  55. {
  56. printf ("mktime returned %ld, expected %ld\n",
  57. (long) t, (long) EVENING69_CET);
  58. result = 1;
  59. }
  60. else
  61. (void) puts ("Dec 31 1969 CET test passed");
  62. }
  63. return result;
  64. }