From 5241e23578a0f919d198fe0ad450ca80c28b7685 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Thu, 12 Dec 2019 17:19:38 +0100 Subject: [PATCH] emoji is time native now --- src/tww/time_lib.py | 58 ++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/tww/time_lib.py b/src/tww/time_lib.py index 20dcb82..89ba043 100644 --- a/src/tww/time_lib.py +++ b/src/tww/time_lib.py @@ -9,18 +9,6 @@ from dateutil.tz import tzlocal, gettz from tww import parse_query, solve_query, TZ_OFFSETS -def tzinfo_from_offset(offset): - if ':' in offset: - ''.join(offset.split(':')) - tznames = TZ_OFFSETS.get(offset) - if not tznames: - return pytz.timezone("utc") - for tzname in tznames: - if tzname.startswith('Etc/GMT'): - return pytz.timezone(tzname) - return pytz.timezone(tznames[0]) - - def time_ago(date=None, diff=None): """ Get a datetime object, timedelta object or a int() Epoch timestamp and return a @@ -89,6 +77,18 @@ def query_to_dt(query): # ================== +def tzinfo_from_offset(offset: str) -> pytz.timezone: + if ':' in offset: + offset = ''.join(offset.split(':')) + tznames = TZ_OFFSETS.get(offset) + if not tznames: + return pytz.timezone("utc") + for tzname in tznames: + if tzname.startswith('Etc/GMT'): + return pytz.timezone(tzname) + return pytz.timezone(tznames[0]) + + def dateparser_parse_dt(s: str): print("Dateparser query: {}".format(s)) parsed = parse_dt(s) @@ -96,26 +96,26 @@ def dateparser_parse_dt(s: str): return parsed -def get_utcnow(tzaware: bool = False): +def get_utcnow(tzaware: bool = True): if tzaware: return datetime.utcnow().replace(tzinfo=pytz.UTC) return datetime.utcnow() -def dt_tz_translation(dt: datetime, to_tz: str, from_tz: str = "+00:00"): - if ':' in to_tz: - to_shh, to_mm = to_tz.split(':') +def dt_tz_translation(dt: datetime, to_tz_offset: str, from_tz_offset: str = "+00:00") -> datetime: + if ':' in to_tz_offset: + to_shh, to_mm = to_tz_offset.split(':') else: - to_shh, to_mm = to_tz[:-2], to_tz[-2:] - if ':' in from_tz: - from_shh, from_mm = from_tz.split(':') + to_shh, to_mm = to_tz_offset[:-2], to_tz_offset[-2:] + if ':' in from_tz_offset: + from_shh, from_mm = from_tz_offset.split(':') else: - from_shh, from_mm = from_tz[:-2], to_tz[-2:] - tzinfo = tzinfo_from_offset(to_tz) + from_shh, from_mm = from_tz_offset[:-2], to_tz_offset[-2:] + tzinfo = tzinfo_from_offset(to_tz_offset) if dt.tzinfo: return dt.astimezone(tzinfo) r_dt = dt + timedelta(hours=int(to_shh), minutes=int(to_mm)) - timedelta(hours=int(from_shh), minutes=int(from_mm)) - r_dt.replace(tzinfo=tzinfo) + r_dt = tzinfo.localize(r_dt) return r_dt @@ -135,21 +135,25 @@ def get_local_tz_offset(): def tzname_to_tz_offset(tzname_iana: str): - return format_offset_from_timedelta(pytz.timezone(tzname_iana).utcoffset(get_utcnow())) + return format_offset_from_timedelta(pytz.timezone(tzname_iana).utcoffset(get_utcnow(False))) -def get_dt_tz_offset(dt: datetime): +def get_dt_tz_offset(dt: datetime) -> timedelta: if dt.tzinfo is not None: if type(dt.tzinfo) is StaticTzInfo: tzoffset = dt.tzinfo._StaticTzInfo__offset else: tzoffset = dt.tzinfo._utcoffset - return format_offset_from_timedelta(tzoffset) - return "+00:00" + return tzoffset + return timedelta(0) def get_seconds_since_epoch(dt): - return int(dt.timestamp()) + utc_seconds = int(dt.timestamp()) + if dt.tzinfo is None: + return utc_seconds + local_seconds = get_dt_tz_offset(dt).seconds + return utc_seconds + local_seconds def epoch_to_dt(seconds):