@@ -176,7 +176,6 @@ def _get_unix_timezone(_root='/'): # type: (str) -> Timezone
176176 # OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and
177177 # Gentoo has a TIMEZONE setting in /etc/conf.d/clock
178178 # We look through these files for a timezone:
179-
180179 zone_re = re .compile ('\s*ZONE\s*=\s*\" ' )
181180 timezone_re = re .compile ('\s*TIMEZONE\s*=\s*\" ' )
182181 end_re = re .compile ('\" ' )
@@ -185,6 +184,7 @@ def _get_unix_timezone(_root='/'): # type: (str) -> Timezone
185184 tzpath = os .path .join (_root , filename )
186185 if not os .path .exists (tzpath ):
187186 continue
187+
188188 with open (tzpath , 'rt' ) as tzfile :
189189 data = tzfile .readlines ()
190190
@@ -194,13 +194,21 @@ def _get_unix_timezone(_root='/'): # type: (str) -> Timezone
194194 if match is None :
195195 # No ZONE= setting. Look for the TIMEZONE= setting.
196196 match = timezone_re .match (line )
197+
197198 if match is not None :
198199 # Some setting existed
199200 line = line [match .end ():]
200201 etctz = line [:end_re .search (line ).start ()]
201202
202- # We found a timezone
203- return Timezone (etctz .replace (' ' , '_' ))
203+ parts = etctz .replace (' ' , '_' ).split ('/' )[- 2 :]
204+ while parts :
205+ tzpath = '/' .join (parts )
206+ try :
207+ return Timezone (tzpath )
208+ except (ValueError , IOError , OSError ):
209+ pass
210+
211+ parts .pop (0 )
204212
205213 # systemd distributions use symlinks that include the zone name,
206214 # see manpage of localtime(5) and timedatectl(1)
0 commit comments