• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits.h>
19 #include "gmtime_data.h"
20 #include "functionalext.h"
21 
22 static time_t gTime = 1659177614;
23 static int16_t gYearBase = 1900;
24 static int16_t gBufferSize = 500;
25 
26 /**
27  * @tc.name      : gmtime_r_0100
28  * @tc.desc      : according to different time zones, convert date and time to GMT time
29  * @tc.level     : Level 0
30  */
gmtime_r_0100(void)31 void gmtime_r_0100(void)
32 {
33     for (int32_t i = 0; i < (int32_t)(sizeof(test_gmtime_data) / sizeof(test_gmtime_data[0])); i++) {
34         const char *handlerChar = test_handle_path(test_gmtime_data[i].tz);
35         if (!handlerChar) {
36             t_error("gmtime_r_0100 failed: handlerChar is NULL\n");
37             continue;
38         }
39 
40         setenv("TZ", handlerChar, 1);
41         tzset();
42         struct tm res = {0};
43         struct tm *gmtm = gmtime_r(&gTime, &res);
44         char buff[gBufferSize];
45         int cnt = sprintf(buff, "%d-%d-%d %d:%d:%d wday=%d,yday=%d,isdst=%d,gmtoff=%ld,zone=%s",
46             (gmtm->tm_year+gYearBase), gmtm->tm_mon, gmtm->tm_mday, gmtm->tm_hour,
47             gmtm->tm_min, gmtm->tm_sec, gmtm->tm_wday, gmtm->tm_yday, gmtm->tm_isdst,
48             gmtm->tm_gmtoff, gmtm->tm_zone);
49         EXPECT_TRUE("gmtime_r_0100", cnt > 0);
50         EXPECT_STREQ("gmtime_r_0100", test_gmtime_data[i].result, buff);
51     }
52 }
53 
54 /**
55  * @tc.name      : gmtime_r_0200
56  * @tc.desc      : test gmtime_r() with invalid input paramter
57  * @tc.level     : Level 2
58  */
gmtime_r_0200(void)59 void gmtime_r_0200(void)
60 {
61     time_t invalid_time = INT_MAX * 31622400LL + 1;
62     struct tm res = {0};
63     struct tm *gmtm = gmtime_r(&invalid_time, &res);
64     EXPECT_TRUE("gmtime_r_0200", NULL == (void *)gmtm);
65 }
66 
main(void)67 int main(void)
68 {
69     gmtime_r_0100();
70     gmtime_r_0200();
71     return t_status;
72 }