tst-ctime.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Test for ctime
  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. static int
  20. do_test (void)
  21. {
  22. char *str;
  23. char strb[32];
  24. time_t t;
  25. /* Use glibc time zone extension "TZ=:" to to guarantee that UTC
  26. without leap seconds is used for the test. */
  27. TEST_VERIFY_EXIT (setenv ("TZ", ":", 1) == 0);
  28. tzset ();
  29. /* Check if the epoch time can be converted. */
  30. t = 0;
  31. str = ctime (&t);
  32. TEST_COMPARE_STRING (str, "Thu Jan 1 00:00:00 1970\n");
  33. /* Same as before but with ctime_r. */
  34. str = ctime_r (&t, strb);
  35. TEST_VERIFY (str == strb);
  36. TEST_COMPARE_STRING (str, "Thu Jan 1 00:00:00 1970\n");
  37. /* Check if the max time value for 32 bit time_t can be converted. */
  38. t = 0x7fffffff;
  39. str = ctime (&t);
  40. TEST_COMPARE_STRING (str, "Tue Jan 19 03:14:07 2038\n");
  41. /* Same as before but with ctime_r. */
  42. str = ctime_r (&t, strb);
  43. TEST_VERIFY (str == strb);
  44. TEST_COMPARE_STRING (str, "Tue Jan 19 03:14:07 2038\n");
  45. /* Check if we run on port with 32 bit time_t size */
  46. time_t tov;
  47. if (__builtin_add_overflow (t, 1, &tov))
  48. return 0;
  49. /* Check if the time is converted after 32 bit time_t overflow. */
  50. str = ctime (&tov);
  51. TEST_COMPARE_STRING (str, "Tue Jan 19 03:14:08 2038\n");
  52. /* Same as before but with ctime_r. */
  53. str = ctime_r (&tov, strb);
  54. TEST_VERIFY (str == strb);
  55. TEST_COMPARE_STRING (str, "Tue Jan 19 03:14:08 2038\n");
  56. return 0;
  57. }
  58. #include <support/test-driver.c>
  59. #else
  60. #include <support/test-driver.h>
  61. int
  62. main (void)
  63. {
  64. return EXIT_UNSUPPORTED;
  65. }
  66. #endif