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 extern time_t __timegm_time64(struct tm *);
22
23 static time_t gTime = 1659177614;
24
25 /**
26 * @tc.name : timegm_0100
27 * @tc.desc : according to different time zones, return a timestamp from 1970-1-1 to the specified date
28 * @tc.level : Level 0
29 */
timegm_0100(void)30 void timegm_0100(void)
31 {
32 time_t timeThis;
33 for (int32_t i = 0; i < (int32_t)(sizeof(test_timegm_data) / sizeof(test_timegm_data[0])); i++) {
34 const char *handlerChar = test_handle_path(test_timegm_data[i].tz);
35 if (!handlerChar) {
36 t_error("timegm_0100 failed: handlerChar is NULL\n");
37 continue;
38 }
39
40 setenv("TZ", handlerChar, 1);
41 tzset();
42 struct tm *timeptr = localtime(&gTime);
43 if (!timeptr) {
44 EXPECT_PTRNE("timegm_0100", timeptr, NULL);
45 return;
46 }
47 timeThis = timegm(timeptr);
48 EXPECT_EQ("timegm_0100", test_timegm_data[i].result, timeThis);
49 }
50 }
51
52 /**
53 * @tc.name : timegm_time64_0100
54 * @tc.desc : according to different time zones, return a timestamp from 1970-1-1 to the specified date
55 * @tc.level : Level 0
56 */
timegm_time64_0100(void)57 void timegm_time64_0100(void)
58 {
59 time_t timeThis;
60 for (int32_t i = 0; i < (int32_t)(sizeof(test_timegm_data) / sizeof(test_timegm_data[0])); i++) {
61 const char *handlerChar = test_handle_path(test_timegm_data[i].tz);
62 if (!handlerChar) {
63 t_error("timegm_time64_0100 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("timegm_time64_0100", timeptr, NULL);
72 return;
73 }
74 timeThis = __timegm_time64(timeptr);
75 EXPECT_EQ("timegm_time64_0100", test_timegm_data[i].result, timeThis);
76 }
77 }
78
main(void)79 int main(void)
80 {
81 timegm_0100();
82 timegm_time64_0100();
83 return t_status;
84 }