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 20#define __TEST_DATA_YEAR__ 121 21#define __TEST_DATA_MONTH__ 9 22#define __TEST_DATA_DAY__ 3 23#define __TEST_DATA_HOUR__ 9 24#define __TEST_DATA_MINUTE__ 12 25 26struct time_struct { 27 const char *tz; 28}; 29 30static const struct time_struct gResultData[] = { 31 {"Asia/Shanghai"}, 32 {"Asia/Tokyo"}, 33 {"Europe/Moscow"}, 34 {"America/New_York"}, 35 {"Europe/Berlin"}, 36}; 37 38/** 39 * @tc.name : time_0100 40 * @tc.desc : according to different time zones, get time 41 * @tc.level : Level 0 42 */ 43void time_0100(void) 44{ 45 for (int32_t i = 0; i < (int32_t)(sizeof(gResultData) / sizeof(gResultData[0])); i++) { 46 const char *tz = gResultData[i].tz; 47 const char *handlerChar; 48#ifdef TIME_ZONE_SUB_TAG 49 char *str = strrchr(tz, TIME_ZONE_SUB_TAG); 50 if (str) { 51 handlerChar = ++str; 52 } else { 53 handlerChar = tz; 54 } 55#else 56 handlerChar = tz; 57#endif 58 setenv("TZ", handlerChar, 1); 59 tzset(); 60 system("date '2021-10-3 9:12:12' > /dev/NULL"); 61 time_t curClock; 62 time_t t = time(&curClock); 63 EXPECT_TRUE("time_0100", t > 0); 64 struct tm *localtm = localtime(&curClock); 65 if (!localtm) { 66 EXPECT_PTRNE("time_0100", localtm, NULL); 67 return; 68 } 69 EXPECT_EQ("time_0100", __TEST_DATA_YEAR__, localtm->tm_year); 70 EXPECT_EQ("time_0100", __TEST_DATA_MONTH__, localtm->tm_mon); 71 EXPECT_EQ("time_0100", __TEST_DATA_DAY__, localtm->tm_mday); 72 EXPECT_EQ("time_0100", __TEST_DATA_HOUR__, localtm->tm_hour); 73 EXPECT_EQ("time_0100", __TEST_DATA_MINUTE__, localtm->tm_min); 74 } 75} 76 77int main(void) 78{ 79 time_0100(); 80 return t_status; 81}