Skip to content

Commit

Permalink
Support for float type of timestamps added in date/time related funct…
Browse files Browse the repository at this point in the history
…ions + 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
  • Loading branch information
misiektoja committed Jun 7, 2024
1 parent 99743fa commit 87931be
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions spotify_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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()))
Expand Down Expand Up @@ -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
Expand All @@ -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 ""

Expand All @@ -432,13 +459,17 @@ 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 ""

if type(ts2) is datetime:
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 ""

Expand Down

0 comments on commit 87931be

Please sign in to comment.