mirror of
https://github.com/datahub-project/datahub.git
synced 2025-11-06 22:04:16 +00:00
fix(ingest): reporting should work with timestamps (#5860)
This commit is contained in:
parent
494f38ea60
commit
2fea3d26df
@ -2,7 +2,7 @@ import json
|
|||||||
import pprint
|
import pprint
|
||||||
import sys
|
import sys
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta, timezone
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
@ -24,19 +24,26 @@ class Report:
|
|||||||
elif isinstance(some_val, timedelta):
|
elif isinstance(some_val, timedelta):
|
||||||
return humanfriendly.format_timespan(some_val)
|
return humanfriendly.format_timespan(some_val)
|
||||||
elif isinstance(some_val, datetime):
|
elif isinstance(some_val, datetime):
|
||||||
now = datetime.now()
|
try:
|
||||||
diff = now - some_val
|
# check if we have a tz_aware object or not (https://stackoverflow.com/questions/5802108/how-to-check-if-a-datetime-object-is-localized-with-pytz)
|
||||||
if abs(diff) < timedelta(seconds=1):
|
tz_aware = (
|
||||||
# the timestamps are close enough that printing a duration isn't useful
|
some_val.tzinfo is not None
|
||||||
return f"{some_val} (now)."
|
and some_val.tzinfo.utcoffset(some_val) is not None
|
||||||
elif diff > timedelta(seconds=0):
|
|
||||||
# timestamp is in the past
|
|
||||||
return f"{some_val} ({humanfriendly.format_timespan(diff)} ago)."
|
|
||||||
else:
|
|
||||||
# timestamp is in the future
|
|
||||||
return (
|
|
||||||
f"{some_val} (in {humanfriendly.format_timespan(some_val - now)})."
|
|
||||||
)
|
)
|
||||||
|
now = datetime.now(timezone.utc) if tz_aware else datetime.now()
|
||||||
|
diff = now - some_val
|
||||||
|
if abs(diff) < timedelta(seconds=1):
|
||||||
|
# the timestamps are close enough that printing a duration isn't useful
|
||||||
|
return f"{some_val} (now)."
|
||||||
|
elif diff > timedelta(seconds=0):
|
||||||
|
# timestamp is in the past
|
||||||
|
return f"{some_val} ({humanfriendly.format_timespan(diff)} ago)."
|
||||||
|
else:
|
||||||
|
# timestamp is in the future
|
||||||
|
return f"{some_val} (in {humanfriendly.format_timespan(some_val - now)})."
|
||||||
|
except Exception:
|
||||||
|
# we don't want to fail reporting because we were unable to pretty print a timestamp
|
||||||
|
return str(datetime)
|
||||||
else:
|
else:
|
||||||
return str(some_val)
|
return str(some_val)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user