University of Arizona, Department of Computer Science

CSc 120: Parsing Dates

Expected Behavior

Write a Python function canonicalize_date(date_str) that takes a string date_str, representing a date in one of several possible formats, and returns a string that is the canonical representation of that date.

I. Input Representation of Dates

The string passed to the canonicalize_date(date_str) function can be in any of the following three formats.

NOTE: To simplify programming, we will assume that all months have 31 days.

II. Canonical Representation of Dates

A date with year yyyy, month mm, and day dd, where yyyy, mm, and dd are strings of digits, has a canonical (i.e., standard) representation given by
"{:d}-{:d}-{:d}".format( int(yyyy ), int(mm), int(dd))

The reason for converting yyyy, mm, and dd to numbers using int() and then back to strings is to remove any leading zeros.

Examples

  1. Call: canonicalize_date("Mar 7 1997")
    Return value: "1997-3-7"

  2. Call: canonicalize_date("02/30/2017")
    Return value: "2017-2-30"

  3. Call: canonicalize_date("2017-03-01")
    Return value: "2017-3-1"

  4. Call: canonicalize_date("Dec 05 2000")
    Return value: "2000-12-5"