]> git.saurik.com Git - apple/libc.git/blob - tests/strptime.c
b34fa1a863097310fce5bb51fe5bd1a0875eec39
[apple/libc.git] / tests / strptime.c
1 #include <time.h>
2
3 #include <darwintest.h>
4 #include <darwintest_utils.h>
5
6 T_DECL(strptime_PR_27004626, "strptime() should fail when a %t doesn't match anything")
7 {
8 struct tm tm;
9 T_ASSERT_NULL(strptime("there'snotemplateforthis", "%t", &tm), NULL);
10 }
11
12 T_DECL(strptime_PR_29381762, "strptime() sets the tm_wday field incorrectly")
13 {
14 time_t epoch = 0;
15 struct tm t = *localtime(&epoch);
16
17 T_LOG("2015-01-01 12:00:00 -> Thursday");
18 (void)strptime("2015-01-01 12:00:00", "%F %T", &t);
19 T_EXPECT_EQ(t.tm_wday, 4, NULL);
20
21 T_LOG("2015-04-19 12:00:00 -> Sunday");
22 (void)strptime("2015-04-19 12:00:00", "%F %T", &t);
23 T_EXPECT_EQ(t.tm_wday, 0, NULL);
24
25 T_LOG("2009-03-03 12:00:00 -> Tuesday");
26 (void)strptime("2009-03-03 12:00:00", "%F %T", &t);
27 T_EXPECT_EQ(t.tm_wday, 2, NULL);
28
29 T_LOG("1990-02-15 12:00:00 -> Thursday");
30 (void)strptime("1990-02-15 12:00:00", "%F %T", &t);
31 T_EXPECT_EQ(t.tm_wday, 4, NULL);
32
33 T_LOG("1993-03-02 12:00:00 -> Sunday");
34 (void)strptime("1993-03-02 12:00:00", "%F %T", &t);
35 T_EXPECT_EQ(t.tm_wday, 2, NULL);
36 }
37
38
39 T_DECL(strptime_PR_42669744_1, "strptime() with %%C, %%y and %%Y")
40 {
41 struct tm tm;
42 char *result;
43
44 // %C%y combinations
45 T_LOG("201, %%C%%y");
46 result = strptime("201", "%C%y", &tm);
47 T_QUIET; T_EXPECT_NOTNULL(result, "201, %%C%%y");
48 T_EXPECT_EQ(tm.tm_year, 2001 - 1900, NULL);
49
50 T_LOG("2010, %%C%%y");
51 result = strptime("2010", "%C%y", &tm);
52 T_QUIET; T_EXPECT_NOTNULL(result, "2010, %%C%%y");
53 T_EXPECT_EQ(tm.tm_year, 2010 - 1900, NULL);
54
55 T_LOG("20010, %%C%%y");
56 result = strptime("20010", "%C%y", &tm);
57 T_QUIET; T_EXPECT_NOTNULL(result, "20010, %%C%%y");
58 T_EXPECT_EQ(tm.tm_year, 2001 - 1900, NULL);
59
60 T_LOG("+2010, %%C%%y");
61 result = strptime("+2010", "%C%y", &tm);
62 T_QUIET; T_EXPECT_NOTNULL(result, "2010, %%C%%y");
63 T_EXPECT_EQ(tm.tm_year, 201 - 1900, NULL);
64
65 T_LOG("-20100, %%C%%y");
66 result = strptime("-20100", "%C%y", &tm);
67 T_QUIET; T_EXPECT_NOTNULL(result, "-20100, %%C%%y");
68 T_EXPECT_EQ(tm.tm_year, -200 + 1 - 1900, NULL);
69
70 T_LOG("-2-1, %%C%%y");
71 result = strptime("-2-1", "%C%y", &tm);
72 T_QUIET; T_EXPECT_NOTNULL(result, "-2-1, %%C%%y");
73 T_EXPECT_EQ(tm.tm_year, -200 - 1 - 1900, NULL);
74
75 T_LOG("-2+1, %%C%%y");
76 result = strptime("-2+1", "%C%y", &tm);
77 T_QUIET; T_EXPECT_NOTNULL(result, "-2+1, %%C%%y");
78 T_EXPECT_EQ(tm.tm_year, -200 + 1 - 1900, NULL);
79
80 // %Y combinations
81 T_LOG("201, %%Y");
82 result = strptime("201", "%Y", &tm);
83 T_QUIET; T_EXPECT_NOTNULL(result, "201, %%Y");
84 T_EXPECT_EQ(tm.tm_year, 201 - 1900, NULL);
85
86 T_LOG("2001, %%Y");
87 result = strptime("2001", "%Y", &tm);
88 T_QUIET; T_EXPECT_NOTNULL(result, "2001, %%Y");
89 T_EXPECT_EQ(tm.tm_year, 2001 - 1900, NULL);
90
91 T_LOG("20010, %%Y");
92 result = strptime("20010", "%Y", &tm);
93 T_QUIET; T_EXPECT_NOTNULL(result, "20010, %%Y");
94 T_EXPECT_EQ(tm.tm_year, 2001 - 1900, NULL);
95
96 T_LOG("+2010, %%Y");
97 result = strptime("+2010", "%Y", &tm);
98 T_QUIET; T_EXPECT_NOTNULL(result, "+2010, %%Y");
99 T_EXPECT_EQ(tm.tm_year, 201 - 1900, NULL);
100
101 T_LOG("-2010, %%Y");
102 result = strptime("-2010", "%Y", &tm);
103 T_QUIET; T_EXPECT_NOTNULL(result, "-2010, %%Y");
104 T_EXPECT_EQ(tm.tm_year, -201 - 1900, NULL);
105 }
106