From 87931be6c5134c0dd661f77215da389e8d2b35c1 Mon Sep 17 00:00:00 2001 From: misiektoja Date: Fri, 7 Jun 2024 14:12:12 +0200 Subject: [PATCH] Support for float type of timestamps added in date/time related functions + get_short_date_from_ts() rewritten to display year if show_year == True and current year is different, also can omit displaying hour and minutes if show_hours == False --- spotify_monitor.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/spotify_monitor.py b/spotify_monitor.py index 1a493e0..b2e6c68 100644 --- a/spotify_monitor.py +++ b/spotify_monitor.py @@ -245,6 +245,9 @@ def calculate_timespan(timestamp1, timestamp2, show_weeks=True, show_hours=True, if type(timestamp1) is int: dt1 = datetime.fromtimestamp(int(ts1)) + elif type(timestamp1) is float: + ts1 = int(round(ts1)) + dt1 = datetime.fromtimestamp(ts1) elif type(timestamp1) is datetime: dt1 = timestamp1 ts1 = int(round(dt1.timestamp())) @@ -253,6 +256,9 @@ def calculate_timespan(timestamp1, timestamp2, show_weeks=True, show_hours=True, if type(timestamp2) is int: dt2 = datetime.fromtimestamp(int(ts2)) + elif type(timestamp2) is float: + ts2 = int(round(ts2)) + dt2 = datetime.fromtimestamp(ts2) elif type(timestamp2) is datetime: dt2 = timestamp2 ts2 = int(round(dt2.timestamp())) @@ -392,22 +398,41 @@ def get_date_from_ts(ts): ts_new = int(round(ts.timestamp())) elif type(ts) is int: ts_new = ts + elif type(ts) is float: + ts_new = int(round(ts)) else: return "" return (f"{calendar.day_abbr[(datetime.fromtimestamp(ts_new)).weekday()]} {datetime.fromtimestamp(ts_new).strftime("%d %b %Y, %H:%M:%S")}") -# Function to return the timestamp/datetime object in human readable format (short version); eg. Sun 21 Apr 15:08 -def get_short_date_from_ts(ts): +# Function to return the timestamp/datetime object in human readable format (short version); eg. +# Sun 21 Apr 15:08 +# Sun 21 Apr 24, 15:08 (if show_year == True and current year is different) +# Sun 21 Apr (if show_hour == False) +def get_short_date_from_ts(ts, show_year=False, show_hour=True): if type(ts) is datetime: ts_new = int(round(ts.timestamp())) elif type(ts) is int: ts_new = ts + elif type(ts) is float: + ts_new = int(round(ts)) else: return "" - return (f"{calendar.day_abbr[(datetime.fromtimestamp(ts_new)).weekday()]} {datetime.fromtimestamp(ts_new).strftime("%d %b %H:%M")}") + if show_hour: + hour_strftime = " %H:%M" + else: + hour_strftime = "" + + if show_year and int(datetime.fromtimestamp(ts_new).strftime("%Y")) != int(datetime.now().strftime("%Y")): + if show_hour: + hour_prefix = "," + else: + hour_prefix = "" + return (f"{calendar.day_abbr[(datetime.fromtimestamp(ts_new)).weekday()]} {datetime.fromtimestamp(ts_new).strftime(f"%d %b %y{hour_prefix}{hour_strftime}")}") + else: + return (f"{calendar.day_abbr[(datetime.fromtimestamp(ts_new)).weekday()]} {datetime.fromtimestamp(ts_new).strftime(f"%d %b{hour_strftime}")}") # Function to return the timestamp/datetime object in human readable format (only hour, minutes and optionally seconds): eg. 15:08:12 @@ -416,6 +441,8 @@ def get_hour_min_from_ts(ts, show_seconds=False): ts_new = int(round(ts.timestamp())) elif type(ts) is int: ts_new = ts + elif type(ts) is float: + ts_new = int(round(ts)) else: return "" @@ -432,6 +459,8 @@ def get_range_of_dates_from_tss(ts1, ts2, between_sep=" - ", short=False): ts1_new = int(round(ts1.timestamp())) elif type(ts1) is int: ts1_new = ts1 + elif type(ts1) is float: + ts1_new = int(round(ts1)) else: return "" @@ -439,6 +468,8 @@ def get_range_of_dates_from_tss(ts1, ts2, between_sep=" - ", short=False): ts2_new = int(round(ts2.timestamp())) elif type(ts2) is int: ts2_new = ts2 + elif type(ts2) is float: + ts2_new = int(round(ts2)) else: return ""