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 "mktime_data.h"
20
21static time_t gTime = 1659177614;
22
23extern time_t __mktime64 (struct tm *);
24
25/**
26 * @tc.name      : mktime_0100
27 * @tc.desc      : according to different time zones, convert time to seconds in duration since 1970-1-1
28 * @tc.level     : Level 0
29 */
30void mktime_0100(void)
31{
32    for (int32_t i = 0; i < (int32_t)(sizeof(test_mktime_data) / sizeof(test_mktime_data[0])); i++) {
33        const char *handlerChar = test_handle_path(test_mktime_data[i].tz);
34        if (!handlerChar) {
35            t_error("mktime_0100 failed: handlerChar is NULL\n");
36            continue;
37        }
38
39        setenv("TZ", handlerChar, 1);
40        tzset();
41        struct tm *timeptr = localtime(&gTime);
42        if (!timeptr) {
43            EXPECT_PTRNE("mktime_0100", timeptr, NULL);
44            return;
45        }
46        time_t mk = mktime(timeptr);
47        EXPECT_EQ("mktime_0100", mk, test_mktime_data[i].result);
48    }
49}
50
51/**
52 * @tc.name      : mktime64_0100
53 * @tc.desc      : according to different time zones, convert time to seconds in duration since 1970-1-1
54 * @tc.level     : Level 0
55 */
56void mktime64_0100(void)
57{
58    for (int32_t i = 0; i < (int32_t)(sizeof(test_mktime_data) / sizeof(test_mktime_data[0])); i++) {
59        const char *handlerChar = test_handle_path(test_mktime_data[i].tz);
60        if (!handlerChar) {
61            t_error("mktime64_0100 failed: handlerChar is NULL\n");
62            continue;
63        }
64
65        setenv("TZ", handlerChar, 1);
66        tzset();
67        struct tm *timeptr = localtime(&gTime);
68        if (!timeptr) {
69            EXPECT_PTRNE("mktime64_0100", timeptr, NULL);
70            return;
71        }
72        time_t mk = __mktime64(timeptr);
73        EXPECT_EQ("mktime64_0100", mk, test_mktime_data[i].result);
74    }
75}
76
77int main(void)
78{
79    mktime_0100();
80    mktime64_0100();
81    return t_status;
82}