Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure years are always 4 digits #22

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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