tst-mktime3.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Test program for mktime bugs with out-of-range tm_sec values. */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. struct tm tests[] =
  6. {
  7. { .tm_sec = -1, .tm_mday = 1, .tm_year = 104 },
  8. { .tm_sec = 65, .tm_min = 59, .tm_hour = 23, .tm_mday = 31,
  9. .tm_mon = 11, .tm_year = 101 }
  10. };
  11. struct tm expected[] =
  12. {
  13. { .tm_sec = 59, .tm_min = 59, .tm_hour = 23, .tm_mday = 31,
  14. .tm_mon = 11, .tm_year = 103, .tm_wday = 3, .tm_yday = 364 },
  15. { .tm_sec = 5, .tm_mday = 1, .tm_year = 102, .tm_wday = 2 }
  16. };
  17. int
  18. main (void)
  19. {
  20. setenv ("TZ", "UTC", 1);
  21. int i;
  22. for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
  23. {
  24. if (mktime (&tests[i]) < 0)
  25. {
  26. printf ("mktime %d failed\n", i);
  27. return 1;
  28. }
  29. #define CHECK(name) \
  30. if (tests[i].name != expected[i].name) \
  31. { \
  32. printf ("test %d " #name " got %d expected %d\n", \
  33. i, tests[i].name, expected[i].name); \
  34. return 1; \
  35. }
  36. CHECK (tm_sec)
  37. CHECK (tm_min)
  38. CHECK (tm_hour)
  39. CHECK (tm_mday)
  40. CHECK (tm_mon)
  41. CHECK (tm_year)
  42. CHECK (tm_wday)
  43. CHECK (tm_yday)
  44. CHECK (tm_isdst)
  45. }
  46. return 0;
  47. }