1 /* Test that posixtime works as required.
2    Copyright (C) 2009-2018 Free Software Foundation, Inc.
3 
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 /* Written by Jim Meyering.  */
18 
19 #include <config.h>
20 
21 #include "posixtm.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdint.h>
27 #include <time.h>
28 
29 #include "intprops.h"
30 #include "macros.h"
31 
32 struct posixtm_test
33 {
34   char const *in;
35   unsigned int syntax_bits;
36   bool valid;
37   int_least64_t t_expected;
38 };
39 
40 /* Test mainly with syntax_bits == LY.  */
41 enum { LY = PDS_CENTURY | PDS_SECONDS };
42 
43 static struct posixtm_test const T[] =
44   {
45     /* no year specified; cross-check via another posixtime call */
46     { "12131415.16",     LY, 1,            0}, /* ??? Dec 13 14:15:16 ???? */
47     { "12131415",        LY, 1,            0}, /* ??? Dec 13 14:15:00 ???? */
48 
49     /* These two tests fail on 64-bit Solaris up through at least
50        Solaris 10, which is off by one day for timestamps before
51        0001-01-01 00:00:00 UTC.  */
52     { "000001010000.00", LY, 1,
53                       - INT64_C (62167219200)},/* Sat Jan  1 00:00:00 0    */
54     { "000012312359.59", LY, 1,
55                       - INT64_C (62135596801)},/* Fri Dec 31 23:59:59 0    */
56 
57     { "000101010000.00", LY, 1,
58                       - INT64_C (62135596800)},/* Sat Jan  1 00:00:00 1    */
59     { "190112132045.51", LY, 1,
60                        - INT64_C (2147483649)},/* Fri Dec 13 20:45:51 1901 */
61     { "190112132045.52", LY, 1,
62                        - INT64_C (2147483648)},/* Fri Dec 13 20:45:52 1901 */
63     { "190112132045.53", LY, 1,  -2147483647}, /* Fri Dec 13 20:45:53 1901 */
64     { "190112132046.52", LY, 1,  -2147483588}, /* Fri Dec 13 20:46:52 1901 */
65     { "190112132145.52", LY, 1,  -2147480048}, /* Fri Dec 13 21:45:52 1901 */
66     { "190112142045.52", LY, 1,  -2147397248}, /* Sat Dec 14 20:45:52 1901 */
67     { "190201132045.52", LY, 1,  -2144805248}, /* Mon Jan 13 20:45:52 1902 */
68     { "196912312359.59", LY, 1,           -1}, /* Wed Dec 31 23:59:59 1969 */
69     { "197001010000.00", LY, 1,            0}, /* Thu Jan  1 00:00:00 1970 */
70     { "197001010000.01", LY, 1,            1}, /* Thu Jan  1 00:00:01 1970 */
71     { "197001010001.00", LY, 1,           60}, /* Thu Jan  1 00:01:00 1970 */
72     { "197001010000.60", LY, 1,           60}, /* Thu Jan  1 00:01:00 1970 */
73     { "197001010100.00", LY, 1,         3600}, /* Thu Jan  1 01:00:00 1970 */
74     { "197001020000.00", LY, 1,        86400}, /* Fri Jan  2 00:00:00 1970 */
75     { "197002010000.00", LY, 1,      2678400}, /* Sun Feb  1 00:00:00 1970 */
76     { "197101010000.00", LY, 1,     31536000}, /* Fri Jan  1 00:00:00 1971 */
77     { "197001000000.00", LY, 0,            0}, /* -- */
78     { "197000010000.00", LY, 0,            0}, /* -- */
79     { "197001010060.00", LY, 0,            0}, /* -- */
80     { "197001012400.00", LY, 0,            0}, /* -- */
81     { "197001320000.00", LY, 0,            0}, /* -- */
82     { "197013010000.00", LY, 0,            0}, /* -- */
83     { "203801190314.06", LY, 1,   2147483646}, /* Tue Jan 19 03:14:06 2038 */
84     { "203801190314.07", LY, 1,   2147483647}, /* Tue Jan 19 03:14:07 2038 */
85     { "203801190314.08", LY, 1,
86                        INT64_C (  2147483648)},/* Tue Jan 19 03:14:08 2038 */
87     { "999912312359.59", LY, 1,
88                        INT64_C (253402300799)},/* Fri Dec 31 23:59:59 9999 */
89     { "1112131415",      LY, 1,   1323785700}, /* Tue Dec 13 14:15:00 2011 */
90     { "1112131415.16",   LY, 1,   1323785716}, /* Tue Dec 13 14:15:16 2011 */
91     { "201112131415.16", LY, 1,   1323785716}, /* Tue Dec 13 14:15:16 2011 */
92     { "191112131415.16", LY, 1,  -1831974284}, /* Wed Dec 13 14:15:16 1911 */
93     { "203712131415.16", LY, 1,   2144326516}, /* Sun Dec 13 14:15:16 2037 */
94     { "3712131415.16",   LY, 1,   2144326516}, /* Sun Dec 13 14:15:16 2037 */
95     { "6812131415.16",   LY, 1,
96                        INT64_C (  3122633716)},/* Thu Dec 13 14:15:16 2068 */
97     { "6912131415.16",   LY, 1,     -1590284}, /* Sat Dec 13 14:15:16 1969 */
98     { "7012131415.16",   LY, 1,     29945716}, /* Sun Dec 13 14:15:16 1970 */
99     { "1213141599",      PDS_TRAILING_YEAR,
100                              1,    945094500}, /* Mon Dec 13 14:15:00 1999 */
101     { "1213141500",      PDS_TRAILING_YEAR,
102                              1,    976716900}, /* Wed Dec 13 14:15:00 2000 */
103     { NULL,               0, 0,            0}
104   };
105 
106 int
main(void)107 main (void)
108 {
109   unsigned int i;
110   int fail = 0;
111   char curr_year_str[30];
112   struct tm *tm;
113   time_t t_now;
114   int err;
115   size_t n_bytes;
116 
117   /* The above test data requires Universal Time, e.g., TZ="UTC0".  */
118   err = setenv ("TZ", "UTC0", 1);
119   ASSERT (err == 0);
120 
121   t_now = time (NULL);
122   ASSERT (t_now != (time_t) -1);
123   tm = localtime (&t_now);
124   ASSERT (tm);
125   n_bytes = strftime (curr_year_str, sizeof curr_year_str, "%Y", tm);
126   ASSERT (0 < n_bytes);
127 
128   for (i = 0; T[i].in; i++)
129     {
130       time_t t_out;
131       time_t t_exp;
132       bool ok;
133 
134       /* Some tests assume that time_t is signed.
135          If it is unsigned and the result is negative, skip the test. */
136       if (T[i].t_expected < 0 && ! TYPE_SIGNED (time_t))
137         {
138           printf ("skipping %s: result is negative, "
139                   "but your time_t is unsigned\n", T[i].in);
140           continue;
141         }
142 
143       if (! (TYPE_MINIMUM (time_t) <= T[i].t_expected
144              && T[i].t_expected <= TYPE_MAXIMUM (time_t)))
145         {
146           printf ("skipping %s: result is out of range of your time_t\n",
147                   T[i].in);
148           continue;
149         }
150 
151       t_exp = T[i].t_expected;
152 
153       /* If an input string does not specify the year number, determine
154          the expected output by calling posixtime with an otherwise
155          equivalent string that starts with the current year.  */
156       if (8 <= strlen (T[i].in)
157           && (T[i].in[8] == '.' || T[i].in[8] == '\0'))
158         {
159           char tmp_buf[20];
160           stpcpy (stpcpy (tmp_buf, curr_year_str), T[i].in);
161           ASSERT (posixtime (&t_exp, tmp_buf, T[i].syntax_bits));
162         }
163 
164       ok = posixtime (&t_out, T[i].in, T[i].syntax_bits);
165       if (ok != !!T[i].valid)
166         {
167           printf ("%s return value mismatch: got %d, expected %d\n",
168                   T[i].in, !!ok, T[i].valid);
169           fail = 1;
170           continue;
171         }
172 
173       if (!ok)
174         continue;
175 
176       if (t_out != t_exp)
177         {
178           printf ("%s mismatch (-: actual; +:expected)\n-%12ld\n+%12ld\n",
179                   T[i].in, t_out, t_exp);
180           fail = 1;
181         }
182     }
183 
184   return fail;
185 }
186 
187 /*
188 Local Variables:
189 indent-tabs-mode: nil
190 End:
191 */
192