Browse Source

Fix timezones

pull/242/head
Florian Mounier 10 years ago
parent
commit
49f2c5d246
  1. 13
      demo/moulinrouge/tests.py
  2. 1
      docs/changelog.rst
  3. 5
      docs/documentation/types/xy.rst
  4. 6
      pygal/graph/time.py
  5. 8
      pygal/test/test_date.py

13
demo/moulinrouge/tests.py

@ -736,13 +736,16 @@ def get_test_routes(app):
@app.route('/test/datetimeline') @app.route('/test/datetimeline')
def test_datetimeline(): def test_datetimeline():
line = 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', [ line.add('dt', [
(datetime(2013, 1, 12, 8, 0), 300), (datetime(2013, 1, 12, 8, tzinfo=tz7), 300),
(datetime(2013, 1, 12, 12), 412), (datetime(2013, 1, 12, 8), 412),
(datetime(2013, 2, 22, 12), 823), (datetime(2013, 1, 12, 8, tzinfo=tzn4), 823)
(datetime(2013, 2, 22, 20), 672)
]) ])
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 line.x_label_rotation = 45
return line.render_response() return line.render_response()

1
docs/changelog.rst

@ -27,6 +27,7 @@ Changelog
* Add flake8 test to py.test in tox * Add flake8 test to py.test in tox
* Remove stroke style in style and set it as a global / serie configuration. * Remove stroke style in style and set it as a global / serie configuration.
* Fix None values in tables * Fix None values in tables
* Fix timezones in DateTimeLine
1.7.0 1.7.0
===== =====

5
docs/documentation/types/xy.rst

@ -54,6 +54,11 @@ DateTime
(datetime(2013, 2, 22, 9, 45), 672) (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 Date
++++ ++++

6
pygal/graph/time.py

@ -21,12 +21,14 @@ 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 from datetime import datetime, date, time, timedelta, timezone
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
@ -82,7 +84,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) dt = datetime.fromtimestamp(x, timezone.utc)
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()

8
pygal/test/test_date.py

@ -77,10 +77,10 @@ def test_datetime():
assert list( assert list(
map(lambda t: t.split(' ')[0], map(lambda t: t.split(' ')[0],
q(".axis.x text").map(texts))) == [ q(".axis.x text").map(texts))) == [
'2013-01-12T15:13:20', '2013-01-12T14:13:20+00:00',
'2013-01-24T05:00:00', '2013-01-24T04:00:00+00:00',
'2013-02-04T18:46:40', '2013-02-04T17:46:40+00:00',
'2013-02-16T08:33:20'] '2013-02-16T07:33:20+00:00']
def test_timedelta(): def test_timedelta():

Loading…
Cancel
Save