Browse Source

For python 2

pull/242/head
Florian Mounier 10 years ago
parent
commit
1d9d4f774e
  1. 3
      pygal/_compat.py
  2. 6
      pygal/graph/time.py

3
pygal/_compat.py

@ -65,7 +65,8 @@ def total_seconds(td):
def timestamp(x): def timestamp(x):
if hasattr(x, 'timestamp'): if hasattr(x, 'timestamp'):
return x.timestamp() from datetime import timezone
return x.replace(tzinfo=timezone.utc).timestamp()
else: else:
if hasattr(x, 'utctimetuple'): if hasattr(x, 'utctimetuple'):
t = x.utctimetuple() t = x.utctimetuple()

6
pygal/graph/time.py

@ -21,14 +21,12 @@ Various datetime line plot
""" """
from pygal.adapters import positive from pygal.adapters import positive
from pygal.graph.xy import XY 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 from pygal._compat import timestamp, total_seconds
def datetime_to_timestamp(x): def datetime_to_timestamp(x):
if isinstance(x, datetime): if isinstance(x, datetime):
if x.tzinfo is None:
x = x.replace(tzinfo=timezone.utc)
return timestamp(x) return timestamp(x)
return x return x
@ -84,7 +82,7 @@ class DateTimeLine(XY):
def _x_format(self): def _x_format(self):
"""Return the value formatter for this graph""" """Return the value formatter for this graph"""
def datetime_to_str(x): def datetime_to_str(x):
dt = datetime.fromtimestamp(x, timezone.utc) dt = datetime.utcfromtimestamp(x)
if self.x_value_formatter: if self.x_value_formatter:
return self.x_value_formatter(dt) return self.x_value_formatter(dt)
return dt.isoformat() return dt.isoformat()

Loading…
Cancel
Save