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