Raku includes several classes that deal with temporal information: Date, DateTime, Instant and Duration. The three first are dateish, so they mix in the Dateish role, which defines all methods and properties that classes that deal with date should assume. It also includes a class hierarchy of exceptions rooted in X::Temporal.
We will try to illustrate these classes in the next (somewhat extended) example, which can be used to process all files in a directory (by default
.) with a particular extension (by default
.raku) in a directory, sort them according to their age, and compute how many files have been created per month and how many were modified in certain periods expressed in ranges of months:
sub MAIN( = ".", = "raku" )
DateTime is used in line 2 to contain the current date and time returned by
A CATCH phaser is declared in lines 7 to 11. Its main mission is to distinguish between
DateTime-related exceptions and other types. These kinds of exception can arise from invalid formats or timezone clashes. Barring some corruption of the file attributes, both are impossible, but in any case they should be caught and separated from other types of exceptions.
We use Instants in lines 12-13 to represent the moment in which the files where accessed and modified. An Instant is measured in atomic seconds and is a very low-level description of a time event; however, the Duration declared in line 14 represents the time transcurred among two different
Instants and we will be using it to represent the age.
For some variables, we might be interested in dealing with them with some dateish traits.
$time-of-day contains the time of the day the file was changed;
changed will return an Instant, but it is converted into a Date (which is
Instant is not) and then the time of day is extracted from that.
$time-of-day will have
creates a date Range and
$file-changed-date is smartmatched against it. Dates can be used this way; in this case it creates a
Range that excludes its last element.
This very variable is also used to compute the month of the year when the file was modified. Date is obviously
Dateish and then has the
month method to extract that property from it.
Duration objects can be compared. This is used in
to sort the files by age.