Skip to content

Commit

Permalink
Correctly handle utc timestamp for python 2.x. Backport again several…
Browse files Browse the repository at this point in the history
… things. This may be the last backport for 2.6 as the next time I'm thinking of discontinuing support instead of implementing 2.7 in 2.6... This should fix Kozea#306 and maybe Kozea#302.
  • Loading branch information
paradoxxxzero committed Feb 26, 2016
1 parent 7430ac7 commit ee066cd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
3 changes: 2 additions & 1 deletion demo/moulinrouge/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ def test_secondary_xy():
chart.add(10 * '1b', [(4, 12), (5, 8), (6, 4)], secondary=True)
chart.add(10 * '2b', [(3, 24), (0, 17), (12, 9)], secondary=True)
chart.add(10 * '2', [(8, 23), (21, 1), (5, 0)])
chart.value_formatter = lambda x: str(int(x)) + '+'
return chart.render_response()

@app.route('/test/box')
Expand Down Expand Up @@ -930,7 +931,7 @@ def test_datetimeline():
(datetime(2013, 1, 12, 8), 412),
(datetime(2013, 1, 12, 8, tzinfo=tzn4), 823)
])
line.x_value_formatter = lambda x: x.isoformat() # 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
27 changes: 22 additions & 5 deletions pygal/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division
"""Various hacks for transparent python 2 / python 3 support"""

import sys
from collections import Iterable
import time
from datetime import datetime, timedelta, tzinfo


if sys.version_info[0] == 3:
Expand Down Expand Up @@ -70,16 +71,32 @@ def total_seconds(td):
) / 10 ** 6
return td.total_seconds()

try:
from datetime import timezone
utc = timezone.utc
except ImportError:
class UTC(tzinfo):
def tzname(self, dt):
return 'UTC'

def utcoffset(self, dt):
return timedelta(0)

def dst(self, dt):
return None
utc = UTC()


def timestamp(x):
"""Get a timestamp from a date in python 3 and python 2"""
if x.tzinfo is None:
# Naive dates to utc
x = x.replace(tzinfo=utc)

if hasattr(x, 'timestamp'):
from datetime import timezone
if x.tzinfo is None:
return x.replace(tzinfo=timezone.utc).timestamp()
return x.timestamp()
else:
return time.mktime(x.utctimetuple())
return total_seconds(x - datetime(1970, 1, 1, tzinfo=utc))

try:
from urllib import quote_plus
Expand Down
15 changes: 15 additions & 0 deletions pygal/test/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""Date related charts tests"""

from pygal import DateLine, TimeLine, DateTimeLine, TimeDeltaLine
from pygal._compat import timestamp, utc
from pygal.test.utils import texts
from datetime import datetime, date, time, timedelta

Expand Down Expand Up @@ -158,3 +159,17 @@ def test_date_labels():
'2013-01-01',
'2013-02-01',
'2013-03-01']


def test_utc_timestamping():
assert timestamp(
datetime(2017, 7, 14, 2, 40).replace(tzinfo=utc)
) == 1500000000

for d in (
datetime.now(),
datetime.utcnow(),
datetime(1999, 12, 31, 23, 59, 59),
datetime(2000, 1, 1, 0, 0, 0)
):
assert datetime.utcfromtimestamp(timestamp(d)) == d
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ setenv =
COVERAGE_FILE=.cov-{envname}

commands =
coverage run --source=pygal {envbindir}/py.test pygal/test --junitxml=junit-{envname}.xml --flake8
coverage run --source=pygal {envbindir}/py.test {posargs:pygal/test} --junitxml=junit-{envname}.xml --flake8
coverage xml -o coverage-{envname}.xml

0 comments on commit ee066cd

Please sign in to comment.