Skip to content

Commit

Permalink
Fix timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Jul 10, 2015
1 parent 14a7a32 commit 49f2c5d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
13 changes: 8 additions & 5 deletions demo/moulinrouge/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,13 +736,16 @@ def test_only_zeroes():
@app.route('/test/datetimeline')
def test_datetimeline():
line = DateTimeLine()
from datetime import timezone, timedelta
tz7 = timezone(timedelta(hours=7), 'GMT +7')
tzn4 = timezone(timedelta(hours=-4), 'GMT -4')

line.add('dt', [
(datetime(2013, 1, 12, 8, 0), 300),
(datetime(2013, 1, 12, 12), 412),
(datetime(2013, 2, 22, 12), 823),
(datetime(2013, 2, 22, 20), 672)
(datetime(2013, 1, 12, 8, tzinfo=tz7), 300),
(datetime(2013, 1, 12, 8), 412),
(datetime(2013, 1, 12, 8, tzinfo=tzn4), 823)
])
line.x_value_formatter = lambda x: x.strftime("%Y-%m-%d")
line.x_value_formatter = lambda x: x.isoformat() # strftime("%Y-%m-%d")
line.x_label_rotation = 45
return line.render_response()

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Changelog
* Add flake8 test to py.test in tox
* Remove stroke style in style and set it as a global / serie configuration.
* Fix None values in tables
* Fix timezones in DateTimeLine

1.7.0
=====
Expand Down
5 changes: 5 additions & 0 deletions docs/documentation/types/xy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ DateTime
(datetime(2013, 2, 22, 9, 45), 672)
])

.. caution::

datetime are taken in utc by default (ie: no tzinfo).
If you have dates with timezones ensure that all your dates
have timezone otherwise you will have incoherences.

Date
++++
Expand Down
6 changes: 4 additions & 2 deletions pygal/graph/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
"""
from pygal.adapters import positive
from pygal.graph.xy import XY
from datetime import datetime, date, time, timedelta
from datetime import datetime, date, time, timedelta, timezone
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

Expand Down Expand Up @@ -82,7 +84,7 @@ class DateTimeLine(XY):
def _x_format(self):
"""Return the value formatter for this graph"""
def datetime_to_str(x):
dt = datetime.fromtimestamp(x)
dt = datetime.fromtimestamp(x, timezone.utc)
if self.x_value_formatter:
return self.x_value_formatter(dt)
return dt.isoformat()
Expand Down
8 changes: 4 additions & 4 deletions pygal/test/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def test_datetime():
assert list(
map(lambda t: t.split(' ')[0],
q(".axis.x text").map(texts))) == [
'2013-01-12T15:13:20',
'2013-01-24T05:00:00',
'2013-02-04T18:46:40',
'2013-02-16T08:33:20']
'2013-01-12T14:13:20+00:00',
'2013-01-24T04:00:00+00:00',
'2013-02-04T17:46:40+00:00',
'2013-02-16T07:33:20+00:00']


def test_timedelta():
Expand Down

0 comments on commit 49f2c5d

Please sign in to comment.