9
9
#define _XOPEN_SOURCE
10
10
// glibc requires _DEFAULT_SOURCE to be defined in order to get tm_gmtoff.
11
11
#define _DEFAULT_SOURCE
12
+
13
+ #include <assert.h>
12
14
#include <time.h>
13
15
#include <stdio.h>
14
16
#include <string.h>
15
17
18
+ #if __GLIBC__ || __EMSCRIPTEN__
19
+ // Not all implementations support these (for example, upstream musl)
20
+ #define HAVE_WDAY
21
+ #define HAVE_TIMEZONE
22
+ #endif
23
+
24
+ #define STRPTIME (a , b , c ) assert(strptime(a, b, c) != 0)
25
+
16
26
void ReadMonth (const char * month ) {
17
27
struct tm value = {0 };
18
- if (strptime (month , "%b" , & value )) {
19
- printf ("%s: %d\n" , month , value .tm_mon );
20
- }
28
+ STRPTIME (month , "%b" , & value );
29
+ printf ("%s: %d\n" , month , value .tm_mon );
21
30
}
22
31
23
32
int main () {
24
33
struct tm tm ;
25
- char * ptr = strptime ("17410105012000" , "%H%M%S%d%m%Y" , & tm );
26
-
27
- printf (
28
- "%s: %s, %d/%d/%d %d:%d:%d" , (ptr != NULL && * ptr == '\0' ) ? "OK" : "ERR" ,
29
- tm .tm_wday == 0
30
- ? "Sun"
31
- : (tm .tm_wday == 1
32
- ? "Mon"
33
- : (tm .tm_wday == 2
34
- ? "Tue"
35
- : (tm .tm_wday == 3
36
- ? "Wed"
37
- : (tm .tm_wday == 4
38
- ? "Thu"
39
- : (tm .tm_wday == 5
40
- ? "Fri"
41
- : (tm .tm_wday == 6 ? "Sat"
42
- : "ERR" )))))),
34
+ STRPTIME ("17410105012000" , "%H%M%S%d%m%Y" , & tm );
35
+
36
+ printf ("%d/%d/%d %d:%d:%d\n" ,
43
37
tm .tm_mon + 1 , tm .tm_mday , tm .tm_year + 1900 , tm .tm_hour , tm .tm_min ,
44
38
tm .tm_sec );
45
-
46
- printf ("\n" );
39
+ #ifdef HAVE_WDAY
40
+ assert (tm .tm_wday == 3 );
41
+ #endif
47
42
48
43
ReadMonth ("jan" );
49
44
ReadMonth ("january" );
@@ -65,49 +60,54 @@ int main() {
65
60
66
61
67
62
// check that %% is handled correctly for normal strings
68
- strptime ("2020-05-01T00:01%z" ,"%Y-%m-%dT%H:%M%%z" ,& tm );
63
+ STRPTIME ("2020-05-01T00:01%z" ,"%Y-%m-%dT%H:%M%%z" ,& tm );
69
64
printf ("%d\n" ,tm .tm_min );
70
65
71
66
// check that %% is handled correctly even if the letter after it is
72
67
// in EQUIVALENT_MATCHERS
73
- strptime ("%D2020-05-01T00:01" ,"%%D%Y-%m-%dT%H:%M" ,& tm );
68
+ STRPTIME ("%D2020-05-01T00:01" ,"%%D%Y-%m-%dT%H:%M" ,& tm );
74
69
printf ("%d,%d\n" ,tm .tm_year + 1900 ,tm .tm_min );
75
70
76
71
77
72
// check that EQUIVALENT_MATCHERS works
78
73
// %c == %a %b %d %H:%M:%S %Y
79
- strptime ("Sun March 31 12:34:56 2345" ,"%c" ,& tm );
74
+ STRPTIME ("Sun March 31 12:34:56 2345" ,"%c" ,& tm );
80
75
printf ("%d,%d,%d,%d,%d,%d\n" ,tm .tm_year + 1900 ,tm .tm_mon + 1 ,tm .tm_mday ,tm .tm_hour ,tm .tm_min ,tm .tm_sec );
81
76
82
77
// check that EQUIVALENT_MATCHERS works twice
83
78
// 'T': '%H\\:%M\\:%S',
84
79
// 'D': '%m\\/%d\\/%y',
85
- strptime ("12:34:56 01/02/03" ,"%T %D" ,& tm );
80
+ STRPTIME ("12:34:56 01/02/03" ,"%T %D" ,& tm );
86
81
printf ("%d,%d,%d,%d,%d,%d\n" ,tm .tm_year + 1900 ,tm .tm_mon + 1 ,tm .tm_mday ,tm .tm_hour ,tm .tm_min ,tm .tm_sec );
87
82
88
83
// check regex special characters don't break things
89
- strptime (".?12:34:56 01/02/03" ,".?%T %D" ,& tm );
84
+ STRPTIME (".?12:34:56 01/02/03" ,".?%T %D" ,& tm );
90
85
printf ("%d,%d,%d,%d,%d,%d\n" ,tm .tm_year + 1900 ,tm .tm_mon + 1 ,tm .tm_mday ,tm .tm_hour ,tm .tm_min ,tm .tm_sec );
91
86
92
87
88
+ #ifdef HAVE_TIMEZONE
93
89
// check timezone offsets
94
- strptime ("2020-05-01T00:00+0100" ,"%Y-%m-%dT%H:%M%z" ,& tm );
95
- printf ("%ld\n" ,tm .tm_gmtoff ); // 3600
90
+ STRPTIME ("2020-05-01T00:00+0100" ,"%Y-%m-%dT%H:%M%z" ,& tm );
91
+ printf ("tm_gmtoff: %ld\n" ,tm .tm_gmtoff );
92
+ assert (tm .tm_gmtoff == 3600 );
96
93
97
- strptime ("2020-05-01T00:00Z" ,"%Y-%m-%dT%H:%M%z" ,& tm );
98
- printf ("%ld\n" ,tm .tm_gmtoff ); // 0
94
+ STRPTIME ("2020-05-01T00:00Z" ,"%Y-%m-%dT%H:%M%z" ,& tm );
95
+ printf ("tm_gmtoff: %ld\n" ,tm .tm_gmtoff );
96
+ assert (tm .tm_gmtoff == 0 );
99
97
100
- strptime ("2020-05-01T00:00-02:30" ,"%Y-%m-%dT%H:%M%z" ,& tm );
101
- printf ("%ld\n" ,tm .tm_gmtoff ); // -9000
98
+ STRPTIME ("2020-05-01T00:00-02:30" ,"%Y-%m-%dT%H:%M%z" ,& tm );
99
+ printf ("tm_gmtoff: %ld\n" ,tm .tm_gmtoff );
100
+ assert (tm .tm_gmtoff == -9000 );
101
+ #endif
102
102
103
103
// check that the numbers of spaces in format string are ignored
104
- strptime ("12 34 56" ,"%H %M %S" ,& tm );
104
+ STRPTIME ("12 34 56" ,"%H %M %S" ,& tm );
105
105
printf ("%d,%d,%d\n" ,tm .tm_hour ,tm .tm_min ,tm .tm_sec );
106
106
107
- strptime ("123456" ,"%H %M %S" ,& tm );
107
+ STRPTIME ("123456" ,"%H %M %S" ,& tm );
108
108
printf ("%d,%d,%d\n" ,tm .tm_hour ,tm .tm_min ,tm .tm_sec );
109
109
110
- strptime ("12 34 56" ,"%H %M %S" ,& tm );
110
+ STRPTIME ("12 34 56" ,"%H %M %S" ,& tm );
111
111
printf ("%d,%d,%d\n" ,tm .tm_hour ,tm .tm_min ,tm .tm_sec );
112
112
113
113
return 0 ;
0 commit comments