diff --git a/src/tww/time_lib.py b/src/tww/time_lib.py index 7638e90..96f10fa 100644 --- a/src/tww/time_lib.py +++ b/src/tww/time_lib.py @@ -11,7 +11,8 @@ from tww import parse_query, solve_query, TZ_OFFSETS 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 pretty string like 'an hour ago', 'Yesterday', '3 months ago', 'just now', etc Modified from: http://stackoverflow.com/a/1551394/141084 @@ -56,19 +57,25 @@ def time_ago(date=None, diff=None): minutes = second_diff % 3600 // 60 if second_diff < 86400: hrs_diff = second_diff // 3600 - return str("{}{:02d}:{:02d}:{:02d}".format(sign, hrs_diff, minutes, seconds)) + return str("{}{:02d}:{:02d}:{:02d}".format(sign, hrs_diff, minutes, + seconds)) seconds = second_diff % 60 minutes = second_diff % 3600 // 60 hours = second_diff // 3600 if day_diff < 365: if day_diff < 30: - return str("{}{}d{:02d}:{:02d}:{:02d}".format(sign, day_diff, hours, minutes, seconds)) + return str("{}{}d{:02d}:{:02d}:{:02d}".format(sign, day_diff, hours, + minutes, seconds)) months, days = day_diff // 30, day_diff % 30 - return str("{}{}m{}d{:02d}:{:02d}:{:02d}".format(sign, months, days, hours, minutes, seconds)) + return str( + "{}{}m{}d{:02d}:{:02d}:{:02d}".format(sign, months, days, hours, + minutes, seconds)) years = day_diff // 365 days = day_diff % 365 months, days = days // 30, days % 30 - return str("{}{}y{}m{}d{:02d}:{:02d}:{:02d}".format(sign, years, months, days, hours, minutes, seconds)) + return str( + "{}{}y{}m{}d{:02d}:{:02d}:{:02d}".format(sign, years, months, days, + hours, minutes, seconds)) def query_to_dt(query): @@ -90,9 +97,13 @@ def tzinfo_from_offset(offset: str) -> pytz.timezone: def dateparser_parse_dt(s: str): - print("Dateparser query: {}".format(s)) + # print("Dateparser query: {}".format(s)) parsed = parse_dt(s) - print("Dateparser parsed query: {}".format(parsed)) + # print("Dateparser parsed query: {}".format(parsed)) + if not parsed: + return None + if parsed.tzinfo is None: + parsed = parsed.replace(tzinfo=pytz.timezone("utc")) return parsed @@ -102,7 +113,8 @@ def get_utcnow(tzaware: bool = True): return datetime.utcnow() -def dt_tz_translation(dt: datetime, to_tz_offset: str, from_tz_offset: str = "+00:00") -> datetime: +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: @@ -114,7 +126,8 @@ def dt_tz_translation(dt: datetime, to_tz_offset: str, from_tz_offset: str = "+0 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 = dt + timedelta(hours=int(to_shh), minutes=int(to_mm)) - timedelta( + hours=int(from_shh), minutes=int(from_mm)) r_dt = tzinfo.localize(r_dt) return r_dt @@ -131,11 +144,13 @@ def get_local_tzname_iana(): def get_local_tz_offset(): - return format_offset_from_timedelta(datetime.now(tzlocal()).tzinfo._std_offset) + return format_offset_from_timedelta( + datetime.now(tzlocal()).tzinfo._std_offset) def tzname_to_tz_offset(tzname_iana: str): - return format_offset_from_timedelta(pytz.timezone(tzname_iana).utcoffset(get_utcnow(False))) + return format_offset_from_timedelta( + pytz.timezone(tzname_iana).utcoffset(get_utcnow(False))) def get_dt_tz_offset(dt: datetime) -> timedelta: @@ -148,20 +163,28 @@ def get_dt_tz_offset(dt: datetime) -> timedelta: return timedelta(0) -def get_seconds_since_epoch(dt): - utc_seconds = int(dt.timestamp()) +def get_us_since_epoch(dt: datetime): + utc_seconds = int(dt.timestamp() * 1e6) if dt.tzinfo is None: return utc_seconds local_seconds = get_dt_tz_offset(dt).seconds return utc_seconds + local_seconds +def get_ms_since_epoch(dt): + return int(get_us_since_epoch(dt) / 1e3) + + +def get_s_since_epoch(dt): + return int(get_us_since_epoch(dt) / 1e6) + + def epoch_to_dt(seconds): return datetime.fromtimestamp(seconds) def time_to_emoji(dt): - seconds = get_seconds_since_epoch(dt) + seconds = get_s_since_epoch(dt) a = int((seconds / 900 - 3) / 2 % 24) return chr(128336 + a // 2 + a % 2 * 12) @@ -187,7 +210,7 @@ if __name__ == "__main__": ) )) - print(get_seconds_since_epoch( + print(get_s_since_epoch( dateparser_parse_dt("2019-12-11 15:53:40+0000") )) @@ -204,21 +227,30 @@ if __name__ == "__main__": "Now in New York" # Location:City "Now in Bulgaria" # Location:Country "Now in USA" # Location:Country - multiple timezones - "Now in CET" # Timezone:Abbreviation - https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations - "Now in Europe/Zurich" # Timezone:IANA - tz db, https://en.wikipedia.org/wiki/List_of_tz_database_time_zones, timezone database - "Now in Alfa" # Timezone:Military - https://en.wikipedia.org/wiki/List_of_military_time_zones - "Now in +02:00" # Timezone:Offset - https://en.wikipedia.org/wiki/List_of_UTC_time_offsets + "Now in CET" # Timezone:Abbreviation - + # https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations + "Now in Europe/Zurich" # Timezone:IANA - tz db, + # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones, timezone + # database + "Now in Alfa" # Timezone:Military - + # https://en.wikipedia.org/wiki/List_of_military_time_zones + "Now in +02:00" # Timezone:Offset - + # https://en.wikipedia.org/wiki/List_of_UTC_time_offsets "