5.1.1 Reasons for defining two similar classes
The <time-offset> class is similar to the <time-of-day> class. They both define a total-seconds slot. Why do we need to have two classes that are so similar?
A
<time-of-day>is conceptually different from a<time-offset>. If thetotal-secondsslot of a<time-of-day>is180, that means the time of day at 0:03 (that is, 3 minutes past midnight). If thetotal-secondsslot of a<time-offset>is180, that means 3 minutes in the future. If you ask what time it is, the answer is a<time-of-day>. If you ask how long it takes to wash the dog, the answer is a<time-offset>.A
<time-offset>can represent time in the past by having a negative value oftotal-seconds. A<time-of-day>, in contrast, should not have a negative value oftotal-seconds. Later in this book, we provide methods that guarantee that thetotal-secondsslot of<time-of-day>instances is not negative; see Section 10.2.2, page 120, and Section 10.3, page 123.We need different methods for describing instances of
<time-offset>and instances of<time-of-day>. The<time-of-day>method prints8:30, and the<time-offset>method should printminus 8:30orplus 8:30.Eventually, we will need to be able to add a
<time-of-day>to a<time-offset>. For example, we can add the<time-of-day>9:03 to the<time-offset>2:50 and get the<time-of-day>11:53. We will also need to add two<time-offset>instances. For example, 2 minutes plus 8 minutes is equal to 10 minutes. But we cannot add two<time-of-day>instances, because it does not make sense to add three o'clock to four o'clock.




