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_r (const time_t *__restrict, struct tm *__restrict);
26
27 /**
28 * @tc.name : localtime_r_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_r_0100(void)33 void localtime_r_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_r_0100 failed: handlerChar is NULL\n");
39 continue;
40 }
41
42 setenv("TZ", handlerChar, 1);
43 tzset();
44 struct tm *localtm = NULL;
45 struct tm res = {0};
46 localtm = localtime_r(&gTime, &res);
47 char buff[gBufferSize];
48 int cnt = sprintf(buff, "%d-%d-%d %d:%d:%d wday=%d,yday=%d,isdst=%d,gmtoff=%ld,zone=%s",
49 (localtm->tm_year+gYearBase), localtm->tm_mon, localtm->tm_mday, localtm->tm_hour,
50 localtm->tm_min, localtm->tm_sec, localtm->tm_wday, localtm->tm_yday, localtm->tm_isdst,
51 localtm->tm_gmtoff, localtm->tm_zone);
52 EXPECT_TRUE("localtime_r_0100", cnt > 0);
53 EXPECT_STREQ("localtime_r_0100", test_localtime_data[i].result, buff);
54 }
55 }
56
57 /**
58 * @tc.name : localtime64_r_0100
59 * @tc.desc : according to different time zones, converts the time in seconds from 1970-1-1 0:00
60 * to the current time system offset to local time
61 * @tc.level : Level 0
62 */
localtime64_r_0100(void)63 void localtime64_r_0100(void)
64 {
65 for (int32_t i = 0; i < (int32_t)(sizeof(test_localtime_data) / sizeof(test_localtime_data[0])); i++) {
66 const char *handlerChar = test_handle_path(test_localtime_data[i].tz);
67 if (!handlerChar) {
68 t_error("localtime_r_0100 failed: handlerChar is NULL\n");
69 continue;
70 }
71
72 setenv("TZ", handlerChar, 1);
73 tzset();
74 struct tm *localtm = NULL;
75 struct tm res = {0};
76 localtm = __localtime64_r(&gTime, &res);
77 char buff[gBufferSize];
78 int cnt = sprintf(buff, "%d-%d-%d %d:%d:%d wday=%d,yday=%d,isdst=%d,gmtoff=%ld,zone=%s",
79 (localtm->tm_year+gYearBase), localtm->tm_mon, localtm->tm_mday, localtm->tm_hour,
80 localtm->tm_min, localtm->tm_sec, localtm->tm_wday, localtm->tm_yday, localtm->tm_isdst,
81 localtm->tm_gmtoff, localtm->tm_zone);
82 EXPECT_TRUE("localtime64_r_0100", cnt > 0);
83 EXPECT_STREQ("localtime64_r_0100", test_localtime_data[i].result, buff);
84 }
85 }
86
main(void)87 int main(void)
88 {
89 localtime_r_0100();
90 localtime64_r_0100();
91 return t_status;
92 }