29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14.3 The class Date<br />

Class Date, a subclass of Magnitude, represents a particular day, in a particular month of a<br />

particular year. A date object c<strong>an</strong> be created in a number of ways . For example, it is possible to request<br />

the date today <strong><strong>an</strong>d</strong> thus create a date object:<br />

todaysDate := Date today.<br />

It is also possible to create a date object for <strong>an</strong> explicit date, using the<br />

For example:<br />

newDay:month:year:.<br />

birthDate := Date newDay: 23 month: #Sep year: 1964.<br />

Note that only the first three letters of the month name are signific<strong>an</strong>t <strong><strong>an</strong>d</strong> that if the century is not<br />

provided in the year parameter (e.g. if 64 had been passed in rather th<strong>an</strong> 1964), then the current century<br />

is used as a default.<br />

The Date class also allows date objects to be compared, added, subtracted etc. by virtue of its<br />

inherited abilities from Magnitude. For example:<br />

(todaysDate < birthDate) ifTrue: [Tr<strong>an</strong>script show: 'Weird!'].<br />

Tr<strong>an</strong>script show: (todaysDate subtractDate: birthDate) printString.<br />

This will calculate the difference between today’s date <strong><strong>an</strong>d</strong> the 23rd of September 1964. The result<br />

will be printed in terms of the difference in days. To convert this into (<strong>an</strong> approximate) number of years,<br />

you c<strong>an</strong> divide by 365. This ignores leap years. For example:<br />

Tr<strong>an</strong>script show: ((todaysDate subtractDate: birthDate) // 365).<br />

Question: Why have I used // rather th<strong>an</strong> / ? Answer, because / will return a fraction if the number of<br />

days c<strong>an</strong>not be divided exactly by 365, where as // will ignore <strong>an</strong>y remainder.<br />

There are also a r<strong>an</strong>ge of other messages which are supported by the class Date. These messages<br />

allow you to determine if the date object is in a leap year, which day of the year it is, which month the<br />

date object is in, which year it is part of etc. Some examples using todaysDate are presented below:<br />

todaysDate day.<br />

todaysDate leap.<br />

todaysDate monthName.<br />

todaysDate weekday.<br />

todaysDate year.<br />

Class Date also supports a r<strong>an</strong>ge of messages itself. These messages provide additional behavio r<br />

related to dates. For example, the Date class will respond to messages requesting the number of days<br />

in a particular month for a particular year:<br />

Date daysInMonth: #Feb forYear: 1997.<br />

Another class method c<strong>an</strong> be used to determine if a given year is a l<br />

using the leapYear: message to the Date class:<br />

eap year or not. This is done<br />

Date leapYear: 1999.<br />

14.4 The class Time<br />

The class Time, provides a similar set of features for objects which represent a specific inst<strong>an</strong>t of time<br />

(to a particular second) as the class Date does for a specific calendar date. A new inst<strong>an</strong>ce of the class<br />

Time c<strong>an</strong> be created using the inst<strong>an</strong>ce creation message now or by reading from a string. The string<br />

c<strong>an</strong> contain hours, minutes <strong><strong>an</strong>d</strong> seconds separated by colons (‘11:59:20’). It c<strong>an</strong> also contain <strong>an</strong> AM or<br />

PM designation. For example:<br />

currentTime := Time now.<br />

previousTime := Time readFromString: '1:10:20 am'.<br />

119

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!