Skip to content

Commit

Permalink
Merge pull request #193 from Lenqth/fix-before-1970
Browse files Browse the repository at this point in the history
Fallback on error in datetime() before year 1970
  • Loading branch information
kennethreitz authored Apr 2, 2024
2 parents 66ec91e + 2845e5d commit 722eb25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/maya/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ def datetime(self, to_timezone=None, naive=False):
if to_timezone:
dt = self.datetime().astimezone(pytz.timezone(to_timezone))
else:
dt = Datetime.utcfromtimestamp(self._epoch)
try:
dt = Datetime.utcfromtimestamp(self._epoch)
except: # Fallback for before year 1970 issue
dt = Datetime.utcfromtimestamp(0) + timedelta(microseconds=self._epoch*1000000)
dt.replace(tzinfo=self._tz)
# Strip the timezone info if requested to do so.
if naive:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_maya.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@ def test_issue_104():
t = maya.MayaDT.from_struct(t)
assert str(t) == "Wed, 11 Oct 2017 21:12:11 GMT"

def test_before_1970():
d1 = maya.when("1899-17-11 08:09:10")
assert d1.year == 1899
assert d1.month == 11
assert d1.day == 17
assert d1.week == 46
assert d1.weekday == 5
assert d1.hour == 8
assert d1.minute == 9
assert d1.second == 10
assert d1.microsecond == 0
# Test properties for maya.parse()
d2 = maya.parse("February 29, 1904 13:12:34")
assert d2.year == 1904
assert d2.month == 2
assert d2.day == 29
assert d2.week == 9
assert d2.weekday == 1
assert d2.hour == 13
assert d2.minute == 12
assert d2.second == 34
assert d2.microsecond == 0

def test_human_when():
r1 = maya.when("yesterday")
Expand Down

0 comments on commit 722eb25

Please sign in to comment.