mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-07 13:41:15 -07:00
parent
045bfb9502
commit
c0fb1dbbdc
20 changed files with 333 additions and 1122 deletions
|
@ -1,24 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import time
|
||||
import datetime
|
||||
import calendar
|
||||
import pytz_deprecation_shim as pds
|
||||
|
||||
try:
|
||||
import zoneinfo # pragma: no cover
|
||||
except ImportError:
|
||||
from backports import zoneinfo # pragma: no cover
|
||||
|
||||
from tzlocal import windows_tz
|
||||
|
||||
|
||||
class ZoneInfoNotFoundError(pds.UnknownTimeZoneError, zoneinfo.ZoneInfoNotFoundError):
|
||||
"""An exception derived from both pytz and zoneinfo
|
||||
|
||||
This exception will be trappable both by pytz expecting clients and
|
||||
zoneinfo expecting clients.
|
||||
"""
|
||||
|
||||
|
||||
def get_system_offset():
|
||||
|
@ -38,9 +21,9 @@ def get_system_offset():
|
|||
# so we check that the difference is less than one minute, because nobody
|
||||
# has that small DST differences.
|
||||
if abs(offset - time.altzone) < 60:
|
||||
return -time.altzone # pragma: no cover
|
||||
return -time.altzone
|
||||
else:
|
||||
return -time.timezone # pragma: no cover
|
||||
return -time.timezone
|
||||
|
||||
|
||||
def get_tz_offset(tz):
|
||||
|
@ -56,70 +39,8 @@ def assert_tz_offset(tz):
|
|||
tz_offset = get_tz_offset(tz)
|
||||
system_offset = get_system_offset()
|
||||
if tz_offset != system_offset:
|
||||
msg = (
|
||||
"Timezone offset does not match system offset: {} != {}. "
|
||||
"Please, check your config files."
|
||||
).format(tz_offset, system_offset)
|
||||
msg = ('Timezone offset does not match system offset: {0} != {1}. '
|
||||
'Please, check your config files.').format(
|
||||
tz_offset, system_offset
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
|
||||
def _tz_name_from_env(tzenv=None):
|
||||
if tzenv is None:
|
||||
tzenv = os.environ.get("TZ")
|
||||
|
||||
if not tzenv:
|
||||
return None
|
||||
|
||||
if tzenv in windows_tz.tz_win:
|
||||
# Yup, it's a timezone
|
||||
return tzenv
|
||||
|
||||
if os.path.isabs(tzenv) and os.path.exists(tzenv):
|
||||
# It's a file specification
|
||||
parts = tzenv.split(os.sep)
|
||||
|
||||
# Is it a zone info zone?
|
||||
possible_tz = "/".join(parts[-2:])
|
||||
if possible_tz in windows_tz.tz_win:
|
||||
# Yup, it is
|
||||
return possible_tz
|
||||
|
||||
# Maybe it's a short one, like UTC?
|
||||
if parts[-1] in windows_tz.tz_win:
|
||||
# Indeed
|
||||
return parts[-1]
|
||||
|
||||
|
||||
def _tz_from_env(tzenv=None):
|
||||
if tzenv is None:
|
||||
tzenv = os.environ.get("TZ")
|
||||
|
||||
if not tzenv:
|
||||
return None
|
||||
|
||||
# Some weird format that exists:
|
||||
if tzenv[0] == ":":
|
||||
tzenv = tzenv[1:]
|
||||
|
||||
# TZ specifies a file
|
||||
if os.path.isabs(tzenv) and os.path.exists(tzenv):
|
||||
# Try to see if we can figure out the name
|
||||
tzname = _tz_name_from_env(tzenv)
|
||||
if not tzname:
|
||||
# Nope, not a standard timezone name, just take the filename
|
||||
tzname = tzenv.split(os.sep)[-1]
|
||||
with open(tzenv, "rb") as tzfile:
|
||||
zone = zoneinfo.ZoneInfo.from_file(tzfile, key=tzname)
|
||||
return pds.wrap_zone(zone)
|
||||
|
||||
# TZ must specify a zoneinfo zone.
|
||||
try:
|
||||
tz = pds.timezone(tzenv)
|
||||
# That worked, so we return this:
|
||||
return tz
|
||||
except pds.UnknownTimeZoneError:
|
||||
# Nope, it's something like "PST4DST" etc, we can't handle that.
|
||||
raise ZoneInfoNotFoundError(
|
||||
"tzlocal() does not support non-zoneinfo timezones like %s. \n"
|
||||
"Please use a timezone in the form of Continent/City"
|
||||
) from None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue