]> git.saurik.com Git - apple/libc.git/blame - tests/stdtime.c
Libc-1272.200.26.tar.gz
[apple/libc.git] / tests / stdtime.c
CommitLineData
974e3884
A
1#include <stdio.h>
2#include <time.h>
3#include <stdlib.h>
4#include <string.h>
5#include <locale.h>
6#include <err.h>
c92a3ae8 7#include <TargetConditionals.h>
974e3884
A
8
9#include <darwintest.h>
10
11T_DECL(strptime_PR_24428248, "strptime parse day of year %j does not work correctly")
12{
13 struct tm percent_j = {0};
14 struct tm standard = {0};
15 strptime("2007 80 0 0 15", "%Y %j %H %M %S", &percent_j);
16 strptime("2007-03-21 0:0:15", "%Y-%m-%d %H:%M:%S", &standard);
17 time_t percent_j_out = mktime(&percent_j);
18 time_t standard_out = mktime(&standard);
19 T_EXPECT_EQ(percent_j_out, standard_out, NULL);
20}
21
c92a3ae8 22#if !TARGET_OS_BRIDGE
974e3884
A
23T_DECL(strptime_PR_5879606, "alloca(strlen(input)) in strptime(\"%Z\")")
24{
25 struct tm tm;
26 time_t t = time(NULL);
27 size_t s = 100000000;
28 char *buf;
29
30 localtime_r(&t, &tm);
31 T_LOG("%s", asctime(&tm));
32 T_ASSERT_NOTNULL(strptime("GMT", "%Z", &tm), "strptime GMT");
33 T_LOG("%s", asctime(&tm));
34
35 localtime_r(&t, &tm);
36 T_ASSERT_NOTNULL(strptime("PST", "%Z", &tm), "strptime PST");
37 T_LOG("%s", asctime(&tm));
38
39 localtime_r(&t, &tm);
40 T_ASSERT_NOTNULL(strptime("PDT", "%Z", &tm), "strptime PDT");
41 T_LOG("%s", asctime(&tm));
42
43 T_QUIET; T_ASSERT_NOTNULL((buf = malloc(s)), NULL);
44 memset(buf, 'Z', s);
45 buf[s - 1] = 0;
46 T_ASSERT_NULL(strptime(buf, "%Z", &tm), NULL);
47 free(buf);
48}
49
50T_DECL(strptime_PR_6882179, "date command fails with 'illegal time format'")
51{
52 struct tm tm;
53 char buf[] = "Tue May 12 18:19:41 PDT 2009";
54
55 T_ASSERT_NOTNULL(strptime(buf, "%a %b %d %T %Z %Y", &tm), NULL);
56
57 T_EXPECT_EQ(tm.tm_sec, 0x29, NULL);
58 T_EXPECT_EQ(tm.tm_min, 0x13, NULL);
59 T_EXPECT_EQ(tm.tm_hour, 0x12, NULL);
60 T_EXPECT_EQ(tm.tm_mday, 0xc, NULL);
61 T_EXPECT_EQ(tm.tm_mon, 0x4, NULL);
62 T_EXPECT_EQ(tm.tm_year, 0x6d, NULL);
63 T_EXPECT_EQ(tm.tm_wday, 0x2, NULL);
64 T_EXPECT_EQ(tm.tm_yday, 0x83, NULL);
65}
c92a3ae8 66#endif
974e3884
A
67
68T_DECL(strptime_lukemftp, "year parsing"){
69 struct tm tm;
70 setlocale(LC_ALL, "C");
71 T_ASSERT_NOTNULL(strptime("20090505223446", "%Y%m%d%H%M%S", &tm), NULL);
72}
73
74T_DECL(strptime_five_digit_year, "strptime(%Y) with a 5 digit year")
75{
76 char *timestr = "20080922T020000";
77 struct tm tm;
78 bzero(&tm, sizeof(tm));
79 T_ASSERT_NOTNULL(strptime("10001", "%Y", &tm), NULL);
80 T_EXPECT_EQ(tm.tm_year, 10001 - 1900, NULL);
81 T_ASSERT_NOTNULL(strptime(timestr, "%Y%m%dT%H%M%S", &tm), NULL);
82}
83
84T_DECL(strptime_PR_10842560, "strptime() with %W and %U")
85{
86 const struct test {
87 const char *fmt;
88 const char *str;
89 const char *result;
90 } test[] = {
91 {"%Y:%U:%w:%H", "2012:6:0:23", "Sun Feb 05 2012 23:00"},
92 {"%Y:%w:%U:%H", "2012:0:6:23", "Sun Feb 05 2012 23:00"},
93 {"%U:%w:%Y:%H", "6:0:2012:23", "Sun Feb 05 2012 23:00"},
94 {"%U:%Y:%w:%H", "6:2012:0:23", "Sun Feb 05 2012 23:00"},
95 {"%w:%Y:%U:%H", "0:2012:6:23", "Sun Feb 05 2012 23:00"},
96 {"%w:%U:%Y:%H", "0:6:2012:23", "Sun Feb 05 2012 23:00"},
97 {"%Y:%V:%w:%H", "2012:6:0:23", "Sun Feb 12 2012 23:00"},
98 {"%Y:%w:%V:%H", "2012:0:6:23", "Sun Feb 12 2012 23:00"},
99 {"%V:%w:%Y:%H", "6:0:2012:23", "Sun Feb 12 2012 23:00"},
100 {"%V:%Y:%w:%H", "6:2012:0:23", "Sun Feb 12 2012 23:00"},
101 {"%w:%Y:%V:%H", "0:2012:6:23", "Sun Feb 12 2012 23:00"},
102 {"%w:%V:%Y:%H", "0:6:2012:23", "Sun Feb 12 2012 23:00"},
103 {"%Y:%W:%w:%H", "2012:6:0:23", "Sun Feb 12 2012 23:00"},
104 {"%Y:%w:%W:%H", "2012:0:6:23", "Sun Feb 12 2012 23:00"},
105 {"%W:%w:%Y:%H", "6:0:2012:23", "Sun Feb 12 2012 23:00"},
106 {"%W:%Y:%w:%H", "6:2012:0:23", "Sun Feb 12 2012 23:00"},
107 {"%w:%Y:%W:%H", "0:2012:6:23", "Sun Feb 12 2012 23:00"},
108 {"%w:%W:%Y:%H", "0:6:2012:23", "Sun Feb 12 2012 23:00"},
109 {"%Y:%U:%w:%H", "2011:6:0:23", "Sun Feb 06 2011 23:00"},
110 {"%Y:%U:%w:%H", "2010:6:0:23", "Sun Feb 07 2010 23:00"},
111 {"%Y:%U:%w:%H", "2009:6:0:23", "Sun Feb 08 2009 23:00"},
112 {"%Y:%U:%w:%H", "2008:6:0:23", "Sun Feb 10 2008 23:00"},
113 {"%Y:%U:%w:%H", "2007:6:0:23", "Sun Feb 11 2007 23:00"},
114 {"%Y:%U:%w:%H", "2006:6:0:23", "Sun Feb 05 2006 23:00"},
115 {"%Y:%V:%w:%H", "2011:6:0:23", "Sun Feb 13 2011 23:00"},
116 {"%Y:%V:%w:%H", "2010:6:0:23", "Sun Feb 14 2010 23:00"},
117 {"%Y:%V:%w:%H", "2009:6:0:23", "Sun Feb 08 2009 23:00"},
118 {"%Y:%V:%w:%H", "2008:6:0:23", "Sun Feb 10 2008 23:00"},
119 {"%Y:%V:%w:%H", "2007:6:0:23", "Sun Feb 11 2007 23:00"},
120 {"%Y:%V:%w:%H", "2006:6:0:23", "Sun Feb 12 2006 23:00"},
121 {"%Y:%W:%w:%H", "2011:6:0:23", "Sun Feb 13 2011 23:00"},
122 {"%Y:%W:%w:%H", "2010:6:0:23", "Sun Feb 14 2010 23:00"},
123 {"%Y:%W:%w:%H", "2009:6:0:23", "Sun Feb 15 2009 23:00"},
124 {"%Y:%W:%w:%H", "2008:6:0:23", "Sun Feb 17 2008 23:00"},
125 {"%Y:%W:%w:%H", "2007:6:0:23", "Sun Feb 11 2007 23:00"},
126 {"%Y:%W:%w:%H", "2006:6:0:23", "Sun Feb 12 2006 23:00"},
127 {NULL, NULL, NULL}
128 };
129 const struct test *tp;
130
131 for(tp = test; tp->fmt; tp++){
132 struct tm Tm;
133 char *s;
134 char Buf[100];
135
136 memset(&Tm,0,sizeof(Tm));
137 s = strptime(tp->str, tp->fmt, &Tm);
138 T_QUIET; T_EXPECT_NOTNULL(s, "strptime() should return non-NULL");
139 if (s) {
140 strftime(Buf, sizeof(Buf), "%a %b %d %Y %R", &Tm);
141 T_EXPECT_EQ_STR(Buf, tp->result, "%s | %s", tp->fmt, tp->str);
142 }
143 }
144}
145
c92a3ae8 146#if !TARGET_OS_BRIDGE
974e3884
A
147T_DECL(strptime_asctime, "strptime->asctime")
148{
149 char *test[] = {
150 "Sun, 6 Apr 2003 03:30:00 -0500",
151 "Sun, 6 Apr 2003 04:30:00 -0500",
152 "Sun, 6 Apr 2003 05:30:00 -0500",
153 "Sun, 6 Apr 2003 06:30:00 -0500",
154 "Wed, 17 Sep 2003 13:30:00 -0500",
155 "Sun, 26 Oct 2003 03:30:00 -0500",
156 "Sun, 26 Oct 2003 04:30:00 -0500",
157 "Sun, 26 Oct 2003 05:30:00 -0500",
158 "Sun, 26 Oct 2003 06:30:00 -0500",
159 NULL
160 };
161
162 char *result[] = {
163 "Sun Apr 6 00:30:00 2003\n",
164 "Sun Apr 6 01:30:00 2003\n",
165 "Sun Apr 6 03:30:00 2003\n",
166 "Sun Apr 6 04:30:00 2003\n",
167 "Wed Sep 17 11:30:00 2003\n",
168 "Sun Oct 26 01:30:00 2003\n",
169 "Sun Oct 26 01:30:00 2003\n",
170 "Sun Oct 26 02:30:00 2003\n",
171 "Sun Oct 26 03:30:00 2003\n",
172 NULL
173 };
174
175 int i = 0;
176 while (test[i]){
177 struct tm tm = {0};
178 strptime(test[i], "%a, %d %b %Y %H:%M:%S %z", &tm);
179 T_EXPECT_EQ_STR(result[i], asctime(&tm), "%s", test[i]);
180 i++;
181 }
182}
c92a3ae8 183#endif