• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "strptime_data.h"
21 
22 static int16_t gBufferSize = 256;
23 static time_t gTime = 1659177614;
24 static int16_t gYearBase = 1900;
25 
26 /**
27  * @tc.name      : strptime_0100
28  * @tc.desc      : according to different time zones, convert a string to a time
29  * type according to a specific time format
30  * @tc.level     : Level 0
31  */
strptime_0100(void)32 void strptime_0100(void)
33 {
34     for ( int32_t i = 0; i < (int32_t)(sizeof(test_asctime_data) / sizeof(test_asctime_data[0])); i++) {
35         const char *handlerChar = test_handle_path(test_asctime_data[i].tz);
36         if (!handlerChar) {
37             t_error("strptime_0100 failed: handlerChar is NULL\n");
38             continue;
39         }
40 
41         setenv("TZ", handlerChar, 1);
42         tzset();
43         char buffer[gBufferSize];
44         struct tm *timeptr = localtime(&gTime);
45         if (!timeptr) {
46             EXPECT_PTRNE("strptime_0100", timeptr, NULL);
47             return;
48         }
49         size_t cnt = strftime(buffer, sizeof(buffer) - 1, "%c", timeptr);
50         EXPECT_TRUE("strptime_0100", cnt > 0);
51 
52         struct tm tmTime = {0};
53         strptime(buffer, "%c", &tmTime);
54         char *result = asctime(&tmTime);
55         if (result == NULL) {
56             EXPECT_FALSE("strptime_0100", result == NULL);
57             return;
58         }
59         result[strlen(result) - 1] = 0x00;
60         EXPECT_STREQ("strptime_0100", test_asctime_data[i].result, result);
61     }
62 }
63 
64 /**
65  * @tc.name      : strptime_0200
66  * @tc.desc      : according to different time zones, convert a string to a time
67  * type according to a specific time format
68  * @tc.level     : Level 0
69  */
strptime_0200(void)70 void strptime_0200(void)
71 {
72     for ( int32_t i = 0; i < (int32_t)(sizeof(test_strptime_data)/sizeof(test_strptime_data[0])); i++) {
73         const char *handlerChar = test_handle_path(test_strptime_data[i].tz);
74         if (!handlerChar) {
75             t_error("strptime_0200 failed: handlerChar is NULL\n");
76             continue;
77         }
78 
79         setenv("TZ", handlerChar, 1);
80         tzset();
81         char buffer[gBufferSize];
82         struct tm *timeptr = localtime(&gTime);
83         if (!timeptr) {
84             EXPECT_TRUE("strptime_0200", timeptr == NULL);
85             return;
86         }
87         size_t len = strftime(buffer, sizeof(buffer) - 1, "%c %Z%z", timeptr);
88         EXPECT_TRUE("strptime_0200", len > 0);
89         struct tm tmTime = {0};
90         strptime(buffer, "%c %Z%z", &tmTime);
91         char buffResult[gBufferSize];
92 
93         int cnt = sprintf(buffResult, "%d-%d-%d %d:%d:%d wday=%d,yday=%d,isdst=%d,gmtoff=%ld,zone=%s",
94             (tmTime.tm_year+gYearBase), tmTime.tm_mon, tmTime.tm_mday, tmTime.tm_hour,
95             tmTime.tm_min, tmTime.tm_sec, tmTime.tm_wday, tmTime.tm_yday, tmTime.tm_isdst,
96             tmTime.tm_gmtoff, tmTime.tm_zone);
97         EXPECT_TRUE("strptime_0200", cnt > 0);
98         EXPECT_STREQ("strptime_0200", test_strptime_data[i].result, buffResult);
99     }
100 }
101 
102 /**
103  * @tc.name      : strptime_0300
104  * @tc.desc      : according to different time zones, convert a string to a time
105  * type according to a specific time format
106  * @tc.level     : Level 0
107  */
strptime_0300(void)108 void strptime_0300(void)
109 {
110     char *buffer = "2022-04-10";
111     struct tm tmTime = {0};
112     strptime(buffer, "%F", &tmTime);
113     char buffResult[gBufferSize];
114     int cnt = sprintf(buffResult, "%04d-%02d-%02d",
115         (tmTime.tm_year+gYearBase), tmTime.tm_mon + 1, tmTime.tm_mday);
116     EXPECT_TRUE("strptime_0300", cnt > 0);
117     EXPECT_STREQ("strptime_0300", buffer, buffResult);
118 }
119 
120 /**
121  * @tc.name      : strptime_0400
122  * @tc.desc      : according to different time zones, convert a string to a time
123  * type according to a specific time format
124  * @tc.level     : Level 0
125  */
strptime_0400(void)126 void strptime_0400(void)
127 {
128     char *buffer = "23";
129     struct tm tmTime = {0};
130     strptime(buffer, "%g", &tmTime);
131     char buffResult[gBufferSize];
132     int cnt = sprintf(buffResult, "%d", tmTime.tm_year);
133     EXPECT_TRUE("strptime_0400", cnt > 0);
134     EXPECT_STREQ("strptime_0400", buffer, buffResult);
135 }
136 
137 /**
138  * @tc.name      : strptime_0500
139  * @tc.desc      : according to different time zones, convert a string to a time
140  * type according to a specific time format
141  * @tc.level     : Level 0
142  */
strptime_0500(void)143 void strptime_0500(void)
144 {
145     const char *buffer = "16";
146     struct tm tmTime = {0};
147     strptime(buffer, " %k", &tmTime);
148     char buffResult[gBufferSize];
149     int cnt = sprintf(buffResult, "%d", tmTime.tm_hour);
150     EXPECT_TRUE("strptime_0500", cnt > 0);
151     EXPECT_STREQ("strptime_0500", buffer, buffResult);
152 }
153 
154 /**
155  * @tc.name      : strptime_0600
156  * @tc.desc      : according to different time zones, convert a string to a time
157  * type according to a specific time format
158  * @tc.level     : Level 0
159  */
strptime_0600(void)160 void strptime_0600(void)
161 {
162     const char *buffer = " 4";
163     struct tm tmTime = {0};
164     strptime(buffer, " %l", &tmTime);
165     char buffResult[gBufferSize];
166     int cnt = sprintf(buffResult, "%d", tmTime.tm_hour);
167     EXPECT_TRUE("strptime_0600", cnt > 0);
168     EXPECT_STREQ("strptime_0600", "4", buffResult);
169 }
170 
171 /**
172  * @tc.name      : strptime_0700
173  * @tc.desc      : according to different time zones, convert a string to a time
174  * type according to a specific time format
175  * @tc.level     : Level 0
176  */
strptime_0700(void)177 void strptime_0700(void)
178 {
179     const char *buffer = "1659177614";
180     const char *handlerChar = test_handle_path(test_asctime_data[0].tz);
181     if (!handlerChar) {
182         t_error("strptime_0700 failed: handlerChar is NULL\n");
183         return;
184     }
185 
186     setenv("TZ", handlerChar, 1);
187     time_t second = 0;
188     struct tm tmTime = {0};
189     strptime(buffer, "%s", &tmTime);
190     second = mktime(&tmTime);
191     char buffResult[gBufferSize];
192     int cnt = sprintf(buffResult, "%ld", second);
193     EXPECT_TRUE("strptime_0700", cnt > 0);
194     EXPECT_STREQ("strptime_0700", buffer, buffResult);
195 }
196 
197 /**
198  * @tc.name      : strptime_0800
199  * @tc.desc      : according to different time zones, convert a string to a time
200  * type according to a specific time format
201  * @tc.level     : Level 0
202  */
strptime_0800(void)203 void strptime_0800(void)
204 {
205     const char *buffer = "1";
206     struct tm tmTime = {0};
207     strptime(buffer, "%u", &tmTime);
208     char buffResult[gBufferSize];
209     int cnt = sprintf(buffResult, "%d", tmTime.tm_wday);
210     EXPECT_TRUE("strptime_0800", cnt > 0);
211     EXPECT_STREQ("strptime_0800", buffer, buffResult);
212 }
213 
214 /**
215  * @tc.name      : strptime_0900
216  * @tc.desc      : according to different time zones, convert a string to a time
217  * type according to a specific time format
218  * @tc.level     : Level 0
219  */
strptime_0900(void)220 void strptime_0900(void)
221 {
222     const char *buffer = "30-Oct-2021";
223     struct tm tmTime = {0};
224     strptime(buffer, "%v", &tmTime);
225     char buffResult[gBufferSize];
226     int cnt = sprintf(buffResult, "%d-%d-%d",
227         (tmTime.tm_year+gYearBase), tmTime.tm_mon, tmTime.tm_mday);
228     EXPECT_TRUE("strptime_0900", cnt > 0);
229     EXPECT_STREQ("strptime_0900", "2021-9-30", buffResult);
230 }
231 
main(void)232 int main(void)
233 {
234     strptime_0100();
235     strptime_0200();
236     strptime_0300();
237     strptime_0400();
238     strptime_0500();
239     strptime_0600();
240     strptime_0700();
241     strptime_0800();
242     strptime_0900();
243     return t_status;
244 }