]> git.saurik.com Git - apple/libc.git/blame - tests/stdtime.c
Libc-1439.100.3.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;
aa54d2fa 29 char tz[100] = { 0 };
974e3884
A
30
31 localtime_r(&t, &tm);
32 T_LOG("%s", asctime(&tm));
33 T_ASSERT_NOTNULL(strptime("GMT", "%Z", &tm), "strptime GMT");
34 T_LOG("%s", asctime(&tm));
35
aa54d2fa
A
36 strftime(tz, sizeof(tz), "%Z", &tm);
37 T_LOG("The current time zone name is: %s\n", tz);
974e3884
A
38
39 localtime_r(&t, &tm);
aa54d2fa 40 T_ASSERT_NOTNULL(strptime(tz, "%Z", &tm), "strptime local TZ name");
974e3884
A
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{
507116e3
A
76 // POSIX conformance requires that %Y only use 4 characters, so use the
77 // field width to change that for this test.
974e3884
A
78 char *timestr = "20080922T020000";
79 struct tm tm;
80 bzero(&tm, sizeof(tm));
507116e3 81 T_ASSERT_NOTNULL(strptime("10001", "%5Y", &tm), NULL);
974e3884
A
82 T_EXPECT_EQ(tm.tm_year, 10001 - 1900, NULL);
83 T_ASSERT_NOTNULL(strptime(timestr, "%Y%m%dT%H%M%S", &tm), NULL);
84}
85
86T_DECL(strptime_PR_10842560, "strptime() with %W and %U")
87{
88 const struct test {
89 const char *fmt;
90 const char *str;
91 const char *result;
92 } test[] = {
93 {"%Y:%U:%w:%H", "2012:6:0:23", "Sun Feb 05 2012 23:00"},
94 {"%Y:%w:%U:%H", "2012:0:6:23", "Sun Feb 05 2012 23:00"},
95 {"%U:%w:%Y:%H", "6:0:2012:23", "Sun Feb 05 2012 23:00"},
96 {"%U:%Y:%w:%H", "6:2012:0:23", "Sun Feb 05 2012 23:00"},
97 {"%w:%Y:%U:%H", "0:2012:6:23", "Sun Feb 05 2012 23:00"},
98 {"%w:%U:%Y:%H", "0:6:2012:23", "Sun Feb 05 2012 23:00"},
99 {"%Y:%V:%w:%H", "2012:6:0:23", "Sun Feb 12 2012 23:00"},
100 {"%Y:%w:%V:%H", "2012:0:6:23", "Sun Feb 12 2012 23:00"},
101 {"%V:%w:%Y:%H", "6:0:2012:23", "Sun Feb 12 2012 23:00"},
102 {"%V:%Y:%w:%H", "6:2012:0:23", "Sun Feb 12 2012 23:00"},
103 {"%w:%Y:%V:%H", "0:2012:6:23", "Sun Feb 12 2012 23:00"},
104 {"%w:%V:%Y:%H", "0:6:2012:23", "Sun Feb 12 2012 23:00"},
105 {"%Y:%W:%w:%H", "2012:6:0:23", "Sun Feb 12 2012 23:00"},
106 {"%Y:%w:%W:%H", "2012:0:6:23", "Sun Feb 12 2012 23:00"},
107 {"%W:%w:%Y:%H", "6:0:2012:23", "Sun Feb 12 2012 23:00"},
108 {"%W:%Y:%w:%H", "6:2012:0:23", "Sun Feb 12 2012 23:00"},
109 {"%w:%Y:%W:%H", "0:2012:6:23", "Sun Feb 12 2012 23:00"},
110 {"%w:%W:%Y:%H", "0:6:2012:23", "Sun Feb 12 2012 23:00"},
111 {"%Y:%U:%w:%H", "2011:6:0:23", "Sun Feb 06 2011 23:00"},
112 {"%Y:%U:%w:%H", "2010:6:0:23", "Sun Feb 07 2010 23:00"},
113 {"%Y:%U:%w:%H", "2009:6:0:23", "Sun Feb 08 2009 23:00"},
114 {"%Y:%U:%w:%H", "2008:6:0:23", "Sun Feb 10 2008 23:00"},
115 {"%Y:%U:%w:%H", "2007:6:0:23", "Sun Feb 11 2007 23:00"},
116 {"%Y:%U:%w:%H", "2006:6:0:23", "Sun Feb 05 2006 23:00"},
117 {"%Y:%V:%w:%H", "2011:6:0:23", "Sun Feb 13 2011 23:00"},
118 {"%Y:%V:%w:%H", "2010:6:0:23", "Sun Feb 14 2010 23:00"},
119 {"%Y:%V:%w:%H", "2009:6:0:23", "Sun Feb 08 2009 23:00"},
120 {"%Y:%V:%w:%H", "2008:6:0:23", "Sun Feb 10 2008 23:00"},
121 {"%Y:%V:%w:%H", "2007:6:0:23", "Sun Feb 11 2007 23:00"},
122 {"%Y:%V:%w:%H", "2006:6:0:23", "Sun Feb 12 2006 23:00"},
123 {"%Y:%W:%w:%H", "2011:6:0:23", "Sun Feb 13 2011 23:00"},
124 {"%Y:%W:%w:%H", "2010:6:0:23", "Sun Feb 14 2010 23:00"},
125 {"%Y:%W:%w:%H", "2009:6:0:23", "Sun Feb 15 2009 23:00"},
126 {"%Y:%W:%w:%H", "2008:6:0:23", "Sun Feb 17 2008 23:00"},
127 {"%Y:%W:%w:%H", "2007:6:0:23", "Sun Feb 11 2007 23:00"},
128 {"%Y:%W:%w:%H", "2006:6:0:23", "Sun Feb 12 2006 23:00"},
129 {NULL, NULL, NULL}
130 };
131 const struct test *tp;
132
133 for(tp = test; tp->fmt; tp++){
134 struct tm Tm;
135 char *s;
136 char Buf[100];
137
138 memset(&Tm,0,sizeof(Tm));
139 s = strptime(tp->str, tp->fmt, &Tm);
140 T_QUIET; T_EXPECT_NOTNULL(s, "strptime() should return non-NULL");
141 if (s) {
142 strftime(Buf, sizeof(Buf), "%a %b %d %Y %R", &Tm);
143 T_EXPECT_EQ_STR(Buf, tp->result, "%s | %s", tp->fmt, tp->str);
144 }
145 }
146}
147
c92a3ae8 148#if !TARGET_OS_BRIDGE
a9aaacca
A
149T_DECL(strptime_asctime, "strptime->asctime",
150 T_META_REQUIRES_OS_VARIANT_NOT("IsDarwinOS")) {
974e3884
A
151 char *test[] = {
152 "Sun, 6 Apr 2003 03:30:00 -0500",
153 "Sun, 6 Apr 2003 04:30:00 -0500",
154 "Sun, 6 Apr 2003 05:30:00 -0500",
155 "Sun, 6 Apr 2003 06:30:00 -0500",
156 "Wed, 17 Sep 2003 13:30:00 -0500",
157 "Sun, 26 Oct 2003 03:30:00 -0500",
158 "Sun, 26 Oct 2003 04:30:00 -0500",
159 "Sun, 26 Oct 2003 05:30:00 -0500",
160 "Sun, 26 Oct 2003 06:30:00 -0500",
161 NULL
162 };
163
164 char *result[] = {
165 "Sun Apr 6 00:30:00 2003\n",
166 "Sun Apr 6 01:30:00 2003\n",
167 "Sun Apr 6 03:30:00 2003\n",
168 "Sun Apr 6 04:30:00 2003\n",
169 "Wed Sep 17 11:30:00 2003\n",
170 "Sun Oct 26 01:30:00 2003\n",
171 "Sun Oct 26 01:30:00 2003\n",
172 "Sun Oct 26 02:30:00 2003\n",
173 "Sun Oct 26 03:30:00 2003\n",
174 NULL
175 };
176
177 int i = 0;
178 while (test[i]){
179 struct tm tm = {0};
180 strptime(test[i], "%a, %d %b %Y %H:%M:%S %z", &tm);
181 T_EXPECT_EQ_STR(result[i], asctime(&tm), "%s", test[i]);
182 i++;
183 }
184}
c92a3ae8 185#endif