1 /* Test that nstrftime works as required.
2 Copyright (C) 2011-2019 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Jim Meyering. */
30 #define STREQ(a, b) (strcmp (a, b) == 0)
32 /* Support for settings like TZ='<+00>0' was added in IEEE Std 1003.1-2001. */
33 #define TZ_ANGLE_BRACKETS_SHOULD_WORK (200112 <= _POSIX_VERSION)
43 static struct posixtm_test
const T
[] =
45 { 1300000000, 0, "%F", "2011-03-13" },
46 { 0, 10, "%T.%N", "00:00:00.000000010" },
56 for (i
= 0; T
[i
].fmt
; i
++)
60 struct tm
*tm
= gmtime (&t
);
65 n
= nstrftime (buf
, sizeof buf
, T
[i
].fmt
, tm
, 0, T
[i
].in_ns
);
69 printf ("nstrftime failed with format %s\n", T
[i
].fmt
);
72 if (! STREQ (buf
, T
[i
].exp
))
75 printf ("%s: result mismatch: got %s, expected %s\n",
76 T
[i
].fmt
, buf
, T
[i
].exp
);
89 static struct tzalloc_test TZ
[] =
92 { 0, "PST8PDT,M3.2.0,M11.1.0" },
98 { 0, "CET-1CEST,M3.5.0,M10.5.0/3" },
102 { 0, "NZST-12NZDT,M9.5.0,M4.1.0/3" },
108 struct localtime_rz_test
110 /* Input parameters. */
111 struct tzalloc_test
*tza
;
114 /* Expected result. */
117 /* Determines if an incorrectly unset tm_isdst
118 results in failure or just a warning. */
122 static struct localtime_rz_test LT
[] =
124 { TZ
+Pacific
, 0, "1969-12-31 16:00:00 -0800 (PST)", 0 },
125 { TZ
+Arizona
, 0, "1969-12-31 17:00:00 -0700 (MST)", 0 },
126 { TZ
+UTC
, 0, "1970-01-01 00:00:00 +0000 (UTC)", 0 },
127 { TZ
+CentEur
, 0, "1970-01-01 01:00:00 +0100 (CET)", 0 },
128 { TZ
+Japan
, 0, "1970-01-01 09:00:00 +0900 (JST)", 0 },
129 { TZ
+NZ
, 0, "1970-01-01 13:00:00 +1300 (NZDT)", 1 },
130 { TZ
+Pacific
, 500000001, "1985-11-04 16:53:21 -0800 (PST)", 0 },
131 { TZ
+Arizona
, 500000001, "1985-11-04 17:53:21 -0700 (MST)", 0 },
132 { TZ
+UTC
, 500000001, "1985-11-05 00:53:21 +0000 (UTC)", 0 },
133 { TZ
+CentEur
, 500000001, "1985-11-05 01:53:21 +0100 (CET)", 1 },
134 { TZ
+Japan
, 500000001, "1985-11-05 09:53:21 +0900 (JST)", 0 },
135 { TZ
+NZ
, 500000001, "1985-11-05 13:53:21 +1300 (NZDT)", 0 },
136 { TZ
+Pacific
, 1000000002, "2001-09-08 18:46:42 -0700 (PDT)", 0 },
137 { TZ
+Arizona
, 1000000002, "2001-09-08 18:46:42 -0700 (MST)", 0 },
138 { TZ
+UTC
, 1000000002, "2001-09-09 01:46:42 +0000 (UTC)", 0 },
139 { TZ
+CentEur
, 1000000002, "2001-09-09 03:46:42 +0200 (CEST)", 0 },
140 { TZ
+Japan
, 1000000002, "2001-09-09 10:46:42 +0900 (JST)", 0 },
141 { TZ
+NZ
, 1000000002, "2001-09-09 13:46:42 +1200 (NZST)", 0 },
142 #if TZ_ANGLE_BRACKETS_SHOULD_WORK
143 { TZ
+Unknown
, 0, "1970-01-01 00:00:00 -0000 (-00)", 0 },
144 { TZ
+Unknown
, 500000001, "1985-11-05 00:53:21 -0000 (-00)", 0 },
145 { TZ
+Unknown
, 1000000002, "2001-09-09 01:46:42 -0000 (-00)", 0 },
156 for (i
= 0; LT
[i
].tza
; i
++)
158 struct tzalloc_test
*tza
= LT
[i
].tza
;
160 timezone_t tz
= tza
->tz
;
162 static char const format
[] = "%Y-%m-%d %H:%M:%S %z (%Z)";
167 if (!tz
&& tza
->setting
)
169 tz
= tzalloc (tza
->setting
);
173 printf ("%s: tzalloc: %s\n", TZ
[i
].setting
, strerror (errno
));
179 setting
= tza
->setting
? tza
->setting
: "UTC0";
181 if (!localtime_rz (tz
, <
[i
].t
, &tm
))
184 printf ("%s: %ld: localtime_rz: %s\n", setting
, lt
,
189 n
= nstrftime (buf
, sizeof buf
, format
, &tm
, tz
, 0);
193 printf ("%s: %ld: nstrftime failed\n", setting
, lt
);
197 if (! (STREQ (buf
, LT
[i
].exp
)
198 || (!tz
&& n
== strlen (LT
[i
].exp
)
199 && memcmp (buf
, LT
[i
].exp
, n
- sizeof "(GMT)" + 1) == 0
200 && STREQ (buf
+ n
- sizeof "(GMT)" + 1, "(GMT)"))))
202 /* Don't fail for unhandled dst in ahistorical entries,
203 as gnulib doesn't currently fix that issue, seen on Darwin 14. */
204 if (!LT
[i
].ahistorical
|| tm
.tm_isdst
)
206 printf ("%s: expected \"%s\", got \"%s\"\n",
207 setting
, LT
[i
].exp
, buf
);
222 for (mon
= 1; mon
<= 12; mon
++)
226 struct tm qtm
= { .tm_mon
= mon
- 1 };
227 char fmt
[3] = {'%','q','\0'};
229 size_t r
= nstrftime (out
, sizeof (out
), fmt
, &qtm
, 0, 0);
232 puts ("nstrftime(\"%q\") failed");
237 exp
[0] = mon
< 4 ? '1' : mon
< 7 ? '2' : mon
< 10 ? '3' : '4';
238 if (strcmp (out
, exp
) != 0)
240 printf ("nstrftime %%q: expected \"%s\", got \"%s\"\n", exp
, out
);
253 fail
|= posixtm_test ();
254 fail
|= tzalloc_test ();
255 fail
|= quarter_test ();
261 indent-tabs-mode: nil