emoji is time native now
This commit is contained in:
parent
e4eafb5dd8
commit
5241e23578
@ -9,18 +9,6 @@ from dateutil.tz import tzlocal, gettz
|
|||||||
from tww import parse_query, solve_query, TZ_OFFSETS
|
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):
|
def time_ago(date=None, diff=None):
|
||||||
"""
|
"""
|
||||||
Get a datetime object, timedelta object or a int() Epoch timestamp and return a
|
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):
|
def dateparser_parse_dt(s: str):
|
||||||
print("Dateparser query: {}".format(s))
|
print("Dateparser query: {}".format(s))
|
||||||
parsed = parse_dt(s)
|
parsed = parse_dt(s)
|
||||||
@ -96,26 +96,26 @@ def dateparser_parse_dt(s: str):
|
|||||||
return parsed
|
return parsed
|
||||||
|
|
||||||
|
|
||||||
def get_utcnow(tzaware: bool = False):
|
def get_utcnow(tzaware: bool = True):
|
||||||
if tzaware:
|
if tzaware:
|
||||||
return datetime.utcnow().replace(tzinfo=pytz.UTC)
|
return datetime.utcnow().replace(tzinfo=pytz.UTC)
|
||||||
return datetime.utcnow()
|
return datetime.utcnow()
|
||||||
|
|
||||||
|
|
||||||
def dt_tz_translation(dt: datetime, to_tz: str, from_tz: str = "+00:00"):
|
def dt_tz_translation(dt: datetime, to_tz_offset: str, from_tz_offset: str = "+00:00") -> datetime:
|
||||||
if ':' in to_tz:
|
if ':' in to_tz_offset:
|
||||||
to_shh, to_mm = to_tz.split(':')
|
to_shh, to_mm = to_tz_offset.split(':')
|
||||||
else:
|
else:
|
||||||
to_shh, to_mm = to_tz[:-2], to_tz[-2:]
|
to_shh, to_mm = to_tz_offset[:-2], to_tz_offset[-2:]
|
||||||
if ':' in from_tz:
|
if ':' in from_tz_offset:
|
||||||
from_shh, from_mm = from_tz.split(':')
|
from_shh, from_mm = from_tz_offset.split(':')
|
||||||
else:
|
else:
|
||||||
from_shh, from_mm = from_tz[:-2], to_tz[-2:]
|
from_shh, from_mm = from_tz_offset[:-2], to_tz_offset[-2:]
|
||||||
tzinfo = tzinfo_from_offset(to_tz)
|
tzinfo = tzinfo_from_offset(to_tz_offset)
|
||||||
if dt.tzinfo:
|
if dt.tzinfo:
|
||||||
return dt.astimezone(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 = 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
|
return r_dt
|
||||||
|
|
||||||
|
|
||||||
@ -135,21 +135,25 @@ def get_local_tz_offset():
|
|||||||
|
|
||||||
|
|
||||||
def tzname_to_tz_offset(tzname_iana: str):
|
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 dt.tzinfo is not None:
|
||||||
if type(dt.tzinfo) is StaticTzInfo:
|
if type(dt.tzinfo) is StaticTzInfo:
|
||||||
tzoffset = dt.tzinfo._StaticTzInfo__offset
|
tzoffset = dt.tzinfo._StaticTzInfo__offset
|
||||||
else:
|
else:
|
||||||
tzoffset = dt.tzinfo._utcoffset
|
tzoffset = dt.tzinfo._utcoffset
|
||||||
return format_offset_from_timedelta(tzoffset)
|
return tzoffset
|
||||||
return "+00:00"
|
return timedelta(0)
|
||||||
|
|
||||||
|
|
||||||
def get_seconds_since_epoch(dt):
|
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):
|
def epoch_to_dt(seconds):
|
||||||
|
Loading…
Reference in New Issue
Block a user