@@ -86,16 +86,14 @@ def get_tz_name_for_windows(cls):
8686 return get_localzone_name ()
8787
8888 @classmethod
89- def get_tz_name_for_unix (cls ):
89+ def get_tz_name_for_unix (cls , _root = '/' ):
9090 tzenv = os .environ .get ('TZ' )
9191 if tzenv :
9292 try :
9393 return _tz_from_env (tzenv )
9494 except ValueError :
9595 pass
9696
97- _root = '/'
98-
9997 # Now look for distribution specific configuration files
10098 # that contain the timezone name.
10199 tzpath = os .path .join (_root , 'etc/timezone' )
@@ -150,22 +148,23 @@ def get_tz_name_for_unix(cls):
150148 tzpath = os .path .join (_root , 'etc/localtime' )
151149 if os .path .exists (tzpath ) and os .path .islink (tzpath ):
152150 tzpath = os .path .realpath (tzpath )
153- start = tzpath .find ( "/" ) + 1
154- while start is not 0 :
155- tzpath = tzpath [ start :]
151+ parts = tzpath .split ( '/' )[ - 2 :]
152+ while parts :
153+ tzpath = '/' . join ( parts )
156154 try :
157155 return Timezone .load (tzpath )
158- except ValueError :
156+ except ( ValueError , IOError , OSError ) :
159157 pass
160- start = tzpath .find ("/" ) + 1
158+
159+ parts .pop (0 )
161160
162161 # No explicit setting existed. Use localtime
163162 for filename in ('etc/localtime' , 'usr/local/etc/localtime' ):
164163 tzpath = os .path .join (_root , filename )
165164
166165 if not os .path .exists (tzpath ):
167166 continue
168- return Timezone ('' , * Loader .load (tzpath ))
167+ return Timezone ('' , * Loader .load_from_file (tzpath ))
169168
170169 raise RuntimeError ('Can not find any timezone configuration' )
171170
0 commit comments