@ -5,7 +5,7 @@
from datetime import datetime , timedelta , timezone
import re
from typing import Any
from typing import Any , Optional
from ago import human
@ -84,7 +84,9 @@ def utc_from_timestamp(timestamp: int) -> datetime:
return datetime . fromtimestamp ( timestamp , timezone . utc )
def descriptive_timedelta ( target : datetime , abbreviate : bool = False ) - > str :
def descriptive_timedelta (
target : datetime , abbreviate : bool = False , precision : Optional [ int ] = None
) - > str :
""" Return a descriptive string for how long ago a datetime was.
The returned string will be of a format like " 4 hours ago " or " 3 hours, 21 minutes
@ -105,6 +107,7 @@ def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str:
if seconds_ago < 1 :
return " a moment ago "
if not precision :
# determine whether one or two precision levels is appropriate
if seconds_ago < 3600 :
# if it's less than an hour, we always want only one precision level
@ -132,13 +135,15 @@ def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str:
return result
def adaptive_date ( target : datetime , abbreviate : bool = False ) - > str :
def adaptive_date (
target : datetime , abbreviate : bool = False , precision : Optional [ int ] = None
) - > str :
""" Return a date string that switches from relative to absolute past a threshold. """
threshold = timedelta ( days = 7 )
# if the date is more recent than threshold, return the relative "ago"-style string
if utc_now ( ) - target < threshold :
return descriptive_timedelta ( target , abbreviate )
return descriptive_timedelta ( target , abbreviate , precision )
# if abbreviating, use the short version of month name ("Dec" instead of "December")
if abbreviate :