1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdlib.h>
17 #include <time.h>
18 #include "asctime_data.h"
19 #include "functionalext.h"
20 #include "strftime_data.h"
21
22 static time_t gTime = 1659177614;
23 static int16_t gBufferSize = 256;
24
25 /**
26 * @tc.name : strftime_0100
27 * @tc.desc : according to different time zones, format date
28 * @tc.level : Level 0
29 */
strftime_0100(void)30 void strftime_0100(void)
31 {
32 for (int32_t i = 0; i < (int32_t)(sizeof(test_asctime_data) / sizeof(test_asctime_data[0])); i++) {
33 const char *handlerChar = test_handle_path(test_asctime_data[i].tz);
34 if (!handlerChar) {
35 t_error("strftime_0100 failed: handlerChar is NULL\n");
36 continue;
37 }
38
39 setenv("TZ", handlerChar, 1);
40 tzset();
41 char buffer[gBufferSize];
42 struct tm *timeptr = localtime(&gTime);
43 if (!timeptr) {
44 EXPECT_PTRNE("strftime_0100", timeptr, NULL);
45 return;
46 }
47 size_t count = strftime(buffer, sizeof(buffer) - 1, "%c", timeptr);
48 EXPECT_TRUE("strftime_0100", count > 0);
49 EXPECT_STREQ("strftime_0100", buffer, test_asctime_data[i].result);
50 }
51 }
52
53 /**
54 * @tc.name : strftime_0200
55 * @tc.desc : according to different time zones, format date
56 * @tc.level : Level 0
57 */
strftime_0200(void)58 void strftime_0200(void)
59 {
60 for (int32_t i = 0; i < (int32_t)(sizeof(test_strftime_data) / sizeof(test_strftime_data[0])); i++) {
61 const char *handlerChar = test_handle_path(test_strftime_data[i].tz);
62 if (!handlerChar) {
63 t_error("strftime_0200 failed: handlerChar is NULL\n");
64 continue;
65 }
66
67 setenv("TZ", handlerChar, 1);
68 tzset();
69 struct tm *timeptr = localtime(&gTime);
70 if (!timeptr) {
71 EXPECT_PTRNE("strftime_0200", timeptr, NULL);
72 return;
73 }
74 char buffer[gBufferSize];
75 size_t count = strftime(buffer, sizeof(buffer) - 1, "%c %Z%z", timeptr);
76 EXPECT_TRUE("strftime_0200", count > 0);
77 EXPECT_STREQ("strftime_0200", buffer, test_strftime_data[i].result);
78 }
79 }
80
81 /**
82 * @tc.name : strftime_0300
83 * @tc.desc : according to different time zones, format date
84 * @tc.level : Level 0
85 */
strftime_0300(void)86 void strftime_0300(void)
87 {
88 const char *handlerChar = test_handle_path("Pacific/Pitcairn");
89 if (!handlerChar) {
90 t_error("strftime_0300 failed: handlerChar is NULL\n");
91 return;
92 }
93
94 setenv("TZ", handlerChar, 1);
95 tzset();
96 struct tm *timeptr = localtime(&gTime);
97 if (!timeptr) {
98 EXPECT_PTRNE("strftime_0300", timeptr, NULL);
99 return;
100 }
101 char buffer[gBufferSize];
102 size_t count = strftime(buffer, sizeof(buffer) - 1, "%k", timeptr);
103 EXPECT_TRUE("strftime_0300", count > 0);
104 EXPECT_STREQ("strftime_0300", buffer, " 2");
105 }
106
107 /**
108 * @tc.name : strftime_0400
109 * @tc.desc : according to different time zones, format date
110 * @tc.level : Level 0
111 */
strftime_0400(void)112 void strftime_0400(void)
113 {
114 const char *handlerChar = test_handle_path("Asia/Shanghai");
115 if (!handlerChar) {
116 t_error("strftime_0400 failed: handlerChar is NULL\n");
117 return;
118 }
119
120 setenv("TZ", handlerChar, 1);
121 tzset();
122 struct tm *timeptr = localtime(&gTime);
123 if (!timeptr) {
124 EXPECT_PTRNE("strftime_0400", timeptr, NULL);
125 return;
126 }
127 char buffer[gBufferSize];
128 size_t count = strftime(buffer, sizeof(buffer) - 1, "%k", timeptr);
129 EXPECT_TRUE("strftime_0400", count > 0);
130 EXPECT_STREQ("strftime_0400", buffer, "18");
131 }
132
133 /**
134 * @tc.name : strftime_0500
135 * @tc.desc : according to different time zones, format date
136 * @tc.level : Level 0
137 */
strftime_0500(void)138 void strftime_0500(void)
139 {
140 const char *handlerChar = test_handle_path("Asia/Shanghai");
141 if (!handlerChar) {
142 t_error("strftime_0500 failed: handlerChar is NULL\n");
143 return;
144 }
145
146 setenv("TZ", handlerChar, 1);
147 tzset();
148 struct tm *timeptr = localtime(&gTime);
149 if (!timeptr) {
150 EXPECT_PTRNE("strftime_0500", timeptr, NULL);
151 return;
152 }
153 char buffer[gBufferSize];
154 size_t count = strftime(buffer, sizeof(buffer) - 1, "%I", timeptr);
155 EXPECT_TRUE("strftime_0500", count > 0);
156 EXPECT_STREQ("strftime_0500", buffer, "06");
157 }
158
159 /**
160 * @tc.name : strftime_0600
161 * @tc.desc : according to different time zones, format date
162 * @tc.level : Level 0
163 */
strftime_0600(void)164 void strftime_0600(void)
165 {
166 const char *handlerChar = test_handle_path("Asia/Shanghai");
167 if (!handlerChar) {
168 t_error("strftime_0600 failed: handlerChar is NULL\n");
169 return;
170 }
171
172 setenv("TZ", handlerChar, 1);
173 tzset();
174 struct tm *timeptr = localtime(&gTime);
175 if (!timeptr) {
176 EXPECT_PTRNE("strftime_0600", timeptr, NULL);
177 return;
178 }
179 char buffer[gBufferSize];
180 size_t count = strftime(buffer, sizeof(buffer) - 1, "%P", timeptr);
181 EXPECT_TRUE("strftime_0600", count > 0);
182 EXPECT_STREQ("strftime_0600", buffer, "pm");
183 }
184
185 /**
186 * @tc.name : strftime_0700
187 * @tc.desc : according to different time zones, format date
188 * @tc.level : Level 0
189 */
strftime_0700(void)190 void strftime_0700(void)
191 {
192 const char *handlerChar = test_handle_path("Asia/Shanghai");
193 if (!handlerChar) {
194 t_error("strftime_0700 failed: handlerChar is NULL\n");
195 return;
196 }
197
198 setenv("TZ", handlerChar, 1);
199 tzset();
200 struct tm *timeptr = localtime(&gTime);
201 if (!timeptr) {
202 EXPECT_PTRNE("strftime_0700", timeptr, NULL);
203 return;
204 }
205 char buffer[gBufferSize];
206 size_t count = strftime(buffer, sizeof(buffer) - 1, "%v", timeptr);
207 EXPECT_TRUE("strftime_0700", count > 0);
208 EXPECT_STREQ("strftime_0700", buffer, "30-Jul-2022");
209 }
210
main(void)211 int main(void)
212 {
213 strftime_0100();
214 strftime_0200();
215 strftime_0300();
216 strftime_0400();
217 strftime_0500();
218 strftime_0600();
219 strftime_0700();
220 return t_status;
221 }