# TZ specifies a file
if os.path.exists(tzenv):
with open(tzenv, 'rb') as tzfile:
return pytz.tzfile.build_tzinfo('local', tzfile)
# TZ specifies a zoneinfo zone.
try:
tz = pytz.timezone(tzenv)
# That worked, so we return this:
return tz
except pytz.UnknownTimeZoneError:
raise pytz.UnknownTimeZoneError(
"tzlocal() does not support non-zoneinfo timezones like %s. \n"
"Please use a timezone in the form of Continent/City")
The pytz.tzfile.build_tzinfo('local', tzfile) should probably be limited to absolute paths (if os.path.isabs(tzenv)).
This would mimic the behaviour of GNU libc:
if (__libc_enable_secure
&& ((*file == '/'
&& memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
&& memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
|| strstr (file, "../") != NULL))
/* This test is certainly a bit too restrictive but it should
catch all critical cases. */
goto ret_free_transitions;
If for some reason a file exists in the current directory with the same name as the time zone, it will be used instead of the pytz one:
The reason is this code:
The
pytz.tzfile.build_tzinfo('local', tzfile)
should probably be limited to absolute paths (if os.path.isabs(tzenv)
).This would mimic the behaviour of GNU libc: