[v3,0/7] Fix some libm static issues
[platform/upstream/glibc.git] / time / tst-mktime4.c
1 /* Test for mktime (4)
2    Copyright (C) 2021-2024 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18
19 #include <time.h>
20 #include <stdlib.h>
21 #include <support/check.h>
22
23 const struct tm tm0 =
24   {
25     .tm_year = 70,
26     .tm_mon = 0,
27     .tm_mday = 1,
28     .tm_hour = 0,
29     .tm_min = 0,
30     .tm_sec = 0,
31     .tm_wday = 4,
32     .tm_yday = 0,
33   };
34
35 const struct tm tmY2038 =
36   {
37     .tm_year = 138,
38     .tm_mon = 0,
39     .tm_mday = 19,
40     .tm_hour = 3,
41     .tm_min = 14,
42     .tm_sec = 7,
43   };
44
45 const struct tm tm32bitmax =
46   {
47     .tm_year = 206,
48     .tm_mon = 1,
49     .tm_mday = 7,
50     .tm_hour = 6,
51     .tm_min = 28,
52     .tm_sec = 15,
53   };
54
55 static
56 int test_mktime_helper (struct tm *tm, long long int exp_val, int line)
57 {
58   time_t result, t;
59
60   /* Check if we run on port with 32 bit time_t size.  */
61   if (__builtin_add_overflow (exp_val, 0, &t))
62     return 0;
63
64   result = mktime (tm);
65   if (result == (time_t) -1)
66     FAIL_RET ("*** mktime failed: %m in line: %d", line);
67
68   if ((long long int) result != exp_val)
69     FAIL_RET ("*** Result different than expected (%lld != %lld) in %d\n",
70               (long long int) result, exp_val, line);
71
72   return 0;
73 }
74
75 static int
76 do_test (void)
77 {
78   struct tm t;
79   /* Use glibc time zone extension "TZ=:" to to guarantee that UTC
80      without leap seconds is used for the test.  */
81   TEST_VERIFY_EXIT (setenv ("TZ", ":", 1) == 0);
82
83   /* Check that mktime (1970-01-01 00:00:00) returns 0.  */
84   t = tm0;
85   test_mktime_helper (&t, 0, __LINE__);
86
87   /* Check that mktime (2038-01-19 03:14:07) returns 0x7FFFFFFF.  */
88   t = tmY2038;
89   test_mktime_helper (&t, 0x7fffffff, __LINE__);
90
91   /* Check that mktime (2038-01-19 03:14:08) returns 0x80000000
92      (time_t overflow).  */
93   t = tmY2038;
94   t.tm_sec++;
95   test_mktime_helper (&t, 0x80000000, __LINE__);
96
97   /* Check that mktime (2106-02-07 06:28:15) returns 0xFFFFFFFF.  */
98   t = tm32bitmax;
99   test_mktime_helper (&t, 0xFFFFFFFF, __LINE__);
100
101   /* Check that mktime (2106-02-07 06:28:16) returns 0x100000000.  */
102   t = tm32bitmax;
103   t.tm_sec++;
104   test_mktime_helper (&t, 0x100000000, __LINE__);
105
106   return 0;
107 }
108
109 #include <support/test-driver.c>