diff --git a/pygal/_compat.py b/pygal/_compat.py index 56fbeb1..d83ec73 100644 --- a/pygal/_compat.py +++ b/pygal/_compat.py @@ -65,7 +65,8 @@ def total_seconds(td): def timestamp(x): if hasattr(x, 'timestamp'): - return x.timestamp() + from datetime import timezone + return x.replace(tzinfo=timezone.utc).timestamp() else: if hasattr(x, 'utctimetuple'): t = x.utctimetuple() diff --git a/pygal/graph/time.py b/pygal/graph/time.py index 9c1ec7c..3f25112 100644 --- a/pygal/graph/time.py +++ b/pygal/graph/time.py @@ -21,14 +21,12 @@ Various datetime line plot """ from pygal.adapters import positive from pygal.graph.xy import XY -from datetime import datetime, date, time, timedelta, timezone +from datetime import datetime, date, time, timedelta from pygal._compat import timestamp, total_seconds def datetime_to_timestamp(x): if isinstance(x, datetime): - if x.tzinfo is None: - x = x.replace(tzinfo=timezone.utc) return timestamp(x) return x @@ -84,7 +82,7 @@ class DateTimeLine(XY): def _x_format(self): """Return the value formatter for this graph""" def datetime_to_str(x): - dt = datetime.fromtimestamp(x, timezone.utc) + dt = datetime.utcfromtimestamp(x) if self.x_value_formatter: return self.x_value_formatter(dt) return dt.isoformat()