From d900ed2d2cc9a8ebf460c52e818ae720f9ed4952 Mon Sep 17 00:00:00 2001 From: wtty-fool Date: Sat, 21 Apr 2018 20:16:24 +0200 Subject: [PATCH 1/2] Added conversion to and from Mayan Long Count --- src/maya/core.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/maya/core.py b/src/maya/core.py index 62ef276..f32cf75 100644 --- a/src/maya/core.py +++ b/src/maya/core.py @@ -212,6 +212,16 @@ def from_iso8601(klass, iso8601_string): """Returns MayaDT instance from iso8601 string.""" return parse(iso8601_string) + @classmethod + def from_long_count(klass, long_count_string): + """Returns MayaDT instance from Maya Long Count string.""" + days_since_creation = -1856305 + factors = (144000, 7200, 360, 20, 1) + for i, value in enumerate(long_count_string.split('.')): + days_since_creation += int(value) * factors[i] + days_since_creation *= 3600 * 24 + return klass(epoch=days_since_creation) + @staticmethod def from_rfc2822(rfc2822_string): """Returns MayaDT instance from rfc2822 string.""" @@ -269,6 +279,22 @@ def rfc3339(self): """Returns an RFC 3339 representation of the MayaDT.""" return self.datetime().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5] + "Z" + def long_count(self): + """Returns a Mayan Long Count representation of the Maya DT.""" + # Creation (0.0.0.0.0) occurred on -3114-08-11 + # 1856305 is distance (in days) between Creation and UNIX epoch + days_since_creation = int(1856305 + self._epoch / (3600*24)) + caps = (0, 20, 20, 18, 20) + lc_date = [0, 0, 0, 0, days_since_creation] + for i in range(4, 0, -1): + if lc_date[i] >= caps[i]: + lc_date[i-1] += int(lc_date[i]/caps[i]) + lc_date[i] %= caps[i] + elif lc_date[i] < 0: + lc_date[i-1] += int(lc_date[i]/caps[i]) + lc_date[i] = 0 + return '.'.join(str(i) for i in lc_date) + # Properties # ---------- @property From 8493dd7e8421e539f45e65870267569a6b153ce3 Mon Sep 17 00:00:00 2001 From: wtty-fool Date: Sat, 21 Apr 2018 20:16:38 +0200 Subject: [PATCH 2/2] Mayan Long Count tests --- tests/test_maya.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_maya.py b/tests/test_maya.py index 96c922e..ca8ffc5 100644 --- a/tests/test_maya.py +++ b/tests/test_maya.py @@ -30,6 +30,21 @@ def test_iso8601(string, expected): assert r == d.iso8601() +@pytest.mark.parametrize( + "string,expected", + [ + ('January 1, 1970', "12.17.16.7.5"), + ('December 21, 2012', "13.0.0.0.0"), + ('March 4, 1900', "12.14.5.10.0"), + ], +) +def test_long_count(string, expected): + r = maya.parse(string).long_count() + d = maya.MayaDT.from_long_count(r) + assert r == expected + assert r == d.long_count() + + @pytest.mark.parametrize( "string,expected", [