From f3ebfff8a0778f6ccce285ef11b8e73d15282b7e Mon Sep 17 00:00:00 2001 From: Deimos Date: Wed, 3 Oct 2018 18:38:36 -0600 Subject: [PATCH] Work around bad timezone handling in ago library The ago library doesn't handle datetimes correctly if they have any timezone except the system's localtime. This can be worked around by calculating a timedelta and passing that instead of a datetime. This commit updates the descriptive_timedelta() function to do that. I've also registered an issue on the library, but I don't know if it's maintained any more: https://bitbucket.org/russellballestrini/ago/issues/3 --- tildes/tildes/lib/datetime.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tildes/tildes/lib/datetime.py b/tildes/tildes/lib/datetime.py index 2a66375..efd54d0 100644 --- a/tildes/tildes/lib/datetime.py +++ b/tildes/tildes/lib/datetime.py @@ -97,7 +97,11 @@ def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str: A time of less than a second returns "a moment ago". """ - seconds_ago = (utc_now() - target).total_seconds() + # the ago library doesn't deal with timezones properly, so we need to calculate the + # timedelta ourselves and only ever call human() using a timedelta + delta = utc_now() - target + + seconds_ago = delta.total_seconds() if seconds_ago < 1: return "a moment ago" @@ -107,7 +111,7 @@ def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str: precision = 1 else: # try a precision=2 version, and check the units it ends up with - result = human(target, precision=2) + result = human(delta, precision=2) units = ("year", "day", "hour", "minute", "second") unit_indices = [i for (i, unit) in enumerate(units) if unit in result] @@ -119,7 +123,7 @@ def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str: # otherwise, drop back down to precision=1 precision = 1 - result = human(target, precision, abbreviate=abbreviate) + result = human(delta, precision, abbreviate=abbreviate) # remove commas if abbreviating ("3d 2h ago", not "3d, 2h ago") if abbreviate: