1 /* $NetBSD: strptimetest.c,v 1.2 2005/03/05 07:48:47 martin Exp $ */
4 * This file placed in the public domain.
5 * David Laight March 2005
17 #define STR(x) XSTR(x)
19 typedef struct tm tm_t
;
22 const static struct test_list
{
28 { "mon", "%a", 3, { .tm_wday
= 1 } },
29 { "tueSDay", "%A", 7, { .tm_wday
= 2 } },
30 { "september", "%b", 9, { .tm_mon
= 8 } },
31 { "septembe", "%B", 3, { .tm_mon
= 8 } },
32 { "Fri Mar 4 20:05:34 2005", "%a %b %e %H:%M:%S %Y", 24,
33 { .tm_wday
= 5, .tm_mon
=2, .tm_mday
= 4, .tm_hour
=20,
34 .tm_min
=5, .tm_sec
=34, .tm_year
=105 } },
37 /* The pre-1.23 versions get some of these wrong! */
38 { "5\t3 4 8pm:05:34 2005", "%w%n%m%t%d%n%k%p:%M:%S %Y", 21,
39 { .tm_wday
= 5, .tm_mon
=2, .tm_mday
= 4, .tm_hour
=20,
40 .tm_min
=5, .tm_sec
=34, .tm_year
=105 } },
42 { "Fri Mar 4 20:05:34 2005", "%c", 24,
43 { .tm_wday
= 5, .tm_mon
=2, .tm_mday
= 4, .tm_hour
=20,
44 .tm_min
=5, .tm_sec
=34, .tm_year
=105 } },
45 { "x20y", "x%Cy", 4, { .tm_year
= 100 } },
46 { "x84y", "x%yy", 4, { .tm_year
= 84 } },
47 { "x2084y", "x%C%yy", 6, { .tm_year
= 184 } },
48 { "x8420y", "x%y%Cy", 6, { .tm_year
= 184 } },
49 { "%20845", "%%%C%y5", 6, { .tm_year
= 184 } },
52 { "1980", "%Y", 4, { .tm_year
= 80 } },
53 { "1980", "%EY", 4, { .tm_year
= 80 } },
54 { "sunday", "%A", 6 },
55 { "sunday", "%EA", -1 },
56 { "SaturDay", "%A", 8, { .tm_wday
= 6 } },
57 { "SaturDay", "%OA", -1 },
60 { "59", "%S", 2, { .tm_sec
= 59 } },
61 { "60", "%S", 2, { .tm_sec
= 60 } },
62 { "61", "%S", 2, { .tm_sec
= 61 } },
71 fprintf(stderr
, "usage: strptimetest [-v] [[-Ssec] [-Mmin] [-Hhour] [-dmday] [-mmon] [-Yyear] [-uwday] [-jyday] buffer format length]\n");
76 mismatch(const char *buf
, const char *fmt
, int len
,
77 const char *tm_x
, int got_tm_x
, int want_tm_x
)
80 "strptime(\"%.*s\" \"%s\", \"%s\", ...) failed %s %d != %d\n",
81 len
, buf
, buf
+ len
, fmt
, tm_x
, got_tm_x
, want_tm_x
);
85 test(const char *buf
, const char *fmt
, int len
, const tm_t
*want
)
91 printf("strptime(\"%.*s\" \"%s\", \"%s\", ...)\n",
92 len
, buf
, buf
+ len
, fmt
);
94 memset(&got
, 0, sizeof got
);
95 np
= strptime(buf
, fmt
, &got
);
101 "strptime(\"%.*s\" \"%s\", \"%s\", ...) failed\n",
102 len
, buf
, buf
+ len
, fmt
);
108 "strptime(\"%.*s\" \"%s\", \"%s\", ...) ok so failed\n",
109 (int)(np
- buf
), buf
, np
, fmt
);
112 if (np
- buf
!= len
) {
114 "strptime(\"%.*s\" \"%s\", \"%s\", ...) failed len %d\n",
115 (int)(np
- buf
), buf
, np
, fmt
, len
);
119 #define CHECK(tm_x) \
120 if (got.tm_x != want->tm_x) { \
121 mismatch(buf, fmt, len, STR(tm_x), got.tm_x, want->tm_x); \
141 const struct test_list
*t
;
144 for (t
= std_tests
; t
->fmt
; t
++) {
145 rval
= test(t
->buf
, t
->fmt
, t
->len
, &t
->want
);
158 v
= strtoul(optarg
, &ep
, 0);
159 if (*ep
|| ep
== optarg
)
165 main(int argc
, char *argv
[])
169 memset(&tm_want
, 0, sizeof tm_want
);
171 switch (getopt(argc
, argv
, "S:M:H:d:m:Y:u:j:v")) {
179 case 'S': tm_want
.tm_sec
= argint(); continue;
180 case 'M': tm_want
.tm_min
= argint(); continue;
181 case 'H': tm_want
.tm_hour
= argint(); continue;
182 case 'd': tm_want
.tm_mday
= argint(); continue;
183 case 'm': tm_want
.tm_mon
= argint(); continue;
184 case 'Y': tm_want
.tm_year
= argint() - 1900; continue;
185 case 'u': tm_want
.tm_wday
= argint(); continue;
186 case 'j': tm_want
.tm_yday
= argint(); continue;
198 return test(argv
[0], argv
[1], atoi(argv
[2]), &tm_want
);