• 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 <fstream>
17 #include <sys/stat.h>
18 #include "time_zone_info.h"
19 #include "ipc_skeleton.h"
20 #include "time_sysevent.h"
21 
22 namespace OHOS {
23 namespace MiscServices {
24 namespace {
25 constexpr const char *TIMEZONE_KEY = "persist.time.timezone";
26 constexpr const char *TIMEZONE_LIST_CONFIG_PATH = "/system/etc/zoneinfo/timezone_list.cfg";
27 constexpr const char *DISTRO_TIMEZONE_LIST_CONFIG = "/system/etc/tzdata_distro/timezone_list.cfg";
28 constexpr int TIMEZONE_OK = 0;
29 constexpr int CONFIG_LEN = 35;
30 constexpr int HOUR_TO_MIN = 60;
31 } // namespace
32 
GetInstance()33 TimeZoneInfo &TimeZoneInfo::GetInstance()
34 {
35     static TimeZoneInfo instance;
36     return instance;
37 }
38 
Init()39 void TimeZoneInfo::Init()
40 {
41     TIME_HILOGD(TIME_MODULE_SERVICE, "Start");
42     char value[CONFIG_LEN] = "Asia/Shanghai";
43     if (GetParameter(TIMEZONE_KEY, "", value, CONFIG_LEN) < TIMEZONE_OK) {
44         TIME_HILOGW(TIME_MODULE_SERVICE, "No found timezone from system parameter");
45     }
46     if (!SetTimezone(value)) {
47         TIME_HILOGE(TIME_MODULE_SERVICE, "Init Set kernel failed");
48     }
49     curTimezoneId_ = value;
50     TIME_HILOGD(TIME_MODULE_SERVICE, "Timezone value: %{public}s", value);
51 }
52 
SetTimezone(const std::string & timezoneId)53 bool TimeZoneInfo::SetTimezone(const std::string &timezoneId)
54 {
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     TIME_HILOGI(TIME_MODULE_SERVICE, "Set timezone : %{public}s, Current timezone : %{public}s, uid: %{public}d",
61         timezoneId.c_str(), curTimezoneId_.c_str(), IPCSkeleton::GetCallingUid());
62     std::set<std::string> availableTimeZoneIDs = GetTimeZoneAvailableIDs();
63     if (availableTimeZoneIDs.find(timezoneId) == availableTimeZoneIDs.end()) {
64         TIME_HILOGE(TIME_MODULE_SERVICE, "Invalid timezone");
65         return false;
66     }
67     setenv("TZ", timezoneId.c_str(), 1);
68     tzset();
69     if (!SetTimezoneToKernel()) {
70         TIME_HILOGE(TIME_MODULE_SERVICE, "SetTimezone Set kernel failed");
71         return false;
72     }
73     auto errNo = SetParameter(TIMEZONE_KEY, timezoneId.c_str());
74     if (errNo > TIMEZONE_OK) {
75         TIME_HILOGE(TIME_MODULE_SERVICE, "SetTimezone timezoneId: %{public}d: %{public}s", errNo, timezoneId.c_str());
76         return false;
77     }
78     curTimezoneId_ = timezoneId;
79     TimeBehaviorReport(ReportEventCode::SET_TIMEZONE, curTimezoneId_, timezoneId, 0);
80     return true;
81 }
82 
GetTimeZoneAvailableIDs()83 std::set<std::string> TimeZoneInfo::GetTimeZoneAvailableIDs()
84 {
85     std::set<std::string> availableTimeZoneIDs;
86     struct stat s;
87     const char *tzIdConfigPath = stat(DISTRO_TIMEZONE_LIST_CONFIG, &s) == 0 ?
88         DISTRO_TIMEZONE_LIST_CONFIG : TIMEZONE_LIST_CONFIG_PATH;
89     std::unique_ptr<char[]> resolvedPath = std::make_unique<char[]>(PATH_MAX + 1);
90     if (realpath(tzIdConfigPath, resolvedPath.get()) == nullptr) {
91         TIME_HILOGE(TIME_MODULE_SERVICE, "Get realpath failed, errno: %{public}d", errno);
92         return availableTimeZoneIDs;
93     }
94     std::ifstream file(resolvedPath.get());
95     if (!file.good()) {
96         TIME_HILOGE(TIME_MODULE_SERVICE, "Open timezone list config file failed");
97         return availableTimeZoneIDs;
98     }
99     std::string line;
100     while (std::getline(file, line)) {
101         if (line.length() == 0) {
102             break;
103         }
104         line.resize(line.find_last_not_of("\r\n") + 1);
105         availableTimeZoneIDs.insert(line);
106     }
107     file.close();
108     return availableTimeZoneIDs;
109 }
110 
GetTimezone(std::string & timezoneId)111 bool TimeZoneInfo::GetTimezone(std::string &timezoneId)
112 {
113     timezoneId = curTimezoneId_;
114     return true;
115 }
116 
SetTimezoneToKernel()117 bool TimeZoneInfo::SetTimezoneToKernel()
118 {
119     time_t t = time(nullptr);
120     struct tm *localTime = localtime(&t);
121     struct timezone tz {};
122     if (localTime == nullptr) {
123         TIME_HILOGE(TIME_MODULE_SERVICE, "localtime is nullptr errornum: %{public}s", strerror(errno));
124         return false;
125     }
126     tz.tz_minuteswest = -localTime->tm_gmtoff / HOUR_TO_MIN;
127     tz.tz_dsttime = 0;
128     int result = settimeofday(nullptr, &tz);
129     if (result < 0) {
130         TIME_HILOGE(TIME_MODULE_SERVICE, "Settimeofday timezone fail: %{public}d", result);
131         return false;
132     }
133     TIME_HILOGD(TIME_MODULE_SERVICE, "Settimeofday timezone success");
134     return true;
135 }
136 } // namespace MiscServices
137 } // namespace OHOS