From c1e53c727f1c7b77581522c1d952eb4c09f1bbf4 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 8 Apr 2022 10:35:21 +0100 Subject: [PATCH] Improve date formatting --- matcher/utils.py | 5 +++-- tests/test_utils.py | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/matcher/utils.py b/matcher/utils.py index 81335a0..a47708b 100644 --- a/matcher/utils.py +++ b/matcher/utils.py @@ -180,11 +180,12 @@ def format_wikibase_time(v): # example: https://www.wikidata.org/wiki/Q108266998 if p == 11: - return date.fromisoformat(t[1:11]).strftime("%d %B %Y") + return date.fromisoformat(t[1:11]).strftime("%-d %B %Y") if p == 10: return date.fromisoformat(t[1:8] + "-01").strftime("%B %Y") if p == 9: return t[1:5] if p == 7: century = ((int(t[:5]) - 1) // 100) + 1 - return num2words(century, to="ordinal_num") + " century" + end = " BC" if century < 0 else "" + return num2words(abs(century), to="ordinal_num") + " century" + end diff --git a/tests/test_utils.py b/tests/test_utils.py index e091bc1..2064264 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -12,3 +12,6 @@ def test_format_wikibase_time_century(): v = {"time": "+1950-00-00T00:00:00Z", "precision": 7} assert utils.format_wikibase_time(v) == "20th century" + + v = {"time": "+1868-01-09T00:00:00Z", "precision": 11} + assert utils.format_wikibase_time(v) == "9 January 1868"