|
| 1 | +import os |
| 2 | +import pytz |
| 3 | +import re |
| 4 | + |
| 5 | +from pandas._libs.src.tzlocal import utils |
| 6 | + |
| 7 | +_cache_tz = None |
| 8 | + |
| 9 | + |
| 10 | +def _tz_from_env(tzenv): |
| 11 | + if tzenv[0] == ':': |
| 12 | + tzenv = tzenv[1:] |
| 13 | + |
| 14 | + # TZ specifies a file |
| 15 | + if os.path.exists(tzenv): |
| 16 | + with open(tzenv, 'rb') as tzfile: |
| 17 | + return pytz.tzfile.build_tzinfo('local', tzfile) |
| 18 | + |
| 19 | + # TZ specifies a zoneinfo zone. |
| 20 | + try: |
| 21 | + tz = pytz.timezone(tzenv) |
| 22 | + # That worked, so we return this: |
| 23 | + return tz |
| 24 | + except pytz.UnknownTimeZoneError: |
| 25 | + raise pytz.UnknownTimeZoneError( |
| 26 | + "tzlocal() does not support non-zoneinfo timezones like %s. \n" |
| 27 | + "Please use a timezone in the form of Continent/City") |
| 28 | + |
| 29 | + |
| 30 | +def _try_tz_from_env(): |
| 31 | + tzenv = os.environ.get('TZ') |
| 32 | + if tzenv: |
| 33 | + try: |
| 34 | + return _tz_from_env(tzenv) |
| 35 | + except pytz.UnknownTimeZoneError: |
| 36 | + pass |
| 37 | + |
| 38 | + |
| 39 | +def _get_localzone(_root='/'): |
| 40 | + """Tries to find the local timezone configuration. |
| 41 | +
|
| 42 | + This method prefers finding the timezone name and passing that to pytz, |
| 43 | + over passing in the localtime file, as in the later case the zoneinfo |
| 44 | + name is unknown. |
| 45 | +
|
| 46 | + The parameter _root makes the function look for files like /etc/localtime |
| 47 | + beneath the _root directory. This is primarily used by the tests. |
| 48 | + In normal usage you call the function without parameters.""" |
| 49 | + |
| 50 | + tzenv = _try_tz_from_env() |
| 51 | + if tzenv: |
| 52 | + return tzenv |
| 53 | + |
| 54 | + # Now look for distribution specific configuration files |
| 55 | + # that contain the timezone name. |
| 56 | + for configfile in ('etc/timezone', 'var/db/zoneinfo'): |
| 57 | + tzpath = os.path.join(_root, configfile) |
| 58 | + try: |
| 59 | + with open(tzpath, 'rb') as tzfile: |
| 60 | + data = tzfile.read() |
| 61 | + |
| 62 | + # Issue #3 was that /etc/timezone was a zoneinfo file. |
| 63 | + # That's a misconfiguration, but we need to handle it gracefully: |
| 64 | + if data[:5] == b'TZif2': |
| 65 | + continue |
| 66 | + |
| 67 | + etctz = data.strip().decode() |
| 68 | + if not etctz: |
| 69 | + # Empty file, skip |
| 70 | + continue |
| 71 | + for etctz in data.decode().splitlines(): |
| 72 | + # Get rid of host definitions and comments: |
| 73 | + if ' ' in etctz: |
| 74 | + etctz, dummy = etctz.split(' ', 1) |
| 75 | + if '#' in etctz: |
| 76 | + etctz, dummy = etctz.split('#', 1) |
| 77 | + if not etctz: |
| 78 | + continue |
| 79 | + return pytz.timezone(etctz.replace(' ', '_')) |
| 80 | + except IOError: |
| 81 | + # File doesn't exist or is a directory |
| 82 | + continue |
| 83 | + |
| 84 | + # CentOS has a ZONE setting in /etc/sysconfig/clock, |
| 85 | + # OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and |
| 86 | + # Gentoo has a TIMEZONE setting in /etc/conf.d/clock |
| 87 | + # We look through these files for a timezone: |
| 88 | + |
| 89 | + zone_re = re.compile(r'\s*ZONE\s*=\s*\"') |
| 90 | + timezone_re = re.compile(r'\s*TIMEZONE\s*=\s*\"') |
| 91 | + end_re = re.compile('\"') |
| 92 | + |
| 93 | + for filename in ('etc/sysconfig/clock', 'etc/conf.d/clock'): |
| 94 | + tzpath = os.path.join(_root, filename) |
| 95 | + try: |
| 96 | + with open(tzpath, 'rt') as tzfile: |
| 97 | + data = tzfile.readlines() |
| 98 | + |
| 99 | + for line in data: |
| 100 | + # Look for the ZONE= setting. |
| 101 | + match = zone_re.match(line) |
| 102 | + if match is None: |
| 103 | + # No ZONE= setting. Look for the TIMEZONE= setting. |
| 104 | + match = timezone_re.match(line) |
| 105 | + if match is not None: |
| 106 | + # Some setting existed |
| 107 | + line = line[match.end():] |
| 108 | + etctz = line[:end_re.search(line).start()] |
| 109 | + |
| 110 | + # We found a timezone |
| 111 | + return pytz.timezone(etctz.replace(' ', '_')) |
| 112 | + except IOError: |
| 113 | + # File doesn't exist or is a directory |
| 114 | + continue |
| 115 | + |
| 116 | + # systemd distributions use symlinks that include the zone name, |
| 117 | + # see manpage of localtime(5) and timedatectl(1) |
| 118 | + tzpath = os.path.join(_root, 'etc/localtime') |
| 119 | + if os.path.exists(tzpath) and os.path.islink(tzpath): |
| 120 | + tzpath = os.path.realpath(tzpath) |
| 121 | + start = tzpath.find("/")+1 |
| 122 | + while start is not 0: |
| 123 | + tzpath = tzpath[start:] |
| 124 | + try: |
| 125 | + return pytz.timezone(tzpath) |
| 126 | + except pytz.UnknownTimeZoneError: |
| 127 | + pass |
| 128 | + start = tzpath.find("/")+1 |
| 129 | + |
| 130 | + # Are we under Termux on Android? It's not officially supported, because |
| 131 | + # there is no reasonable way to run tests for this, but let's make an effort. |
| 132 | + if os.path.exists('/system/bin/getprop'): |
| 133 | + import subprocess |
| 134 | + androidtz = subprocess.check_output(['getprop', 'persist.sys.timezone']) |
| 135 | + return pytz.timezone(androidtz.strip().decode()) |
| 136 | + |
| 137 | + # No explicit setting existed. Use localtime |
| 138 | + for filename in ('etc/localtime', 'usr/local/etc/localtime'): |
| 139 | + tzpath = os.path.join(_root, filename) |
| 140 | + |
| 141 | + if not os.path.exists(tzpath): |
| 142 | + continue |
| 143 | + with open(tzpath, 'rb') as tzfile: |
| 144 | + return pytz.tzfile.build_tzinfo('local', tzfile) |
| 145 | + |
| 146 | + raise pytz.UnknownTimeZoneError('Can not find any timezone configuration') |
| 147 | + |
| 148 | + |
| 149 | +def get_localzone(): |
| 150 | + """Get the computers configured local timezone, if any.""" |
| 151 | + global _cache_tz |
| 152 | + if _cache_tz is None: |
| 153 | + _cache_tz = _get_localzone() |
| 154 | + |
| 155 | + utils.assert_tz_offset(_cache_tz) |
| 156 | + return _cache_tz |
| 157 | + |
| 158 | + |
| 159 | +def reload_localzone(): |
| 160 | + """Reload the cached localzone. You need to call this if the timezone has changed.""" |
| 161 | + global _cache_tz |
| 162 | + _cache_tz = _get_localzone() |
| 163 | + utils.assert_tz_offset(_cache_tz) |
| 164 | + return _cache_tz |
0 commit comments