Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_localzone(_root='/'):
tzenv = tzlocal.unix._try_tz_from_env()
if tzenv:
return tzenv
with Popen(
"systemsetup -gettimezone",
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
) as pipe:
tzname = pipe.stdout.read().replace(b'Time Zone: ', b'').strip()
if not tzname or tzname not in pytz.all_timezones_set:
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink(os.path.join(_root, "etc/localtime"))
tzname = link[link.rfind("zoneinfo/") + 9:]
return pytz.timezone(tzname)