From 9d551e223baedc5aaa02204c1f85f351c381721d Mon Sep 17 00:00:00 2001 From: lucas Date: Thu, 14 May 2015 16:16:24 +0800 Subject: [PATCH] Modify tzinfo parameter in `get` api, it now support (str, format, tzinfo=tzinfo()) --- arrow/factory.py | 5 ++++- tests/factory_tests.py | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/arrow/factory.py b/arrow/factory.py index 7fef03c98..76088cf26 100644 --- a/arrow/factory.py +++ b/arrow/factory.py @@ -124,9 +124,12 @@ def get(self, *args, **kwargs): arg_count = len(args) locale = kwargs.get('locale', 'en_us') + tz = kwargs.get('tzinfo', None) # () -> now, @ utc. if arg_count == 0: + if isinstance(tz, tzinfo): + return self.type.now(tz) return self.type.utcnow() if arg_count == 1: @@ -195,7 +198,7 @@ def get(self, *args, **kwargs): # (str, format) -> parse. elif isstr(arg_1) and (isstr(arg_2) or isinstance(arg_2, list)): dt = parser.DateTimeParser(locale).parse(args[0], args[1]) - return self.type.fromdatetime(dt) + return self.type.fromdatetime(dt, tzinfo=tz) else: raise TypeError('Can\'t parse two arguments of types \'{0}\', \'{1}\''.format( diff --git a/tests/factory_tests.py b/tests/factory_tests.py index 911b714ad..1ca4b9c12 100644 --- a/tests/factory_tests.py +++ b/tests/factory_tests.py @@ -146,6 +146,14 @@ def test_two_args_other(self): with assertRaises(TypeError): self.factory.get(object(), object()) + def test_three_args_with_tzinfo(self): + + timefmt = 'YYYYMMDD' + d = '20150514' + + assertEqual(self.factory.get(d, timefmt, tzinfo=tz.tzlocal()), + datetime(2015, 5, 14, tzinfo=tz.tzlocal())) + def test_three_args(self): assertEqual(self.factory.get(2013, 1, 1), datetime(2013, 1, 1, tzinfo=tz.tzutc()))