📘 Current date and time in Perl 6

Print current date and time as an epoch and in a human-readable format.

In Perl 6, the time function returns the current time as the Unix epoch:

say time;

The output is something like this: 1495785518.

For manipulating dates and times, use the built-in DateTime class:

say DateTime.now;

The date is now in a more human-readable format, although still is overloaded with many details: 2017-05-26T10:02:20.500209+02:00.

To access the separate elements of date and time, use the methods on the variable of the DateTime class:

my $dt = DateTime.now;

say $dt.day;   # 26
say $dt.month; # 5
say $dt.year;  # 2017
say $dt.hour;  # 10
say $dt.minute; # 9
say $dt.second; # 5.55802702903748

The meaning of all the elements is quite straightforward.

All the values except seconds are integer. For the seconds, you may want to take the integer part only:

say $dt.second.Int;

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s