-
Notifications
You must be signed in to change notification settings - Fork 346
/
timezone.py
182 lines (134 loc) · 5.06 KB
/
timezone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#
# Copyright (C) 2012 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
"""
Module providing functions for getting the list of timezones, writing timezone
configuration, valid timezones recognition etc.
"""
import langtable
import zoneinfo
from collections import OrderedDict
from functools import cache
from pyanaconda.core import util
from pyanaconda.core.constants import THREAD_STORAGE
from pyanaconda.flags import flags
from pyanaconda.modules.common.constants.objects import BOOTLOADER
from pyanaconda.modules.common.constants.services import TIMEZONE, STORAGE
from pyanaconda.modules.common.util import is_module_available
from pyanaconda.threading import threadMgr
from blivet import arch
from pyanaconda.anaconda_loggers import get_module_logger
log = get_module_logger(__name__)
# The Etc category in zoneinfo.available_timezones() includes some more,
# however confusing ones (like UCT, GMT+0, GMT-0,...)
ETC_ZONES = ['GMT+1', 'GMT+2', 'GMT+3', 'GMT+4', 'GMT+5', 'GMT+6', 'GMT+7',
'GMT+8', 'GMT+9', 'GMT+10', 'GMT+11', 'GMT+12',
'GMT-1', 'GMT-2', 'GMT-3', 'GMT-4', 'GMT-5', 'GMT-6', 'GMT-7',
'GMT-8', 'GMT-9', 'GMT-10', 'GMT-11', 'GMT-12', 'GMT-13',
'GMT-14', 'UTC', 'GMT']
NTP_PACKAGE = "chrony"
NTP_SERVICE = "chronyd"
def time_initialize(timezone_proxy):
"""
Try to guess if RTC uses UTC time or not, set timezone.isUtc properly and
set system time from RTC using the UTC guess.
Guess is done by searching for bootable ntfs devices.
:param timezone_proxy: DBus proxy of the timezone module
"""
if arch.is_s390():
# nothing to do on s390(x) were hwclock doesn't exist
return
if not timezone_proxy.IsUTC and not flags.automatedInstall:
# if set in the kickstart, no magic needed here
threadMgr.wait(THREAD_STORAGE)
bootloader_proxy = STORAGE.get_proxy(BOOTLOADER)
is_utc = not bootloader_proxy.DetectWindows()
timezone_proxy.SetIsUTC(is_utc)
cmd = "hwclock"
args = ["--hctosys"]
if timezone_proxy.IsUTC:
args.append("--utc")
else:
args.append("--localtime")
util.execWithRedirect(cmd, args)
def save_hw_clock(timezone_proxy=None):
"""
Save system time to HW clock.
:param timezone_proxy: DBus proxy of the timezone module
"""
if arch.is_s390():
return
if not is_module_available(TIMEZONE):
return
if not timezone_proxy:
timezone_proxy = TIMEZONE.get_proxy()
cmd = "hwclock"
args = ["--systohc"]
if timezone_proxy.IsUTC:
args.append("--utc")
else:
args.append("--local")
util.execWithRedirect(cmd, args)
def get_preferred_timezone(territory):
"""
Get the preferred timezone for a given territory. Note that this function
simply returns the first timezone in the list of timezones for a given
territory.
:param territory: territory to get preferred timezone for
:type territory: str
:return: preferred timezone for the given territory or None if no found
:rtype: str or None
"""
timezones = langtable.list_timezones(territoryId=territory)
if not timezones:
return None
return timezones[0]
@cache
def all_timezones():
"""
Get all timezones, but with the Etc zones reduced. Cached.
:rtype: set
"""
etc_zones = {"Etc/" + zone for zone in ETC_ZONES}
return zoneinfo.available_timezones() | etc_zones
def get_all_regions_and_timezones():
"""
Get a dictionary mapping the regions to the list of their timezones.
:rtype: dict
"""
result = OrderedDict()
for tz in sorted(all_timezones()):
parts = tz.split("/", 1)
if len(parts) > 1:
if parts[0] not in result:
result[parts[0]] = set()
result[parts[0]].add(parts[1])
return result
def is_valid_timezone(timezone):
"""
Check if a given string is an existing timezone.
:type timezone: str
:rtype: bool
"""
return timezone in all_timezones()
def get_timezone(timezone):
"""
Return a tzinfo object for a given timezone name.
:param str timezone: the timezone name
:rtype: datetime.tzinfo
"""
return zoneinfo.ZoneInfo(timezone)