Skip to content

Commit

Permalink
Fix x formatting token on Python 2.7 (#781)
Browse files Browse the repository at this point in the history
* Fix bug with x token in formatter on Python 2.7

* Tweak Makefile to run tests before publishing
  • Loading branch information
jadchaar authored May 3, 2020
1 parent 13d4562 commit 6136257
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ clean:
rm -rf dist build .egg .eggs arrow.egg-info
rm -f ./**/*.pyc .coverage

publish:
publish: test
rm -rf dist build .egg .eggs arrow.egg-info
pip3 install -U setuptools twine wheel
python3 setup.py sdist bdist_wheel
Expand Down
4 changes: 3 additions & 1 deletion arrow/formatter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import absolute_import, division

import calendar
import re
Expand Down Expand Up @@ -98,9 +98,11 @@ def _format_token(self, dt, token):
return str(int(dt.microsecond / 100000))

if token == "X":
# TODO: replace with a call to dt.timestamp() when we drop Python 2.7
return str(calendar.timegm(dt.utctimetuple()))

if token == "x":
# TODO: replace with a call to dt.timestamp() when we drop Python 2.7
ts = calendar.timegm(dt.utctimetuple()) + (dt.microsecond / 1000000)
return str(int(ts * 1000000))

Expand Down
13 changes: 7 additions & 6 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import time
from datetime import datetime

import pytest
Expand Down Expand Up @@ -101,13 +100,15 @@ def test_sub_second(self):

def test_timestamp(self):

timestamp = time.time()
timestamp = 1588437009.8952794
dt = datetime.utcfromtimestamp(timestamp)
assert self.formatter._format_token(dt, "X") == str(int(timestamp))
expected = str(int(timestamp))
assert self.formatter._format_token(dt, "X") == expected

# time.time() may return a float with greater than 6 digits of precision
rounded_ts = str(round(timestamp * 1000000))
assert self.formatter._format_token(dt, "x") == rounded_ts.format("{f}")
# Must round because time.time() may return a float with greater
# than 6 digits of precision
expected = str(int(timestamp * 1000000))
assert self.formatter._format_token(dt, "x") == expected

def test_timezone(self):

Expand Down

0 comments on commit 6136257

Please sign in to comment.