From dafdb69f1dc0c6c6b1fb8bf2aa07383fbb2bed78 Mon Sep 17 00:00:00 2001 From: Jad Chaar Date: Tue, 10 Sep 2019 13:39:21 -0400 Subject: [PATCH 1/2] Fix bug with tzinfo.zone --- arrow/arrow.py | 1 + requirements.txt | 1 + setup.cfg | 2 +- tests/factory_tests.py | 9 +++++++++ tests/parser_tests.py | 14 +++++++++++--- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/arrow/arrow.py b/arrow/arrow.py index 9ed179c15..1b05b42bb 100644 --- a/arrow/arrow.py +++ b/arrow/arrow.py @@ -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) diff --git a/requirements.txt b/requirements.txt index 55b22311f..780f15d9b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/setup.cfg b/setup.cfg index d93e3fc9b..08cae3ddf 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/tests/factory_tests.py b/tests/factory_tests.py index 0e89ffb01..3c9c26590 100644 --- a/tests/factory_tests.py +++ b/tests/factory_tests.py @@ -2,6 +2,7 @@ import time from datetime import date, datetime +import dateparser from chai import Chai from dateutil import tz @@ -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=) + 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 = ( diff --git a/tests/parser_tests.py b/tests/parser_tests.py index 0fe754331..85d8ad07d 100644 --- a/tests/parser_tests.py +++ b/tests/parser_tests.py @@ -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 From f49cd84a73f048cdfaad40f861273ec2d2bd443f Mon Sep 17 00:00:00 2001 From: Jad Chaar Date: Tue, 10 Sep 2019 13:42:31 -0400 Subject: [PATCH 2/2] Renamed variable --- tests/factory_tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/factory_tests.py b/tests/factory_tests.py index 3c9c26590..bccc16376 100644 --- a/tests/factory_tests.py +++ b/tests/factory_tests.py @@ -108,8 +108,8 @@ 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=) 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) + dt_output = self.factory.get(parsed_date)._datetime.replace(tzinfo=tz.tzutc()) + self.assertEqual(dt_output, expected) def test_kwarg_tzinfo(self):