Skip to content

Commit 1d51a43

Browse files
committed
Summary: Fix the yyyymmdd to strftime format converter to use a single
pass through the format so that it actually works correctly - Legacy-Id: 8732
1 parent b7f6be4 commit 1d51a43

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

ietf/utils/fields.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,29 @@ def validate(self, value):
2525
validate_email(email)
2626

2727
def yyyymmdd_to_strftime_format(fmt):
28-
return (fmt
29-
.replace("yyyy", "%Y")
30-
.replace("yy", "%y")
31-
.replace("mm", "%m")
32-
.replace("m", "%-m")
33-
.replace("MM", "%B")
34-
.replace("M", "%b")
35-
.replace("dd", "%d")
36-
.replace("d", "%-d")
37-
.replace("MM", "%A")
38-
.replace("M", "%a")
39-
)
28+
translation_table = sorted([
29+
("yyyy", "%Y"),
30+
("yy", "%y"),
31+
("mm", "%m"),
32+
("m", "%-m"),
33+
("MM", "%B"),
34+
("M", "%b"),
35+
("dd", "%d"),
36+
("d", "%-d"),
37+
], key=lambda t: len(t[0]), reverse=True)
38+
39+
res = ""
40+
remaining = fmt
41+
while remaining:
42+
for pattern, replacement in translation_table:
43+
if remaining.startswith(pattern):
44+
res += replacement
45+
remaining = remaining[len(pattern):]
46+
break
47+
else:
48+
res += remaining[0]
49+
remaining = remaining[1:]
50+
return res
4051

4152
class DatepickerDateField(forms.DateField):
4253
"""DateField with some glue for triggering JS Bootstrap datepicker."""

0 commit comments

Comments
 (0)