Legacy date migration that must round-trip

Review the AI's codeEasy18 min

Your task

An old warehouse system stores dates as MM/DD/YY text. The new system stores ISO dates, YYYY-MM-DD. During the cutover the old reporting jobs keep running, so every migrated date is converted back to the old format for them, and the conversion back must reproduce the original text exactly. A coding assistant wrote the forward migration:

def migrate_dates(values):
    out = []
    for v in values:
        m, d, y = v.split("/")
        out.append(f"20{y}-{m.zfill(2)}-{d.zfill(2)}")
    return out

Review it against the contract below and write a corrected migrate_dates(values), returning one result per input, in order.

  • A value is migrated only if it is exactly 8 characters: two digits, /, two digits, /, two digits. Anything else ("3/4/97", "03/04/1997", a leading or trailing space) cannot round-trip, because converting it back would produce different text.
  • The month must be 01 to 12 and the day must exist in that month and year. February has 29 days in a leap year (a year divisible by 4, except years divisible by 100 that are not divisible by 400).
  • Two-digit years use a pivot: 70 to 99 mean 1970 to 1999, and 00 to 69 mean 2000 to 2069.
  • A valid value becomes "YYYY-MM-DD". Every other value becomes "REJECT", so it can be fixed by hand rather than migrated wrong.

Examples:

  • migrate_dates(["03/14/97", "12/31/05"]) returns ["1997-03-14", "2005-12-31"].
  • migrate_dates(["3/14/97", "02/29/23", "02/29/00"]) returns ["REJECT", "REJECT", "2000-02-29"]: the first would come back as "03/14/97", 2023 is not a leap year, and 2000 is.
  • migrate_dates(["13/01/20", "04/31/20"]) returns ["REJECT", "REJECT"].

Examples

migrate_dates(values: list[str]) -> list[str]
Example 1
Inputmigrate_dates(["03/14/97", "12/31/05"])
Output["1997-03-14", "2005-12-31"]
Example 2
Inputmigrate_dates(["3/14/97", "02/29/23", "02/29/00"])
Output["REJECT", "REJECT", "2000-02-29"]
Example 3
Inputmigrate_dates(["13/01/20", "04/31/20"])
Output["REJECT", "REJECT"]