From 2845e5dee0356acda0e2921bcfde9ba89106c23b Mon Sep 17 00:00:00 2001 From: Lenqth Date: Mon, 19 Apr 2021 15:31:37 +0900 Subject: [PATCH] Fallback on error in datetime() before year 1970 --- src/maya/core.py | 5 ++++- tests/test_maya.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/maya/core.py b/src/maya/core.py index 1b5eb8d..8795c21 100644 --- a/src/maya/core.py +++ b/src/maya/core.py @@ -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: diff --git a/tests/test_maya.py b/tests/test_maya.py index a528309..fa4b63e 100644 --- a/tests/test_maya.py +++ b/tests/test_maya.py @@ -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")