Problem 5: Time (100pts)

In everyday programming, we often need to store dates. Typically, this involves storing three separate values: the year, the month, and the day. However, manually handling these values in separate variables each time can be cumbersome. To make it easier to store and display dates, we want to implement a class Time that will help us manage these values more conveniently.

We create a class called Time that represents a specific date. The class should have a constructor that accepts a year, month, and day. Additionally, it should include the following methods:

  1. setyear(self, year): Sets the year of the time object self to year.
  2. setmonth(self, month): Sets the month of the time object self to month.
  3. setday(self, day): Sets the day of the time object self to day.
  4. setformat(self, format_str): Accepts a string format_str that includes 'yy', 'mm', and 'dd'. The format string specifies how the date should be displayed when the to_str method is called. The default format string should be 'yy.mm.dd'.

The format_str parameter in the setformat method allows you to specify how the date should be displayed as a string. It is a string that defines the order and format in which the year (yy), month (mm), and day (dd) should appear when the to_str method is called.

  • yy represents the two-digit year.
  • mm represents the two-digit month.
  • dd represents the two-digit day.

You can arrange these placeholders in any order you like and separate them with any delimiter, such as a dot (.), slash (/), or hyphen (-). For example:

  • "yy.mm.dd" will display the date as 25.12.25 for the year 2025, month December, and day 25.
  • "dd-mm-yy" will display the date as 25-12-25.
  • "mm/dd/yy" will display the date as 12/25/25.
  • "yy.mm" will display the date as 25.12.

The initial format_str will be 'yy.mm.dd' when a Time object is created. yy, mm, and dd can appear multiple times or not appear at all. For example, 'yy/mm', 'yy.mm.dd yy/mm/dd', or even 'yyy' are all valid formats (you only need to treat the first two y as the year).

Hint1: When the parameter given to setyear, setmonth, or setday is invalid (for example, if the month is 13 or the month is given as a string like "a"), you need to print Parameter Error! and do not perform any other operations.

Hint2: To simplify the problem, you can assume that every month has 31 days.

Hint3: You can use isinstance(x, int) to check if x is an integer.

Hint4: You might find some built-in string methods helpful, like replace, zfill, etc.

class Time:
    """ A class that can store and display the date.
    >>> time = Time(2024, 11, 20)
    >>> print(time.to_str())
    24.11.20
    >>> time.setyear(2023)
    >>> time.setmonth(2)
    >>> time.setday(5)
    >>> time.setformat("dd-mm-yy")
    >>> time.to_str()
    '05-02-23'
    >>> time.setformat("yy/mm/dd")
    >>> time.to_str()
    '23/02/05'
    >>> time.setyear(-1)
    Parameter Error!
    >>> time.to_str()
    '23/02/05'
    """
    def __init__(self, year, month, day):
        """Initialize a Time object."""
        "*** YOUR CODE HERE ***"

    def setyear(self, year):
        """Set the year of the Time object."""
        "*** YOUR CODE HERE ***"

    def setmonth(self, month):
        """Set the month of the Time object."""
        "*** YOUR CODE HERE ***"

    def setday(self, day):
        """Set the day of the Time object."""
        "*** YOUR CODE HERE ***"

    def setformat(self, format):
        """Set the format of the Time object."""
        "*** YOUR CODE HERE ***"

    def to_str(self):
        """Return the formatted date."""
        "*** YOUR CODE HERE ***"