| 1 |
#!/usr/bin/env python3 |
| 2 |
# encoding=UTF-8 |
| 3 |
|
| 4 |
# Copyright © 2013-2025 Jakub Wilk <[email protected]> |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the “Software”), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
# SOFTWARE. |
| 23 |
|
| 24 |
import collections |
| 25 |
import datetime |
| 26 |
import os |
| 27 |
import subprocess as ipc |
| 28 |
import sys |
| 29 |
|
| 30 |
import pytz |
| 31 |
|
| 32 |
# pylint: disable=wrong-import-position |
| 33 |
|
| 34 |
sys.path[0] += '/..' |
| 35 |
basedir = sys.path[0] |
| 36 |
|
| 37 |
from lib import gettext |
| 38 |
|
| 39 |
int(0_0) # Python >= 3.6 is required |
| 40 |
|
| 41 |
def get_tzdata_version(): |
| 42 |
version = pytz.OLSON_VERSION |
| 43 |
if os.path.exists('/etc/debian_version'): |
| 44 |
# On Debian systems, pytz uses the system timezone database, |
| 45 |
# so pytz.OLSON_VERSION is not reliable. |
| 46 |
version = ipc.check_output(['dpkg-query', '-Wf', '${Version}', 'tzdata']) |
| 47 |
version = version.decode('ASCII') |
| 48 |
version, revision = version.rsplit('-', 1) |
| 49 |
del revision |
| 50 |
return version |
| 51 |
|
| 52 |
def main(): |
| 53 |
minyear = gettext.epoch.year |
| 54 |
maxyear = datetime.date.today().year + 1 |
| 55 |
tzdata = collections.defaultdict(set) |
| 56 |
for tzname in pytz.all_timezones: |
| 57 |
if tzname.startswith('Etc/'): |
| 58 |
continue |
| 59 |
tz = pytz.timezone(tzname) |
| 60 |
try: |
| 61 |
timestamps = list(tz._utc_transition_times) # pylint: disable=protected-access |
| 62 |
except AttributeError: |
| 63 |
timestamps = [] |
| 64 |
timestamps += [datetime.datetime(minyear, 1, 1)] |
| 65 |
for timestamp in timestamps: |
| 66 |
if not (minyear <= timestamp.year <= maxyear): |
| 67 |
continue |
| 68 |
timestamp = tz.fromutc(timestamp) |
| 69 |
code, offset = timestamp.strftime('%Z %z').split() |
| 70 |
assert 3 <= len(code) <= 6 |
| 71 |
tzdata[code].add(offset) |
| 72 |
path = os.path.join(basedir, 'data', 'timezones') |
| 73 |
sys.stdout = open(path + '.tmp', 'wt', encoding='ASCII') # pylint: disable=consider-using-with |
| 74 |
tzdata_version = get_tzdata_version() |
| 75 |
today = datetime.date.today() |
| 76 |
print(f'''\ |
| 77 |
# This file has been generated automatically by private/update-timezones. |
| 78 |
# Do not edit. |
| 79 |
# Timezone database version: {tzdata_version} |
| 80 |
# Last update: {today} |
| 81 |
''') |
| 82 |
print('[timezones]') |
| 83 |
for code, offsets in sorted(tzdata.items()): |
| 84 |
offsets = str.join(' ', sorted(offsets)) |
| 85 |
print(f'{code} = {offsets}') |
| 86 |
print() |
| 87 |
print('# vim\72ft=dosini') |
| 88 |
sys.stdout.close() |
| 89 |
os.rename(path + '.tmp', path) |
| 90 |
|
| 91 |
if __name__ == '__main__': |
| 92 |
main() |
| 93 |
|
| 94 |
# vim:ts=4 sts=4 sw=4 et |