See Original text in context
multi method later(DateTime: *)
Returns an object based on the current one (belonging to any class that mixes this role in), but with a time delta applied. The time delta can be passed as a named argument where the argument name is the unit.
Unless the given unit is second
or seconds
, the given value will be converted to an Int.
Allowed units are second
, seconds
, minute
, minutes
, hour
, hours
, day
, days
, week
, weeks
, month
, months
, year
, years
. Please note that the plural forms can only be used with the later
and earlier
methods.
The :2nd
form of colonpairs can be used as a compact and self-documenting way of specifying the delta:
say DateTime.new('2015-12-24T12:23:00Z').later(:2years);# OUTPUT: «2017-12-24T12:23:00Z»
Since addition of several different time units is not commutative, only one unit may be passed (and the first multi will be used).
my = DateTime.new(date => Date.new('2015-02-27'));say .later(month => 1).later(:2days); # OUTPUT: «2015-03-29T00:00:00Z»say .later(days => 2).later(:1month); # OUTPUT: «2015-04-01T00:00:00Z»say .later(days => 2).later(:month); # same, as +True === 1
You can also (since release 2021.02 of the Rakudo compiler) pass several units at the same time, but you will have to join them in a List
to activate the second form:
say DateTime.new(date => Date.new('2015-02-27')).later( (:1month, :2days) )# OUTPUT: «2015-03-29T00:00:00Z»
If the resultant time has value 60
for seconds, yet no leap second actually exists for that time, seconds will be set to 59
:
say DateTime.new('2008-12-31T23:59:60Z').later: :1day;# OUTPUT: «2009-01-01T23:59:59Z»
Negative offsets are allowed, though earlier is more idiomatic for that.
Objects of type Date will behave in the same way:
my = Date.new('2015-02-27');say .later(month => 1).later(:2days); # OUTPUT: «2015-03-29»say .later(days => 2).later(:1month); # OUTPUT: «2015-04-01»say .later(days => 2).later(:month); # same, as +True === 1