Day 16: Time in Perl6

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;

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 =; # or

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

my $dw =, :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 ="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 ="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


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!

6 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. Oh, got it!

    > my $past = now
    > now – $past
    > now – $past
    > now – $past

  3. > say now
    > say time
    > say now

    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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s