Improve date formatting

This commit is contained in:
Edward Betts 2022-04-08 10:35:21 +01:00
parent 79111a25de
commit c1e53c727f
2 changed files with 6 additions and 2 deletions

View file

@ -180,11 +180,12 @@ def format_wikibase_time(v):
# example: https://www.wikidata.org/wiki/Q108266998 # example: https://www.wikidata.org/wiki/Q108266998
if p == 11: 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: if p == 10:
return date.fromisoformat(t[1:8] + "-01").strftime("%B %Y") return date.fromisoformat(t[1:8] + "-01").strftime("%B %Y")
if p == 9: if p == 9:
return t[1:5] return t[1:5]
if p == 7: if p == 7:
century = ((int(t[:5]) - 1) // 100) + 1 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

View file

@ -12,3 +12,6 @@ def test_format_wikibase_time_century():
v = {"time": "+1950-00-00T00:00:00Z", "precision": 7} v = {"time": "+1950-00-00T00:00:00Z", "precision": 7}
assert utils.format_wikibase_time(v) == "20th century" 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"