Skip to content

Commit

Permalink
Merge pull request #152 from kennethreitz/feature/local-datetime
Browse files Browse the repository at this point in the history
Implement method to get MayaDT instance as local timezone-aware datetime instance
  • Loading branch information
timofurrer authored May 10, 2018
2 parents 7a750a1 + 4056d1a commit eeb07d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions maya/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions tests/test_maya.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<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'),
Expand Down

0 comments on commit eeb07d4

Please sign in to comment.