From 2fea3d26dfb5adebf97fb230cdb19a6f0f192ad4 Mon Sep 17 00:00:00 2001 From: Shirshanka Das Date: Wed, 7 Sep 2022 12:09:08 -0700 Subject: [PATCH] fix(ingest): reporting should work with timestamps (#5860) --- .../src/datahub/ingestion/api/report.py | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/metadata-ingestion/src/datahub/ingestion/api/report.py b/metadata-ingestion/src/datahub/ingestion/api/report.py index e7df88e5bd..e7432d0d98 100644 --- a/metadata-ingestion/src/datahub/ingestion/api/report.py +++ b/metadata-ingestion/src/datahub/ingestion/api/report.py @@ -2,7 +2,7 @@ import json import pprint import sys from dataclasses import dataclass -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from enum import Enum from typing import Any, Dict @@ -24,19 +24,26 @@ class Report: elif isinstance(some_val, timedelta): return humanfriendly.format_timespan(some_val) elif isinstance(some_val, datetime): - now = 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)})." + try: + # 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) + tz_aware = ( + some_val.tzinfo is not None + and some_val.tzinfo.utcoffset(some_val) is not None ) + 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: return str(some_val)