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