Skip to content

Commit 9d55d8b

Browse files
lint-aisdispater
authored andcommitted
Better path handling in symlink case, use os.path methods/constants (#242)
Fixes #202
1 parent 28572a5 commit 9d55d8b

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

pendulum/tz/local_timezone.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,31 +201,29 @@ def _get_unix_timezone(_root='/'): # type: (str) -> Timezone
201201
line = line[match.end():]
202202
etctz = line[:end_re.search(line).start()]
203203

204-
parts = list(reversed(etctz.replace(' ', '_').split('/')))
204+
parts = list(reversed(etctz.replace(' ', '_').split(os.path.sep)))
205205
tzpath = []
206206
while parts:
207207
tzpath.insert(0, parts.pop(0))
208208

209209
try:
210-
return Timezone('/'.join(tzpath))
210+
return Timezone(os.path.join(*tzpath))
211211
except InvalidTimezone:
212212
pass
213213

214214
# systemd distributions use symlinks that include the zone name,
215215
# see manpage of localtime(5) and timedatectl(1)
216-
tzpath = os.path.join(_root, 'etc/localtime')
216+
tzpath = os.path.join(_root, 'etc', 'localtime')
217217
if os.path.exists(tzpath) and os.path.islink(tzpath):
218-
tzpath = os.path.realpath(tzpath)
219-
parts = tzpath.split('/')[-2:]
218+
parts = list(reversed(os.path.realpath(tzpath).replace(' ', '_').split(os.path.sep)))
219+
tzpath = []
220220
while parts:
221-
tzpath = '/'.join(parts)
221+
tzpath.insert(0, parts.pop(0))
222222
try:
223-
return Timezone(tzpath)
224-
except (ValueError, IOError, OSError):
223+
return Timezone(os.path.join(*tzpath))
224+
except InvalidTimezone:
225225
pass
226226

227-
parts.pop(0)
228-
229227
# No explicit setting existed. Use localtime
230228
for filename in ('etc/localtime', 'usr/local/etc/localtime'):
231229
tzpath = os.path.join(_root, filename)

0 commit comments

Comments
 (0)