Day 16: Time in Perl6

It’s the 0x10th day of Christmas, and it’s time for you to learn of time. The synopsis S32::Temporal has been heavily revised in the past year, and today’s post details some of the basics of time as it is implemented in Perl 6.

time and now

The two terms that give the current time (at least what your system thinks is the current time) are time; and now. Here’s a quick example:
> say time; say now;
1292460064
Instant:2010-12-16T00:41:4.873248Z

The first (obvious) difference is that time returns POSIX time, as an integer. now returns an object known as an Instant. Use now if  you want fractions of a second and recognition of leap seconds. time won’t give you fractions of a second or leap seconds, because it returns POSIX time. Which one you use all depends on what you need.

DateTime and friend

Most of the time, you will want to store dates other than now. For this, the DateTime object is what you need. If you want to store the current time, you can use:

my $moment = DateTime.new(now); # or DateTime.new(time)

Otherwise, there are two ways of creating a DateTime object:

my $dw = DateTime.new(:year(1963), :month(11), :day(23), :hour(17), :minute(15));

This is in UTC, if you want to enter it in in another timezone, use the :timezone adverb. Here, only :year is required, the rest defaults to midnight on January 1 of the year.

This way is also pretty tedious. You could instead create a DateTime object by inputting an ISO 8601 timestamp, as a string.

my $dw = DateTime.new("1963-11-23T17:15:00Z");

The Z denotes UTC. To change that, replace Z with +hhmm or -hhmm, where ‘hh’ is the number of hours offset and ‘mm’ the number of minutes.

There is also a Date object, which is created in a similar way, but without hours, minutes, or seconds. For example:

my $jfk = Date.new("1963-11-22"); # you can also use :year and so on

The introduction of the Date object is actually a lesson from CPAN’s DateTime module: sometimes you want to treat days without having to worry about things like time zones and leap seconds. It’s inherently easier to handle pure dates. For example Date has built-in .succ and .pred methods, so you can increment and decrement them in your code.

$jfk++; # Day after JFK

Finally…

That’s about it for Time in P6. To see all the gritty details go to the Temporal specification or ask about it in the community!

7 thoughts on “Day 16: Time in Perl6

  1. Instant:2010-12-16T00:41:4.873248Z
    I miss a Zero to have a two digitsecond.
    I miss succ and pred for Years/months/sec.
    I miss timezone names.
    I miss Operations on Intervalls like ‘1 month 2h5m’ in postgres.
    I can’t use the String Instant in html5 ‘input type=”date”‘.
    But i’m happy to see progress after so many years.

  2. The 16th day of December, maybe, but Christmas has only 12 days, the first of which is tomorrow. Maybe you could have used the Advent idea here?

  3. Oh, got it!

    > my $past = now
    Instant:2011-06-14T08:47:29.753213Z
    > now – $past
    22.9191123836793
    > now – $past
    24.2476572958501
    > now – $past
    25.3030592734226

  4. > say now
    Instant:1326582064.02309
    > say time
    1326582037
    > say now
    Instant:1326582080.96393

    This is perl6 version 2011.12-94-g9493f9b built on parrot 3.11.0 revision RELEASE_3_11_0-7-g5b125a1

    why I can’t got the correct output ?

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.