3. Dates and Times

Introduction

"Dates and time are objects in Smalltalk too ... of course!"

Date today. "Evaluates to an instance of class Date."
Time now. "Evaluates to an instance of class Time."
DateTime now. "Evaluates to an instance of class DateTime."

"Here are some examples of the messages that DateTime responds to
instances of Date and Time respond to similar messages as appropriate."

|myDateTime|
myDateTime := DateTime now.
myDateTime second. " - the seconds part of the DateTime.  An Integer"
myDateTime minute. " - the minutes part"
myDateTime hour. "- the hours part 24hr clock by default"
myDateTime hour24. "- as above, but explicitly the 24 hours clock"
myDateTime hour12. "- as above but with 12 hours clock"
myDateTime dayOfWeek. " -  the integer day index.  1=Sunday"
myDateTime dayOfWeekName. "- day of the week as a Symbol, e.g. #Tuesday"
myDateTime dayOfMonth. "-  day of the month - an Integer."
myDateTime month. "- month of the year.  Month index - an integer."
myDateTime monthName. "- name of the month as a Symbol, e.g. #June"
myDateTime year. " - the year number as an Integer, e.g. 2005"
myDateTime dayOfYear. "- integer day number of the day within the year."

"... and many more.  See the DateTime, Date and Time classes for the
full story."

Finding Today's Date

"We saw Date, Time and DateTime objects being created in the
introduction.  Let's now see how we can show the value of these
objects."

Date today printString.   " ->  '3-Jun-2005' "
Time now printString.  " ->  '22:09:35' "
DateTime now printString. " -> ' 2005-06-03T22:10:22+10:00' "

"The message >>printString can be sent to any object.  It is up to the
object to return an interesting instance of class String."

Converting DMYHMS to Epoch Seconds

"We can create arbitrary dates and time using other class methods on the
Date, Time and DateTime classes."

|myDateTime|

"The following creates a DateTime of the time I wrote this code as if it
was GMT"
myDateTime := DateTime year: 2005 month: 6 day: 3 hour: 22 minute: 18
second: 26.

"The following creates the DateTime with the correct offset for Sydney,
Australia."
myDateTime := DateTime year: 2005 month: 6 day: 3 hour: 22 minute: 18
second: 26 offset: (Duration days: 0 hours: 10 minutes: 0 seconds: 0).

"And now we can get the seconds from myDateTime."
myDateTime asSeconds.  "Evaluates to an Integer"
myDateTime asSeconds printString. "Evaluates to an instance of String
representing the above integer."

Converting Epoch Seconds to DMYHMS

"We can also create a Dates and Times from a number of seconds"

| myDateTime aNumberOfSeconds aDate aTime|
myDateTime := DateTime year: 2005 month: 6 day: 3 hour: 22 minute: 18
second: 26 offset: (Duration days: 0 hours: 10 minutes: 0 seconds: 0).
aNumberOfSeconds := myDateTime asSeconds.
aDate := Date fromSeconds: aNumberOfSeconds.
aTime := Time fromSeconds: aNumberOfSeconds.

Adding to or Subtracting from a Date

Difference of Two Dates

Day in a Week/Month/Year or Week Number

Parsing Dates and Times from Strings

Printing a Date

High-Resolution Timers

Short Sleeps

Program: hopdelta