Short Executable Examples ("Do Its") of Chronos Usage
Examples based on version B1.29 of the Chronos Date/Time Library
The following “do its” provide examples of the functionality and usage of the Chronos Date/Time library.
Example “do its” (executable code snippets) are separated from each other by horizontal lines. Some of the
examples are meant to be “inspected,” some are meant to be “printed,” and some are meant to be evaluated without
being printed or inspected—the latter display their effect on the Transcript.
Examples that display their effect on the Transcript (and which should be executed using the “do it” menu
option) are tagged with the comment “Do it:”
Examples that should be printed in line (and should be executed using the “print it” menu option) are
tagged with the comment “Print it:”
Examples that should be inspected (due to the size and/or complexity of their output) are tagged with
the comment “Inspect it:”
"Print it:"
DateAndTime now.
"Note: DateAndTime is a global alias for Timepoint"
"Print it:"
DateAndTime today.
"Print it:"
YearMonthDay today.
"Print it:"
YearMonthDay today - (DateAndTime year: 1969 month: 7 day: 20)
"Print it:"
DateAndTime now
- (DateAndTime
year: 1969
month: 7
day: 20
hour: 5
minute: 17
second: 40
offset: (Duration hours: -5))
"Print it:"
(DateAndTime now
civilDurationSince:
(DateAndTime
utYear: 1969
month: 7
day: 20
hour: 10
minute: 17
second: 40)) localePrintString
"Inspect it:"
| birthday |
birthday := DayOfMonth month: 6 day: 1.
(1988 to: 2006) collect:
[:year |
| date |
date := birthday inYear: year.
year->date dayOfWeekName]
"Print it:"
Timezone default name
"Note: Timezone is a global alias for ChronosTimezone"
"Print it:"
((Timezone at: 'America/Los_Angeles')
bindingForUTYear: 1968 "Sunday, April 28"
dayOfYear:
((WeekOfMonthDayOfWeek month: 4 week: 0 dayOfWeek: 1)
dayOfYearOrdinalInYear: 1968)
secondsSinceStartOfDayFunction: [:offsetSecondsFromUT |
7199 - offsetSecondsFromUT "1:59:59 am local time"]) offsetFromUT
"Print it:"
"Note: In 1968, DST started at 2am local time on the last Sunday of April
in most of the United States."
((Timezone at: 'America/Los_Angeles')
bindingForUTYear: 1968 "Sunday, April 28
dayOfYear:
((WeekOfMonthDayOfWeek month: 4 week: 0 dayOfWeek: 1)
dayOfYearOrdinalInYear: 1968)
secondsSinceStartOfDayFunction: [:offsetSecondsFromUT |
7200 - offsetSecondsFromUT "2:00:00 am local time"]) offsetFromUT
"Print it:"
(DateAndTime nowIn: 'Pacific/Auckland') printStringUsing: #verbose.
"Print it:"
(DateAndTime nowIn: 'America/Detroit') printStringUsing: #usShort.
"Print it:"
| pointInTime | "Parameters specified in Universal Time"
pointInTime := DateAndTime utYear: 2005 month: 1 day: 1.
Array
with: (pointInTime in: Timezone universal) "Same as `pointInTime asUTC`"
with: (pointInTime in: 'America/Adak')
with: pointInTime
"Note: The local time of <pointInTime> is the current default time zone."
"Print it:"
| pointInTime | "Parameters specified in Universal Time"
pointInTime := DateAndTime utYear: 2005 month: 1 day: 1 hour: 8 minute: 0 second: 0.
Array
with: pointInTime asUTC
with: (pointInTime in: 'America/Los_Angeles')
with: (pointInTime in: 'Asia/Hong_Kong')
"Do it:"
Timepoint showInCommonWorldTimezones: Timepoint now
"Do it:"
Timepoint showInCommonWorldTimezones: Timepoint nominalNow
"Print it:"
DateAndTime isoYear: 2004 week: 1 dayOfWeek: 1
"Print it:"
(DateAndTime year: 2003 month: 12 day: 29)
isoYearWeekDayOfWeekDo:
[:isoYear :isoWeek :isoDayOfWeek |
Array
with: isoYear
with: isoWeek
with: isoDayOfWeek]
"Print it:"
YearMonthDay today daysSinceEpoch
"Print it:"
(YearMonthDay daysSinceEpoch: 0) localePrintString
"Print it:"
Timepoint now fractionalDaysSinceEpoch
"Print it:"
(Timepoint daysSinceEpoch: 0.75) localePrintString
"`LMT' stands for `Local Mean Time'--time zones didn't exist prior to the 1800's"
"Print it:"
(Timepoint utDaysSinceEpoch: Timepoint now utFractionalDaysSinceEpoch)
printStringUsing:
(ChronosPrintPolicy newConfigurable
subSecondTimePrecision: 3;
hideTimeZone)
"Print it:" "Integer day number."
Timepoint now julianDay
"Print it:" "Day and day fraction."
Timepoint now julianDate
"Print it:"
Timepoint utJulianDay: 2440422.928935185s timeZone: 'America/Chicago'.
"Note: Julian Day and Day Fraction in Universal Time;
Timepoint bound to Chicago local time."
"Print it:"
Timepoint julianDay: 2440422.720601852s timeZone: 'America/Chicago'.
"Note: Julian Day and Day Fraction in Chicago local time;
Timepoint bound to Chicago local time."
"Print it:" "Date today according to the Hebrew Calendar"
(YearMonthDay todayAs: #Hebrew) printStringUsing: #rfc2822.
"Print it:" "DateAndTime now in Jerusalem according to the Hebrew Calendar"
(HebrewCalendar clock nowIn: 'Asia/Jerusalem') printStringUsing: #universal.
"Print it:"
| today |
today := IslamicFatimidCalendar clock today.
Array with: today with: (today as: #Persian) with: today asGregorian
"Do it:"
Timepoint showInAllCalendars: Timepoint now
"Do it:"
YearMonthDay showAllCalendarEpochDates.
"Do it:"
Transcript cr.
YearMonthDay todayInAllCalendars do:
[:each | Transcript cr; show: (each printStringUsing: #universal)].
"Do it:"
Transcript cr.
Timepoint todayInAllCalendars do:
[:each | Transcript cr; show: (each printStringUsing: #universal)].
"Inspect it:"
Calendar allRegisteredKeys asArray collect:
[:calendarKey | DateAndTime todayIn: 'Europe/Moscow' as: calendarKey]
"Note that not all Calendars start their day at midnight,
although most use time-of-day clocks that start then."
"Do it:"
| yourBirthday |
yourBirthday :=
DateAndTime
year: 1981
month: 7
day: 15
hour: 16
minute: 33
second: 0.
Transcript cr.
(DateAndTime inAllCalendars: yourBirthday) do: [:each |
Transcript cr; show: (each printStringUsing: #universal)].
"Do it:"
| appollo11 |
appollo11 :=
DateAndTime
utYear: 1969 month: 7 day: 20 hour: 10 minute: 17 second: 40
timeZone: 'America/Chicago'.
Transcript cr.
(DateAndTime inAllCalendars: appollo11) do: [:each |
Transcript cr; show: (each printStringUsing: #universal)].
"Inspect it:"
DateAndTime nowInAllTimeZonesAndCalendars
"Inspect it:"
DateAndTime
inAllTimeZonesAndCalendars:
(DateAndTime
year: 1969
month: 7
day: 20
hour: 10
minute: 17
second: 40
timeZone: Timezone universal)
"Do it:"
"Nominal Dates for all holidays meaningful in the United States during 2006."
| list |
list := SortedCollection sortBlock: [:a :b | a value < b value].
SemanticDatePolicy unitedStates
nominalAnnualEventOccurenceDatesInYear: 2006
do: [:semanticKey :date | list add: semanticKey->date].
Transcript cr.
list do: [:assoc |
Transcript
cr; show: (assoc value % #rfc2822);
show: ':';
tab; show: assoc key]
"Do it:"
"All US Federal Holidays for the next year as observed
by the US Government--starting from today."
| list |
list := SortedCollection sortBlock: [:a :b | a key < b key].
SemanticDatePolicy unitedStates
observedAnnualEventOccurenceDatesFrom: YearMonthDay today
through: YearMonthDay yesterday + 1 years
do: [:semanticKey :date :observanceType |
(SemanticDatePolicy usFederalHolidaySemanticKeys includes: semanticKey)
ifTrue: [list add: date->(Array with: semanticKey with: observanceType)]].
Transcript cr.
list do: [:assoc |
Transcript cr; show: (assoc key % #rfc2822); show: ':'; tab.
assoc value do: [:key |
key == #nominal
ifFalse:
[key == #observed
ifTrue:
[Transcript show: ' ('; show: key; show: ')']
ifFalse: [Transcript show: key]]]]
"Do it."
"All Full-Day Closures of the New York Stock Exchange
from 1 April 1969 to 30 September 1973:"
| list |
list := SortedCollection sortBlock: [:a :b | a key < b key].
SemanticDatePolicy nyse
observedEventOccurenceDatesFrom: (YearMonthDay year: 1969 month: 4 day: 1)
through: (YearMonthDay year: 1973 month: 9 day: 30)
do: [:semanticKey :date :observanceType |
semanticKey == #weekend
ifFalse:
[list add: date->(Array
with: semanticKey
with: observanceType)].
Transcript cr.
list do: [:assoc |
Transcript cr; show: (assoc key % #rfc2822); show: ':'; tab.
assoc value do: [:key |
key == #nominal
ifFalse:
[key == #observed
ifTrue:
[Transcript show: ' ('; show: key; show: ')']
ifFalse: [Transcript show: key]]]]]
"Do it:" "Selected Jewish Holidays this year and next year."
| year roshHoshannah yomKippur khanukkah purim pesach list printSpec |
year := HebrewCalendar clock thisYear.
roshHoshannah := DayOfMonth monthKey: #Tishri day: 1 calendar: #Hebrew.
yomKippur := DayOfMonth monthKey: #Tishri day: 10 calendar: #Hebrew.
khanukkah := DayOfMonth monthKey: #Kislev day: 25 calendar: #Hebrew.
purim := DayOfMonth monthKey: #Adar day: 14 calendar: #Hebrew.
pesach := DayOfMonth monthKey: #Nisan day: 15 calendar: #Hebrew.
list := OrderedCollection new.
printSpec :=
#(showDayOfWeekAbbreviation
useMonthName
dateSeparator: $
datePadChar: nil).
2 timesRepeat: [
| hDate |
hDate := roshHoshannah inYear: year.
list add: (Array
with: #'Rosh Hoshannah'
with: hDate % printSpec
with: hDate asGregorian % printSpec).
hDate := yomKippur inYear: year.
list add: (Array
with: #'Yom Kippur'
with: hDate % printSpec
with: hDate asGregorian % printSpec).
hDate := khanukkah inYear: year.
list add: (Array
with: #'Khanukkah'
with: hDate % printSpec
with: hDate asGregorian % printSpec).
hDate := purim inYear: year.
list add: (Array
with: #'Purim'
with: hDate % printSpec
with: hDate asGregorian % printSpec).
hDate := pesach inYear: year.
list add: (Array
with: #'Pesach'
with: hDate % printSpec
with: hDate asGregorian % printSpec).
year := year + 1].
Transcript cr.
list do:
[:array |
Transcript
cr; show: (array at: 1); show: ':';
cr; tab; show: (array at: 2);
cr; tab; show: (array at: 3)]
"Do it:" "Date of Easter for the next 10 years (including this year.)"
| year |
year := GregorianCalendar clock thisYear.
Transcript cr.
10 timesRepeat:
[Transcript
cr; show: (GregorianEaster canonical inYear: year) localePrintString.
year := year + 1].
"Print it:"
YearMonthDay today asTimeperiod
"Print it:"
DateAndTime now asTimeperiod
"Print it:"
Timeperiod today includes: GregorianCalendar clock now.
"Print it:"
(Timeperiod dayStartingAt: YearMonthDay today) = DateAndTime now
"Inspect it:"
(Timeperiod yearStartingAt: YearMonthDay today) monthPeriods
"Print it:"
Timeperiod today isAdjacentTo: YearMonthDay today tomorrow asTimeperiod.
"Print it:"
Timeperiod currentYear * (Timeperiod yearStartingAt: YearMonthDay today).
"Print it:"
(Timeperiod dayStartingAt: Gregorian clock now) + (CalendarDuration months: 6).
"Print it:"
| firstAndLastDays |
firstAndLastDays := OrderedCollection new.
(Timeperiod yearStartingAt: YearMonthDay today)
quartersDo: [:eachQuarter |
firstAndLastDays add: (Array with: eachQuarter first with: eachQuarter last)].
firstAndLastDays
"Print it:"
Timeperiod currentYear every: (CalendarDuration months: 1)
collect: [:aTimeperiod | aTimeperiod last]
"Print it:"
Timeperiod currentYear next every: (CalendarDuration months: 1)
collect: [:aTimeperiod | aTimeperiod last]
"Inspect it:"
(Timeperiod from: TimeOfDay now duration: (Duration hours: 1)) next: 24
"Inspect it:"
| yearStartingToday |
yearStartingToday:=
Timeperiod
from: DateAndTime nominalToday
duration: (CalendarDuration years: 1).
yearStartingToday
every: (CivilDuration years: 0 months: 0 days: 29 hours: 12 minutes: 0 seconds: 0)
collect: [:tp | tp localePrintString]
"Print it:"
(DateAndTime nominalToday yesterday to: DateAndTime nominalToday)
every: (Duration days: 1/9)
collect: [:each | each start timeOfDay]
|