1 // SPDX-License-Identifier: MIT
3 #define _GNU_SOURCE /* For tm_gmtoff */
11 * checkStrptime - parse time and check if it matches expected value
13 * This function compares time and date fields of tm structure only.
14 * It's because tm_wday and tm_yday may - but don't have to - be set
15 * while parsing a date.
17 static void checkStrptime(const char *s
, const char *format
, const struct tm
*expected
) {
21 ret
= strptime(s
, format
, &tm
);
22 if (!ret
|| *ret
!= '\0') {
23 t_error("\"%s\": failed to parse \"%s\"\n", format
, s
);
24 } else if (tm
.tm_sec
!= expected
->tm_sec
||
25 tm
.tm_min
!= expected
->tm_min
||
26 tm
.tm_hour
!= expected
->tm_hour
||
27 tm
.tm_mday
!= expected
->tm_mday
||
28 tm
.tm_mon
!= expected
->tm_mon
||
29 tm
.tm_year
!= expected
->tm_year
) {
33 strftime(buf1
, sizeof(buf1
), "%FT%H:%M:%S%Z", expected
);
34 strftime(buf2
, sizeof(buf2
), "%FT%H:%M:%S%Z", &tm
);
36 t_error("\"%s\": for \"%s\" expected %s but got %s\n", format
, s
, buf1
, buf2
);
40 static void checkStrptimeTz(const char *s
, int h
, int m
) {
41 long int expected
= h
* 3600 + m
* 60;
45 ret
= strptime(s
, "%z", &tm
);
46 if (!ret
|| *ret
!= '\0') {
47 t_error("\"%%z\": failed to parse \"%s\"\n", s
);
48 } else if (tm
.tm_gmtoff
!= expected
) {
49 t_error("\"%%z\": for \"%s\" expected tm_gmtoff %ld but got %ld\n", s
, tm
.tm_gmtoff
, expected
);
53 static struct tm tm1
= {
62 static struct tm tm2
= {
68 .tm_year
= 1991 - 1900,
71 static struct tm tm3
= {
77 .tm_year
= 2015 - 1900,
80 static struct tm tm4
= {
86 .tm_year
= 1856 - 1900,
90 setenv("TZ", "UTC0", 1);
93 checkStrptime("20:57:08", "%H:%M:%S", &tm1
);
94 checkStrptime("20:57:8", "%R:%S", &tm1
);
95 checkStrptime("20:57:08", "%T", &tm1
);
98 checkStrptime("20:57:08", "%H : %M : %S", &tm1
);
99 checkStrptime("20 57 08", "%H %M %S", &tm1
);
100 checkStrptime("20%57%08", "%H %% %M%%%S", &tm1
);
101 checkStrptime("foo20bar57qux08 ", "foo %Hbar %M qux%S ", &tm1
);
104 checkStrptime("1991-08-25", "%Y-%m-%d", &tm2
);
105 checkStrptime("25.08.91", "%d.%m.%y", &tm2
);
106 checkStrptime("08/25/91", "%D", &tm2
);
107 checkStrptime("21.10.15", "%d.%m.%y", &tm3
);
108 checkStrptime("10.7.56 in 18th", "%d.%m.%y in %C th", &tm4
);
111 checkStrptime("1856-07-10", "%F", &tm4
);
112 checkStrptime("683078400", "%s", &tm2
);
113 checkStrptimeTz("+0200", 2, 0);
114 checkStrptimeTz("-0530", -5, -30);
115 checkStrptimeTz("-06", -6, 0);