Skip to content

Commit

Permalink
Fix bug with tzinfo.zone
Browse files Browse the repository at this point in the history
  • Loading branch information
jadchaar committed Sep 10, 2019
1 parent 256e53f commit dafdb69
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def __init__(
elif (
isinstance(tzinfo, dt_tzinfo)
and hasattr(tzinfo, "localize")
and hasattr(tzinfo, "zone")
and tzinfo.zone
):
tzinfo = parser.TzinfoParser.parse(tzinfo.zone)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
backports.functools_lru_cache==1.5.0; python_version == "2.7"
chai==1.1.2
dateparser==0.7.*
mock==3.0.*
nose==1.3.7
nose-cov==1.6
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ignore = E203,E501,W503
line_length = 88
multi_line_output = 3
include_trailing_comma = true
known_third_party = chai,dateutil,mock,pytz,setuptools,simplejson
known_third_party = chai,dateparser,dateutil,mock,pytz,setuptools,simplejson

[bdist_wheel]
universal = 1
9 changes: 9 additions & 0 deletions tests/factory_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
from datetime import date, datetime

import dateparser
from chai import Chai
from dateutil import tz

Expand Down Expand Up @@ -102,6 +103,14 @@ def test_one_arg_tzinfo(self):

assertDtEqual(self.factory.get(tz.gettz("US/Pacific")), self.expected)

# regression test for issue #658
def test_one_arg_dateparser_datetime(self):
expected = datetime(1990, 1, 1).replace(tzinfo=tz.tzutc())
# dateparser outputs: datetime.datetime(1990, 1, 1, 0, 0, tzinfo=<StaticTzInfo 'UTC\+00:00'>)
parsed_date = dateparser.parse("1990-01-01T00:00:00+00:00")
arrow_obj = self.factory.get(parsed_date)._datetime.replace(tzinfo=tz.tzutc())
self.assertEqual(arrow_obj, expected)

def test_kwarg_tzinfo(self):

self.expected = (
Expand Down
14 changes: 11 additions & 3 deletions tests/parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,18 @@ def test_parse_timestamp(self):
self.parser.parse("{:f}123456".format(float_timestamp), "X"), self.expected
)

negative_timestamp = -1565358758
self.expected = datetime.fromtimestamp(negative_timestamp, tz=tz_utc)
# regression test for issue #662
negative_int_timestamp = -int_timestamp
self.expected = datetime.fromtimestamp(negative_int_timestamp, tz=tz_utc)
self.assertEqual(
self.parser.parse("{:d}".format(negative_timestamp), "X"), self.expected
self.parser.parse("{:d}".format(negative_int_timestamp), "X"), self.expected
)

negative_float_timestamp = -float_timestamp
self.expected = datetime.fromtimestamp(negative_float_timestamp, tz=tz_utc)
self.assertEqual(
self.parser.parse("{:f}".format(negative_float_timestamp), "X"),
self.expected,
)

# NOTE: timestamps cannot be parsed from natural language strings (by removing the ^...$) because it will
Expand Down

0 comments on commit dafdb69

Please sign in to comment.