Short Executable Examples ("Do Its") of Chronos Usage
Examples based on version B1.188 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:"
Timepoint now.
"Note: Timepoint may also be referenced by the alias Timepoint,
provided that name isn't already owned by a class from some other class
library"
"Print it:"
Timepoint today.
"Print it:"
YearMonthDay today.
"Print it:"
YearMonthDay today - (Timepoint year: 1969 month: 7 day: 20)
"Print it:"
Timepoint now
- (Timepoint
year: 1969
month: 7
day: 20
hour: 5
minute: 17
second: 40
offset: (ScientificDuration hours: -5))
"Note: ScientifcDuration may also be referenced by the alias
'Duration', provided that name isn't already owned by a class in some
other class library."
"Print it:"
Timepoint now hoursSince:
(Timepoint
utYear: 1969
month: 7
day: 20
hour: 10
minute: 17
timeZone: (ScientificDuration hours: -5))
"Print it:"
(Timepoint now
civilDurationSince:
(Timepoint
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:"
(Timepoint nowIn: 'Pacific/Auckland') printStringUsing: #verbose.
"Print it:"
(Timepoint nowIn: 'America/Detroit') printStringUsing: #usShort.
"Print it:"
| pointInTime | "Parameters specified in Universal Time"
pointInTime := Timepoint 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 := Timepoint 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 now showInCommonWorldTimezones
"When converted to other time zones, point-in-time values that are
invariant to Universal Time preserve the invariant that they represent the
same moment in Universal Time."
"Do it:"
Timepoint nominalNow showInCommonWorldTimezones
"When converted to other time zones, point-in-time values that are
invariant to nominal time preserve the invariant that they represent the same
moment in local time (nominal-time-invariant time ponts represent the same
local time in any and all time zones.)"
"Print it:" "Expected Result: 2003-12-29T00:00:00 (in your local time zone)"
Timepoint isoYear: 2004 week: 1 dayOfWeek: 1
"This is the (somewhat strange) ISO Year-Week-DayOfWeek format
(internet search terms: ISO week date)"
"Print it:" "Expected result: #(2004 1 1)"
(Timepoint year: 2003 month: 12 day: 29)
isoYearWeekDayOfWeekDo:
[:isoYear :isoWeek :isoDayOfWeek |
Array
with: isoYear
with: isoWeek
with: isoDayOfWeek]
"Print it:"
YearMonthDay today daysSinceEpoch
"The epoch of the proleptic Gregorian calendar is January 1, 0001,
which is day zero of the Chronos count of days for the Gregorian calendar."
"Print it:"
(YearMonthDay daysSinceEpoch: 0) localePrintString
"Print it:" "Fractional date/time values"
Timepoint now fractionalDaysSinceEpoch
"Print it:"
"Fractional date/time values.
Expected result (for the en-US locale):
January 1, 1 6:00:00 pm LMT"
(Timepoint daysSinceEpoch: 0.75) localePrintString
"`LMT' stands for `Local Mean Time'--time zones didn't exist prior to the 1800's"
"Print it:" "Fractional date/time values; print policy options"
(Timepoint utDaysSinceEpoch: Timepoint now utFractionalDaysSinceEpoch)
printStringUsing:
(ChronosPrintPolicy newConfigurable
subSecondTimePrecision: 3;
hideTimeZone)
"Print it:" "Julian Day (integer)--local time."
Timepoint now julianDay
"Print it:" "Julian Day and day fraction--local time."
Timepoint now julianDate
"Print it:" "Julian Day and day fraction--Universal Time."
Timepoint now utJulianDate
"Print it:"
Timepoint utJulianDay: 2440422.928935185s timeZone: 'America/Chicago'.
"Note: Julian Day and Day Fraction in Universal Time;
new 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;
new Timepoint bound to Chicago local time."
"Print it:" "Date today according to the Hebrew Calendar, printed in RFC 2822 format"
(YearMonthDay todayAs: #Hebrew) printStringUsing: #rfc2822.
"Print it:" "Timepoint 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
"Get the initial moment of today according to the tabular Islamic calendar,
convert it to the Persian and Gregorian calendars.
Note that the Islamic day starts at 6pm."
"Do it:"
Timepoint now showInAllCalendars
"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 % #universal].
"The binary selector #% is equivalent to #printStringUsing:"
"Inspect it:"
Calendar allRegisteredKeys asArray collect:
[:calendarKey | Timepoint 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 :=
Timepoint
year: 1981
month: 7
day: 15
hour: 16
minute: 33
second: 0.
Transcript cr.
yourBirthday inAllCalendars do: [:each |
Transcript cr; show: each % #universal].
"Do it:"
| appollo11 |
appollo11 :=
Timepoint
utYear: 1969 month: 7 day: 20 hour: 10 minute: 17 second: 40
timeZone: 'America/Chicago'.
Transcript cr.
appollo11 inAllCalendars do: [:each |
Transcript cr; show: (each printStringUsing: #universal)].
"Inspect it:"
Timepoint nowInAllTimeZonesAndCalendars
"Inspect it:"
(Timepoint
year: 1969 month: 7 day: 20 hour: 10 minute: 17 second: 40
timeZone: Timezone universal) inAllTimeZonesAndCalendars
"Do it:"
"Nominal Dates for all `holidays` recognized in the United States
during the current year--although many are actually business days."
| 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);
show: ' (starting sundown of the day before)']
"Do it:" "Date of Easter for the next 10 years (including this year.)"
| thisYear |
thisYear := GregorianCalendar clock thisYear.
Transcript cr.
thisYear to: thisYear + 9 do: [:year |
Transcript
cr; show: (GregorianEaster canonical inYear: year) localePrintString].
"Print it:"
YearMonthDay today nextMonth
"Print it:"
YearMonthDay today nextDayOfWeek: Sunday
"Print it:"
YearMonthDay today withDayOfYear: 1
"Print it:"
YearMonthDay today withDayOfMonth: 1
"Print it:"
YearMonthDay today withMonth: 7
"Do it:"
Transcript cr.
(YearMonthDay readFromString: 'July 20, 1969')
to: YearMonthDay today
every: (CalendarDuration years: 10)
do: [:date | Transcript cr; show: date localePrintString]
"Do it:"
Transcript cr.
Timepoint today
to: Timepoint tomorrow
every: 3.5
hoursDo: [:time | Transcript cr; show: time % #rfc2822]
"Print it:"
YearMonthDay today asTimeperiod
"Print it:"
Timepoint now asTimeperiod
"Print it:"
Timepoint now withDuration: (ScientificDuration seconds: 60)
"In VW and Dolphin, `60 seconds' will also work"
"Print it:"
Timepoint now / (CivilDuration minutes: 1)
"In VW and Dolphin, `1 minutes' will also work"
"Print it:"
Timeperiod today includes: Timepoint now.
"Print it:"
(Timeperiod dayStartingAt: YearMonthDay today) = Timepoint now
"Inspect it:"
(Timeperiod yearStartingAt: YearMonthDay today) monthPeriods
"Print it:"
Timeperiod today isAdjacentTo: YearMonthDay today tomorrow.
"Print it:"
Timeperiod currentYear * (Timeperiod yearStartingAt: YearMonthDay today).
"Print it:"
(Timeperiod dayStartingAt: (Timepoint todayIn: 'Asia/Calcutta'))
* (Timeperiod dayStartingAt: (Timepoint todayIn: 'America/Los_Angeles')).
"Print it:"
Timeperiod currentMonth
* (Timeperiod monthStartingAt: YearMonthDay today).
"Print it:"
Timeperiod currentMonth
* (Timeperiod monthStartingAt: (YearMonthDay today addingMonths: 1)).
"Print it:"
Timeperiod currentMonth
* (Timeperiod
from: YearMonthDay today
duration: (CivilDuration months: -1)).
"Print it:"
Timeperiod currentMonth
* (Timeperiod
from: (YearMonthDay today subtractingMonths: 1)
duration: (CivilDuration months: -1)).
"Print it:"
(Timeperiod
from: YearMonthDay today
duration: (CivilDuration months: -1)) * Timeperiod currentMonth.
"Print it:"
(Timeperiod
from: (YearMonthDay today subtractingMonths: 1)
duration: (CivilDuration months: -1)) * Timeperiod currentMonth.
"Print it:"
Timeperiod currentMonth
* (Timeperiod monthStartingAt: (YearMonthDay today subtractingMonths: 1)).
"Print it:"
Timeperiod currentMonth
* (YearMonthDay atLastDayOfMonth @ (TimeOfDay hour: 12 minute: 0 second: 0)).
"Print it:"
(Timeperiod dayStartingAt: Timepoint now) + (CalendarDuration months: 6).
"Print it:"
| firstAndLastDays |
firstAndLastDays := OrderedCollection new.
(Timeperiod yearStartingAt: YearMonthDay today)
quartersDo: [:eachQuarter |
firstAndLastDays add:
(Array
with: #firstDayOfQuarter->eachQuarter first
with: #lastDayOfQuarter->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: (ScientificDuration hours: 1)) next: 24
"Print it:"
YearMonthDay today prev: 7.
"Inspect it:"
| yearStartingToday |
yearStartingToday:=
Timeperiod
from: Timepoint 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:"
(Timepoint nominalToday yesterday to: Timepoint nominalToday)
every: (ScientificDuration days: 1/9)
collect: [:each | each start timeOfDay]
|