From 4056d1a9aa6a46c9c9998cb2946429ccf736c6a5 Mon Sep 17 00:00:00 2001 From: Timo Furrer Date: Thu, 10 May 2018 17:22:35 +0200 Subject: [PATCH] Implement method to get MayaDT instance as local timezone-aware datetime instance --- maya/core.py | 8 ++++++++ tests/test_maya.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/maya/core.py b/maya/core.py index 9c7c255..de55115 100644 --- a/maya/core.py +++ b/maya/core.py @@ -257,6 +257,14 @@ def datetime(self, to_timezone=None, naive=False): dt = dt.replace(tzinfo=self._tz) return dt + def local_datetime(self): + """Returns a local timezone-aware datetime object + + It's the same as: + mayaDt.datetime(to_timezone=mayaDt.local_timezone) + """ + return self.datetime(to_timezone=self.local_timezone, naive=False) + def iso8601(self): """Returns an ISO 8601 representation of the MayaDT.""" # Get a timezone-naive datetime. diff --git a/tests/test_maya.py b/tests/test_maya.py index 48bdfc5..39377d7 100644 --- a/tests/test_maya.py +++ b/tests/test_maya.py @@ -342,6 +342,28 @@ def __repr__(self): assert mdt.local_timezone == 'UTC' +def test_getting_datetime_for_local_timezone(monkeypatch): + + @property + def mock_local_tz(self): + class StaticTzInfo(object): + zone = 'Europe/Zurich' + + def __repr__(self): + return "" + + return StaticTzInfo() + + monkeypatch.setattr(maya.MayaDT, '_local_tz', mock_local_tz) + + d = maya.parse('1994-02-21T12:00:00+05:30') + + dt = pytz.timezone('Europe/Zurich').localize( + Datetime(1994, 2, 21, 7, 30)) + + assert d.local_datetime() == dt + + @pytest.mark.parametrize("when_str,snap_str,expected_when", [ ('Mon, 21 Feb 1994 21:21:42 GMT', '@d', 'Mon, 21 Feb 1994 00:00:00 GMT'),