mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 14:50:19 +00:00
Merge pull request #152 from kennethreitz/feature/local-datetime
Implement method to get MayaDT instance as local timezone-aware datetime instance
This commit is contained in:
@@ -257,6 +257,14 @@ class MayaDT(object):
|
||||
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.
|
||||
|
||||
@@ -342,6 +342,28 @@ def test_core_local_timezone(monkeypatch):
|
||||
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 "<StaticTzInfo 'Europe/Zurich'>"
|
||||
|
||||
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'),
|
||||
|
||||
Reference in New Issue
Block a user