tst-mktime4.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Test for mktime (4)
  2. Copyright (C) 2021-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  15. #include <time.h>
  16. #include <stdlib.h>
  17. #include <support/check.h>
  18. #if __GNUC_PREREQ (5, 0)
  19. const struct tm tm0 =
  20. {
  21. .tm_year = 70,
  22. .tm_mon = 0,
  23. .tm_mday = 1,
  24. .tm_hour = 0,
  25. .tm_min = 0,
  26. .tm_sec = 0,
  27. .tm_wday = 4,
  28. .tm_yday = 0,
  29. };
  30. const struct tm tmY2038 =
  31. {
  32. .tm_year = 138,
  33. .tm_mon = 0,
  34. .tm_mday = 19,
  35. .tm_hour = 3,
  36. .tm_min = 14,
  37. .tm_sec = 7,
  38. };
  39. const struct tm tm32bitmax =
  40. {
  41. .tm_year = 206,
  42. .tm_mon = 1,
  43. .tm_mday = 7,
  44. .tm_hour = 6,
  45. .tm_min = 28,
  46. .tm_sec = 15,
  47. };
  48. static
  49. int test_mktime_helper (struct tm *tm, long long int exp_val, int line)
  50. {
  51. time_t result, t;
  52. /* Check if we run on port with 32 bit time_t size. */
  53. if (__builtin_add_overflow (exp_val, 0, &t))
  54. return 0;
  55. result = mktime (tm);
  56. if (result == (time_t) -1)
  57. FAIL_RET ("*** mktime failed: %m in line: %d", line);
  58. if ((long long int) result != exp_val)
  59. FAIL_RET ("*** Result different than expected (%lld != %lld) in %d\n",
  60. (long long int) result, exp_val, line);
  61. return 0;
  62. }
  63. static int
  64. do_test (void)
  65. {
  66. struct tm t;
  67. /* Use glibc time zone extension "TZ=:" to to guarantee that UTC
  68. without leap seconds is used for the test. */
  69. TEST_VERIFY_EXIT (setenv ("TZ", ":", 1) == 0);
  70. /* Check that mktime (1970-01-01 00:00:00) returns 0. */
  71. t = tm0;
  72. test_mktime_helper (&t, 0, __LINE__);
  73. /* Check that mktime (2038-01-19 03:14:07) returns 0x7FFFFFFF. */
  74. t = tmY2038;
  75. test_mktime_helper (&t, 0x7fffffff, __LINE__);
  76. /* Check that mktime (2038-01-19 03:14:08) returns 0x80000000
  77. (time_t overflow). */
  78. t = tmY2038;
  79. t.tm_sec++;
  80. test_mktime_helper (&t, 0x80000000, __LINE__);
  81. /* Check that mktime (2106-02-07 06:28:15) returns 0xFFFFFFFF. */
  82. t = tm32bitmax;
  83. test_mktime_helper (&t, 0xFFFFFFFF, __LINE__);
  84. /* Check that mktime (2106-02-07 06:28:16) returns 0x100000000. */
  85. t = tm32bitmax;
  86. t.tm_sec++;
  87. test_mktime_helper (&t, 0x100000000, __LINE__);
  88. return 0;
  89. }
  90. #include <support/test-driver.c>
  91. #else
  92. #include <support/test-driver.h>
  93. int
  94. main (void)
  95. {
  96. return EXIT_UNSUPPORTED;
  97. }
  98. #endif