tst-timegm.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Basic tests for timegm.
  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. static void
  19. do_test_func (time_t (*func)(struct tm *))
  20. {
  21. {
  22. struct tm tmg =
  23. {
  24. .tm_sec = 0,
  25. .tm_min = 0,
  26. .tm_hour = 0,
  27. .tm_mday = 1,
  28. .tm_mon = 0,
  29. .tm_year = 70,
  30. .tm_wday = 4,
  31. .tm_yday = 0,
  32. .tm_isdst = 0
  33. };
  34. time_t t = func (&tmg);
  35. TEST_COMPARE (t, 0);
  36. }
  37. {
  38. struct tm tmg =
  39. {
  40. .tm_sec = 7,
  41. .tm_min = 14,
  42. .tm_hour = 3,
  43. .tm_mday = 19,
  44. .tm_mon = 0,
  45. .tm_year = 138,
  46. .tm_wday = 2,
  47. .tm_yday = 18,
  48. .tm_isdst = 0
  49. };
  50. time_t t = func (&tmg);
  51. TEST_COMPARE (t, 0x7fffffff);
  52. }
  53. if (sizeof (time_t) < 8)
  54. return;
  55. {
  56. struct tm tmg =
  57. {
  58. .tm_sec = 8,
  59. .tm_min = 14,
  60. .tm_hour = 3,
  61. .tm_mday = 19,
  62. .tm_mon = 0,
  63. .tm_year = 138,
  64. .tm_wday = 2,
  65. .tm_yday = 18,
  66. .tm_isdst = 0
  67. };
  68. time_t t = func (&tmg);
  69. TEST_COMPARE (t, (time_t) 0x80000000ull);
  70. }
  71. }
  72. static int
  73. do_test (void)
  74. {
  75. do_test_func (timegm);
  76. /* timelocal is an alias to mktime and behaves like timegm with the
  77. difference that it takes timezone into account. */
  78. TEST_VERIFY_EXIT (setenv ("TZ", ":", 1) == 0);
  79. tzset ();
  80. do_test_func (timelocal);
  81. return 0;
  82. }
  83. #include <support/test-driver.c>