Introduction to Data Structures CS106 Spring 2012

Lab 1: Telling time on the planet “Regulus”

Our idea of a current date (such as January 23, 2004) or time (such as 12:30PM and 0 seconds) are defined in terms of how long it has been since a particular event (related to the start date of the Christian calendar, or the beginning of the day at midnight). In some cases, these definitions involve subtleties, such as the adjustment of clocks for daylight savings time or when crossing time zones, or the adjustment of the calendar in the Gregorian Reformation (compare the calendars printed by the unix calendar program for January 2004 (type “cal 1 2004” in a terminal window) and September 1752 (type “cal 9 1752”)).

Some computer systems represent dates and times in the terms that would be shown on the calendar and clock. Others use a different representation; for example, the date and time can be represented in terms of the number of seconds since the start of January 1, 1970 UTC. Each representation makes some operations easy and others hard (e.g. printing as a string, or finding the number of minutes between two times).

In this lab, you will create classes to represent date and time information. To reduce the number of arbitrary rules you'll need to program (such as leap years), you will write a class representing time on the hypothetical planet “Regulus”: Regulus has days that are exactly as long as Earth days (24 hours, each of 60 minutes, each of which is 60 seconds long), but the year contains exactly 360 days, which the occupants group into 12 months of 30 days each. Years are counted from the original landing of explorers or Regulus.

Regulus has one long, thin continent, and thus no need for time zones. However, this continent is well north of the equator, and Regulus has significant axial tilt, so daylight savings time is used. Everyone sets their clocks forward by 1 hour on April 1, at 2AM, to go from RST to RDT; everyone sets their clocks back by 1 hour on October 1, the first time the clocks read 3AM.

Get the starter files for the Regulus_time project, and create classes that represent date and time information on Regulus in two different ways: one class (Time_represented_by_seconds) should use a count of seconds, as on the original Unix operating system; the other class (Time_represented_by_clock_and_calendar) should use separate fields for days, hours, minutes, etc.. In the former, operations such as adding a certain number of minutes will be easy, but printing will be hard; in the latter, adding minutes is tricky but printing is easy note that most of the non-trivial steps will appear somewhere in each version, so you may want to build and debug one class first, and then cut-and-paste various bits of your algorithm as you create the other. The test suite expects that Time will be defined as one of your time classes, for example by setting Time = Time_represented_by_clock_and_calendar as in the DocTest section.

The two classes should respond to the following messages in exactly the same way it should be impossible to determine which class is in use through any means other than looking at the source code or measuring which operations are fast and which are slow. Each class should have:

Your time classes are not required to work for times before landing (i.e. years before 1).

You should also create files

Remember to submit your work when you're done, and to make backup copies (also via Team->Commit) whenever you achieve significant progress.