CS 3723
Programming Languages  
Fall 2014
  Homework 12. Time/Date Classes 
Week 15-16: Dec 1 - 10

Submit following directions at: submissions and rules at: rules. Deadlines are:
  • 2014-12-08  23:59:59 (that's Mon,   8 Dec 2014, 11:59:59 pm) for full credit.
  • 2014-12-10  23:59:59 (that's Wed, 10 Dec 2014, 11:59:59 pm) for 75% credit.


Program involving classes: [See the Chapter on Classes.]

Note that even though this problem also involves dates, it is independent of the date problem in the homework: 3: Python REs. You should not have any common code between the two problems. This problem has you working with dates and times. Python, like most programming languages, has library functions for treating times and dates, and in normal applications, you should use such library functions. This problem has you creating classes from scratch in order to learn how to do it. You are not allowed to use the Python timedate library module and its classes for this assignment.

Consider the following Date class in Python. Shown is the entire Date class, along with test code and with the results of running the program.

Date Example
Python Python and Output
# date1.py: class for dates

mon = [None,"Jan","Feb","Mar","Apr",
            "May","Jun","Jul","Aug",
            "Sep","Oct","Nov","Dec"]
days = [0, 31, 28, 31, 30, 31, 30,
           31, 31, 30, 31, 30, 31]
def check_date(y, m, d):
    if m < 1 or m > 12 or d < 0 or d > 31 or y < 0:
        return False
    elif d > 0 and d <= days[m]:
        return True
    elif m == 2 and d == 29 and is_leap(y):
        return True
    else:
        return False
    ### end of: check_date

def is_leap(y):
    if y%400 == 0 or (y%4 == 0 and y%100 != 0):
        return True
    else:
        return False
    ### end of: is_leap

class Date:
    def __init__(self, y, m, d):
        self.year = y
        self.month = m
        self.day = d
        ### end of: __init__

    def __str__(self):
        return mon[self.month] + \
          " " + str(self.day) + \
          ", " + str(self.year)
        ### end of: __str__

    def incr_day(self):
        self.day +=1
        if not (self.month == 2 and  \
              is_leap(self.year) and \
              self.day == 29) and    \
              self.day > days[self.month]:
            self.day = 1
            self.incr_month()
        ### end of: incr_day

    def incr_month(self):
        self.month +=1
        if self.month > 12:
            self.month = 1
            self.incr_year()
        ### end of: incr_month

    def incr_year(self):
        self.year += 1
        ### end of: incr_year
    ### end of: date1 class

def test_date(y, m, d):
    date = Date(y, m, d)
    print "Start Date: ", date, " "
    date.incr_day()
    print " Next day:  ", date  , " "
    date.incr_day()
    print " Next day:  ", date  , " "
    date.incr_day()
    print " Next day:  ", date,  "\n"
print "TEST ORDINARY DATES"
test_date(2004, 4, 22)
test_date(2013, 3, 30)
test_date(2013, 4, 29)
print "\nTEST YEAR END"
test_date(2001, 12, 30)
test_date(1999, 12, 30)
print "\nTEST LEAP YEARS"
test_date(2004, 2, 28)
test_date(2001, 2, 27)
test_date(2000, 2, 28)
test_date(1900, 2, 27)
test_date(2100, 2, 27)

% python date1.py TEST ORDINARY DATES Start Date: Apr 22, 2004 Next day: Apr 23, 2004 Next day: Apr 24, 2004 Next day: Apr 25, 2004 Start Date: Mar 30, 2013 Next day: Mar 31, 2013 Next day: Apr 1, 2013 Next day: Apr 2, 2013 Start Date: Apr 29, 2013 Next day: Apr 30, 2013 Next day: May 1, 2013 Next day: May 2, 2013 TEST YEAR END Start Date: Dec 30, 2001 Next day: Dec 31, 2001 Next day: Jan 1, 2002 Next day: Jan 2, 2002 Start Date: Dec 30, 1999 Next day: Dec 31, 1999 Next day: Jan 1, 2000 Next day: Jan 2, 2000 TEST LEAP YEARS Start Date: Feb 28, 2004 Next day: Feb 29, 2004 Next day: Mar 1, 2004 Next day: Mar 2, 2004 Start Date: Feb 27, 2001 Next day: Feb 28, 2001 Next day: Mar 1, 2001 Next day: Mar 2, 2001 Start Date: Feb 28, 2000 Next day: Feb 29, 2000 Next day: Mar 1, 2000 Next day: Mar 2, 2000 Start Date: Feb 27, 1900 Next day: Feb 28, 1900 Next day: Mar 1, 1900 Next day: Mar 2, 1900 Start Date: Feb 27, 2100 Next day: Feb 28, 2100 Next day: Mar 1, 2100 Next day: Mar 2, 2100


What you should do:

  1. Create a Time class: In the same file add another class Time that will keep track of hours, minutes, and seconds. This new class should be similar to the Date class. although simpler. In particular, it should have methods: __init__, __str__, incr_sec, incr_min, and incr_hour.

  2. Test your new Time class by itself: Test it with code to instantiate time objects and to test incrementing by seconds, including showing that it correctly handles the end of a minute, the end of an hour, and the end of 24 hours. For this part you should create test data that prints the following. (You can do this in a way similar to the test of Date above.)

    Possible output
    TEST TIMES
    Start Time:  04:31:26  
     Next sec:   04:31:27  
     Next sec:   04:31:28  
     Next sec:   04:31:29 
    
    Start Time:  07:31:58  
     Next sec:   07:31:59  
     Next sec:   07:32:00  
     Next sec:   07:32:01 
    
    Start Time:  12:59:58  
     Next sec:   12:59:59  
     Next sec:   13:00:00  
     Next sec:   13:00:01 
    
    Start Time:  23:59:58  
     Next sec:   23:59:59  
     Next sec:   00:00:00  
     Next sec:   00:00:01 
    

  3. Let Date inherit from the class object: This is trivial: replace "class Date:" with "class Date(object):". (The class "object" is at the root of all Python classes, whether explicitly defined or not. We need it explicitly here for the inheritance to work.)

  4. Change your Time class so that it inherits the Date class: Then Date is a superclass of Time, and Time is a subclass of Date. (In Java terms, Time extends Date, and in place of the Java "Time extends Date", Python writes "Time(Date)". The initializer for Time should now have 6 parameters, including the ones before, along with year, month, and day. These last three will be passed on into Date using the super method. [Again see the Chapter on Classes.]

  5. Alter the incr_hour method of the Time class: Change it so that in case an increment goes past hour 23, it will wrap around to hour 0 and increment to the next day.

  6. Test your version of Time that inherits from Date: You should test your program with initial data that produces output looking something like the following. (But not the "Comments". You can do this in a way similar to the test of Date above.)

    Possible output Comments
    TEST TIME/DATE
    
    Start time:   Jan 31, 2001 04:31:26
      Next sec:   Jan 31, 2001 04:31:27
      Next sec:   Jan 31, 2001 04:31:28
      Next sec:   Jan 31, 2001 04:31:29
    
    Start time:   Apr 22, 2004 23:59:58
      Next sec:   Apr 22, 2004 23:59:59
      Next sec:   Apr 23, 2004 00:00:00
      Next sec:   Apr 23, 2004 00:00:01
    
    Start time:   Jan 31, 2001 23:59:58
      Next sec:   Jan 31, 2001 23:59:59
      Next sec:   Feb 1, 2001 00:00:00
      Next sec:   Feb 1, 2001 00:00:01
    
    Start time:   Apr 30, 2004 23:59:58
      Next sec:   Apr 30, 2004 23:59:59
      Next sec:   May 1, 2004 00:00:00
      Next sec:   May 1, 2004 00:00:01
    
    Start time:   Feb 28, 2004 23:59:58
      Next sec:   Feb 28, 2004 23:59:59
      Next sec:   Feb 29, 2004 00:00:00
      Next sec:   Feb 29, 2004 00:00:01
    
    Start time:   Feb 28, 1900 23:59:58
      Next sec:   Feb 28, 1900 23:59:59
      Next sec:   Mar 1, 1900 00:00:00
      Next sec:   Mar 1, 1900 00:00:01
    
    Start time:   Feb 28, 2000 23:59:58
      Next sec:   Feb 28, 2000 23:59:59
      Next sec:   Feb 29, 2000 00:00:00
      Next sec:   Feb 29, 2000 00:00:01
    
    Start time:   Feb 28, 2001 23:59:58
      Next sec:   Feb 28, 2001 23:59:59
      Next sec:   Mar 1, 2001 00:00:00
      Next sec:   Mar 1, 2001 00:00:01
    
    Start time:   Dec 31, 2004 23:59:58
      Next sec:   Dec 31, 2004 23:59:59
      Next sec:   Jan 1, 2005 00:00:00
      Next sec:   Jan 1, 2005 00:00:01
    
    Start time:   Dec 31, 1999 23:59:58
      Next sec:   Dec 31, 1999 23:59:59
      Next sec:   Jan 1, 2000 00:00:00
      Next sec:   Jan 1, 2000 00:00:01
    
    
    
    
    Next second
    
    
    
    
    
    Next hour
    
    
    
    
    Next day
    
    
    
    
    Next month
    
    
    
    
    Next day
    (leap year)
    
    
    
    Next month
    (not leap year)
    
    
    
    Next day
    (not leap year)
    
    
    
    Next month
    (leap year)
    
    
    
    Next year
    
    
    
    
    Next century
    

It is permissible to change the code in the class Date, although I didn't need to make any changes.


What you should submit:

If you finished the entire program, then you should submit:

  • The Python source showing the Date class and the Time class that inherits from Date.
  • A run that produces the data shown in item 6 above. (In this case you should NOT submit any run that produces the data shown in item 2 above.)

If you didn't get everything to work, you should submit:

  • Whatever Python code you produced.
  • The results of any runs. If your Date class runs by itself and produces results similar to those in item 2 above, it's important that you show this for part credit.

( Revision date: 2014-11-23. Please use ISO 8601, the International Standard Date and Time Notation.)