• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2021 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 "time_zone_info.h"
17 
18 #include "time_file_utils.h"
19 
20 namespace OHOS {
21 namespace MiscServices {
22 namespace {
23 constexpr const char *TIMEZONE_KEY = "persist.time.timezone";
24 constexpr const char *TIMEZONE_PATH = "/system/etc/zoneinfo/";
25 const int TIMEZONE_OK = 0;
26 const int CONFIG_LEN = 35;
27 const int HOUR_TO_MIN = 60;
28 } // namespace
29 
TimeZoneInfo()30 TimeZoneInfo::TimeZoneInfo()
31 {
32 }
33 
~TimeZoneInfo()34 TimeZoneInfo::~TimeZoneInfo()
35 {
36 }
37 
Init()38 void TimeZoneInfo::Init()
39 {
40     TIME_HILOGD(TIME_MODULE_SERVICE, "Start.");
41     char value[CONFIG_LEN] = "Asia/Shanghai";
42     if (GetParameter(TIMEZONE_KEY, "", value, CONFIG_LEN) < TIMEZONE_OK) {
43         TIME_HILOGW(TIME_MODULE_SERVICE, "No found timezone from system parameter.");
44     }
45     if (!SetTimezone(value)) {
46         TIME_HILOGE(TIME_MODULE_SERVICE, "Init Set kernel failed.");
47     }
48     curTimezoneId_ = value;
49     TIME_HILOGD(TIME_MODULE_SERVICE, "Timezone value: %{public}s", value);
50 }
51 
SetTimezone(const std::string & timezoneId)52 bool TimeZoneInfo::SetTimezone(const std::string &timezoneId)
53 {
54     TIME_HILOGD(TIME_MODULE_SERVICE, "Set timezone");
55     std::lock_guard<std::mutex> lock(timezoneMutex_);
56     if (curTimezoneId_ == timezoneId) {
57         TIME_HILOGI(TIME_MODULE_SERVICE, "Same Timezone has been set.");
58         return true;
59     }
60     if (!TimeFileUtils::IsExistFile(std::string(TIMEZONE_PATH).append(timezoneId))) {
61         TIME_HILOGE(TIME_MODULE_SERVICE, "Invalid timezone");
62         return false;
63     }
64     setenv("TZ", timezoneId.c_str(), 1);
65     tzset();
66     if (!SetTimezoneToKernel()) {
67         TIME_HILOGE(TIME_MODULE_SERVICE, "SetTimezone Set kernel failed.");
68         return false;
69     }
70     auto errNo = SetParameter(TIMEZONE_KEY, timezoneId.c_str());
71     if (errNo > TIMEZONE_OK) {
72         TIME_HILOGE(TIME_MODULE_SERVICE, "SetTimezone timezoneId: %{public}d: %{public}s", errNo, timezoneId.c_str());
73         return false;
74     }
75     curTimezoneId_ = timezoneId;
76     return true;
77 }
78 
GetTimezone(std::string & timezoneId)79 bool TimeZoneInfo::GetTimezone(std::string &timezoneId)
80 {
81     timezoneId = curTimezoneId_;
82     return true;
83 }
84 
SetTimezoneToKernel()85 bool TimeZoneInfo::SetTimezoneToKernel()
86 {
87     time_t t = time(nullptr);
88     struct tm *localTime = localtime(&t);
89     struct timezone tz {};
90     tz.tz_minuteswest = localTime->tm_gmtoff / HOUR_TO_MIN;
91     tz.tz_dsttime = 0;
92     int result = settimeofday(nullptr, &tz);
93     if (result < 0) {
94         TIME_HILOGE(TIME_MODULE_SERVICE, "Settimeofday timezone fail: %{public}d.", result);
95         return false;
96     }
97     TIME_HILOGD(TIME_MODULE_SERVICE, "Settimeofday timezone success ");
98     return true;
99 }
100 } // namespace MiscServices
101 } // namespace OHOS