📜 ⬆️ ⬇️

Work with date and time in Perl6

time and now


The two functions that return the current time (at least the value that your system takes as the current one) are time and now. Example:

> say time; say now; 1292460064 Instant:2010-12-16T00:41:4.873248Z 


The first obvious difference is that time returns POSIX in integer form. now returns an object called Instant. If you need fractions of seconds and recognition of seconds of time coordination, use now.
')

Datetime and his friends


Most of the time you need to store dates that are different from now. The DateTime object is used for this. To save the current time:

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


There are two more ways to create an object:

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


This is in UTC. If you need another time zone, use the addition: timezone. In our case, only: year is required, the rest will be by default 1 January.

This is a rather complicated way. You can create a DateTime object through the ISO 8601 timestamp, as a string:

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


Z means UTC. You can replace Z with + hhmm or -hhmm, where 'hh' is the offset in hours, and 'mm' is in minutes.

There is also a Date object, which is created in the same way, but without hours, minutes and seconds:

 my $jfk = Date.new("1963-11-22"); #      :year    


Sometimes you need to work with days without worrying about time zones and seconds of correction. It is much easier to work with clean dates. For example, Date has built-in methods .succ and .pred, so you can increment and decrement them as you like:

 $jfk++; #   JFK 

Source: https://habr.com/ru/post/256893/


All Articles