diff --git a/demo/moulinrouge/tests.py b/demo/moulinrouge/tests.py index 5a77701..aea539c 100644 --- a/demo/moulinrouge/tests.py +++ b/demo/moulinrouge/tests.py @@ -736,13 +736,16 @@ def get_test_routes(app): @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() diff --git a/docs/changelog.rst b/docs/changelog.rst index bbc8b22..7726c29 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 ===== diff --git a/docs/documentation/types/xy.rst b/docs/documentation/types/xy.rst index 7ca171c..9d03de6 100644 --- a/docs/documentation/types/xy.rst +++ b/docs/documentation/types/xy.rst @@ -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 ++++ diff --git a/pygal/graph/time.py b/pygal/graph/time.py index 57892c4..9c1ec7c 100644 --- a/pygal/graph/time.py +++ b/pygal/graph/time.py @@ -21,12 +21,14 @@ Various datetime line plot """ 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 @@ -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() diff --git a/pygal/test/test_date.py b/pygal/test/test_date.py index 83db53d..5c5842f 100644 --- a/pygal/test/test_date.py +++ b/pygal/test/test_date.py @@ -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():