1import datetime
2import os
3import re
4
5from babel.localtime._helpers import (
6 _get_tzinfo,
7 _get_tzinfo_from_file,
8 _get_tzinfo_or_raise,
9)
10
11
12def _tz_from_env(tzenv: str) -> datetime.tzinfo:
13 if tzenv[0] == ':':
14 tzenv = tzenv[1:]
15
16 # TZ specifies a file
17 if os.path.exists(tzenv):
18 return _get_tzinfo_from_file(tzenv)
19
20 # TZ specifies a zoneinfo zone.
21 return _get_tzinfo_or_raise(tzenv)
22
23
24def _get_localzone(_root: str = '/') -> datetime.tzinfo:
25 """Tries to find the local timezone configuration.
26 This method prefers finding the timezone name and passing that to
27 zoneinfo or pytz, over passing in the localtime file, as in the later
28 case the zoneinfo name is unknown.
29 The parameter _root makes the function look for files like /etc/localtime
30 beneath the _root directory. This is primarily used by the tests.
31 In normal usage you call the function without parameters.
32 """
33
34 tzenv = os.environ.get('TZ')
35 if tzenv:
36 return _tz_from_env(tzenv)
37
38 # This is actually a pretty reliable way to test for the local time
39 # zone on operating systems like OS X. On OS X especially this is the
40 # only one that actually works.
41 try:
42 link_dst = os.readlink('/etc/localtime')
43 except OSError:
44 pass
45 else:
46 pos = link_dst.find('/zoneinfo/')
47 if pos >= 0:
48 zone_name = link_dst[pos + 10:]
49 tzinfo = _get_tzinfo(zone_name)
50 if tzinfo is not None:
51 return tzinfo
52
53 # Now look for distribution specific configuration files
54 # that contain the timezone name.
55 tzpath = os.path.join(_root, 'etc/timezone')
56 if os.path.exists(tzpath):
57 with open(tzpath, 'rb') as tzfile:
58 data = tzfile.read()
59
60 # Issue #3 in tzlocal was that /etc/timezone was a zoneinfo file.
61 # That's a misconfiguration, but we need to handle it gracefully:
62 if data[:5] != b'TZif2':
63 etctz = data.strip().decode()
64 # Get rid of host definitions and comments:
65 if ' ' in etctz:
66 etctz, dummy = etctz.split(' ', 1)
67 if '#' in etctz:
68 etctz, dummy = etctz.split('#', 1)
69
70 return _get_tzinfo_or_raise(etctz.replace(' ', '_'))
71
72 # CentOS has a ZONE setting in /etc/sysconfig/clock,
73 # OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and
74 # Gentoo has a TIMEZONE setting in /etc/conf.d/clock
75 # We look through these files for a timezone:
76 timezone_re = re.compile(r'\s*(TIME)?ZONE\s*=\s*"(?P<etctz>.+)"')
77
78 for filename in ('etc/sysconfig/clock', 'etc/conf.d/clock'):
79 tzpath = os.path.join(_root, filename)
80 if not os.path.exists(tzpath):
81 continue
82 with open(tzpath) as tzfile:
83 for line in tzfile:
84 match = timezone_re.match(line)
85 if match is not None:
86 # We found a timezone
87 etctz = match.group("etctz")
88 return _get_tzinfo_or_raise(etctz.replace(' ', '_'))
89
90 # No explicit setting existed. Use localtime
91 for filename in ('etc/localtime', 'usr/local/etc/localtime'):
92 tzpath = os.path.join(_root, filename)
93
94 if not os.path.exists(tzpath):
95 continue
96 return _get_tzinfo_from_file(tzpath)
97
98 raise LookupError('Can not find any timezone configuration')