Skip to content

Commit

Permalink
Merge pull request #22 from dh-tech/20-four-digit-years
Browse files Browse the repository at this point in the history
Ensure years are always 4 digits
  • Loading branch information
ColeDCrawford authored Nov 10, 2022
2 parents 3782b7d + 3f186b3 commit c97ea87
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions tests/test_dateformat/test_iso8601.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ def test_parse_range(self):
assert ISO8601DateFormat().parse("1800/1900") == UndateInterval(
Undate(1800), Undate(1900)
)

def test_to_string(self):
# NOTE: iso8601 to_string currently tested more thoroughly
# in undate str tests; may want to move those tests here
assert ISO8601DateFormat().to_string(Undate(900)) == "0900"
assert ISO8601DateFormat().to_string(Undate(33)) == "0033"
2 changes: 1 addition & 1 deletion tests/test_undate.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_str(self):

def test_str_open_range(self):
# 900 -
assert str(UndateInterval(Undate(900))) == "900/"
assert str(UndateInterval(Undate(900))) == "0900/"
# - 1900
assert str(UndateInterval(latest=Undate(1900))) == "../1900"
# - 1900-12
Expand Down
12 changes: 9 additions & 3 deletions undate/dateformat/iso8601.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ def to_string(self, undate: Undate) -> str:
# then combine
for date_portion, known in undate.known_values.items():
if known:
date_parts.append(
undate.earliest.strftime(self.iso_format[date_portion])
)
# NOTE: datetime strftime for %Y for 3-digit year
# results in leading zero in some environments
# and not others; force year to always be 4 digits
if date_portion == "year":
date_parts.append("%04d" % undate.earliest.year)
else:
date_parts.append(
undate.earliest.strftime(self.iso_format[date_portion])
)
elif date_portion == "year":
# if not known but this is year, add '-' for --MM-DD unknown year format
date_parts.append("-")
Expand Down

0 comments on commit c97ea87

Please sign in to comment.