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 "functionalext.h"
19 #include "timegm_data.h"
20
21 #define __TEST_DATA_YEAR__ 121
22 #define __TEST_DATA_MONTH__ 9
23 #define __TEST_DATA_DAY__ 3
24 #define __TEST_DATA_HOUR__ 9
25 #define __TEST_DATA_MINUTE__ 12
26
27 extern time_t __time64(time_t *);
28
29 struct time_struct {
30 const char *tz;
31 };
32
33 static const struct time_struct gResultData[] = {
34 {"Asia/Shanghai"},
35 {"Asia/Tokyo"},
36 {"Europe/Moscow"},
37 {"America/New_York"},
38 {"Europe/Berlin"},
39 };
40
41 /**
42 * @tc.name : time_0100
43 * @tc.desc : according to different time zones, get time
44 * @tc.level : Level 0
45 */
time_0100(void)46 void time_0100(void)
47 {
48 for (int32_t i = 0; i < (int32_t)(sizeof(gResultData) / sizeof(gResultData[0])); i++) {
49 const char *handlerChar = test_handle_path(gResultData[i].tz);
50 if (!handlerChar) {
51 t_error("time_0100 failed: handlerChar is NULL\n");
52 continue;
53 }
54
55 setenv("TZ", handlerChar, 1);
56 tzset();
57 system("date '2021-10-3 9:12:12' > /dev/NULL");
58 time_t curClock;
59 time_t t = time(&curClock);
60 EXPECT_TRUE("time_0100", t > 0);
61 struct tm *localtm = localtime(&curClock);
62 if (!localtm) {
63 EXPECT_PTRNE("time_0100", localtm, NULL);
64 return;
65 }
66 EXPECT_EQ("time_0100", __TEST_DATA_YEAR__, localtm->tm_year);
67 EXPECT_EQ("time_0100", __TEST_DATA_MONTH__, localtm->tm_mon);
68 EXPECT_EQ("time_0100", __TEST_DATA_DAY__, localtm->tm_mday);
69 EXPECT_EQ("time_0100", __TEST_DATA_HOUR__, localtm->tm_hour);
70 EXPECT_EQ("time_0100", __TEST_DATA_MINUTE__, localtm->tm_min);
71 }
72 }
73
74 /**
75 * @tc.name : time64_0100
76 * @tc.desc : according to different time zones, get time
77 * @tc.level : Level 0
78 */
time64_0100(void)79 void time64_0100(void)
80 {
81 for (int32_t i = 0; i < (int32_t)(sizeof(gResultData) / sizeof(gResultData[0])); i++) {
82 const char *handlerChar = test_handle_path(gResultData[i].tz);
83 if (!handlerChar) {
84 t_error("time64_0100 failed: handlerChar is NULL\n");
85 continue;
86 }
87
88 setenv("TZ", handlerChar, 1);
89 tzset();
90 system("date '2021-10-3 9:12:12' > /dev/NULL");
91 time_t curClock;
92 time_t t = __time64(&curClock);
93 EXPECT_TRUE("time64_0100", t > 0);
94 struct tm *localtm = localtime(&curClock);
95 if (!localtm) {
96 EXPECT_PTRNE("time64_0100", localtm, NULL);
97 return;
98 }
99 EXPECT_EQ("time64_0100", __TEST_DATA_YEAR__, localtm->tm_year);
100 EXPECT_EQ("time64_0100", __TEST_DATA_MONTH__, localtm->tm_mon);
101 EXPECT_EQ("time64_0100", __TEST_DATA_DAY__, localtm->tm_mday);
102 EXPECT_EQ("time64_0100", __TEST_DATA_HOUR__, localtm->tm_hour);
103 EXPECT_EQ("time64_0100", __TEST_DATA_MINUTE__, localtm->tm_min);
104 }
105 }
106
main(void)107 int main(void)
108 {
109 time_0100();
110 time64_0100();
111 return t_status;
112 }