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 "localtime_data.h"
19 #include "functionalext.h"
20
21 static time_t gTime = 1659177614;
22 static int16_t gYearBase = 1900;
23 static int16_t gBufferSize = 500;
24
25 extern struct tm *__localtime64 (const time_t *);
26
27 /**
28 * @tc.name : localtime_0100
29 * @tc.desc : according to different time zones, converts the time in seconds from 1970-1-1 0:00
30 * to the current time system offset to local time
31 * @tc.level : Level 0
32 */
localtime_0100(void)33 void localtime_0100(void)
34 {
35 for (int32_t i = 0; i < (int32_t)(sizeof(test_localtime_data) / sizeof(test_localtime_data[0])); i++) {
36 const char *handlerChar = test_handle_path(test_localtime_data[i].tz);
37 if (!handlerChar) {
38 t_error("localtime_0100 failed: handlerChar is NULL\n");
39 continue;
40 }
41
42 setenv("TZ", handlerChar, 1);
43 tzset();
44 struct tm *localtm;
45 localtm = localtime(&gTime);
46 char buff[gBufferSize];
47 int cnt = sprintf(buff, "%d-%d-%d %d:%d:%d wday=%d,yday=%d,isdst=%d,gmtoff=%ld,zone=%s",
48 (localtm->tm_year+gYearBase), localtm->tm_mon, localtm->tm_mday, localtm->tm_hour,
49 localtm->tm_min, localtm->tm_sec, localtm->tm_wday, localtm->tm_yday, localtm->tm_isdst,
50 localtm->tm_gmtoff, localtm->tm_zone);
51 EXPECT_TRUE("localtime_0100", cnt > 0);
52 EXPECT_STREQ("localtime_0100", test_localtime_data[i].result, buff);
53 }
54 }
55
56 /**
57 * @tc.name : localtime64_0100
58 * @tc.desc : according to different time zones, converts the time in seconds from 1970-1-1 0:00
59 * to the current time system offset to local time
60 * @tc.level : Level 0
61 */
localtime64_0100(void)62 void localtime64_0100(void)
63 {
64 for (int32_t i = 0; i < (int32_t)(sizeof(test_localtime_data) / sizeof(test_localtime_data[0])); i++) {
65 const char *handlerChar = test_handle_path(test_localtime_data[i].tz);
66 if (!handlerChar) {
67 t_error("localtime64_0100 failed: handlerChar is NULL\n");
68 continue;
69 }
70
71 setenv("TZ", handlerChar, 1);
72 tzset();
73 struct tm *localtm;
74 localtm = __localtime64(&gTime);
75 char buff[gBufferSize];
76 int cnt = sprintf(buff, "%d-%d-%d %d:%d:%d wday=%d,yday=%d,isdst=%d,gmtoff=%ld,zone=%s",
77 (localtm->tm_year+gYearBase), localtm->tm_mon, localtm->tm_mday, localtm->tm_hour,
78 localtm->tm_min, localtm->tm_sec, localtm->tm_wday, localtm->tm_yday, localtm->tm_isdst,
79 localtm->tm_gmtoff, localtm->tm_zone);
80 EXPECT_TRUE("localtime64_0100", cnt > 0);
81 EXPECT_STREQ("localtime64_0100", test_localtime_data[i].result, buff);
82 }
83 }
84
main(void)85 int main(void)
86 {
87 localtime_0100();
88 localtime64_0100();
89 return t_status;
90 }