Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date object "forgets" formatter?

Tags:

raku

Is there an error in the handling of formatter objects when using Perl6's Date? If I manipulate the dates after instantiating the object, it seems as if the formatter returns to the default.

my $d = Date.new(2019, 12, 25, formatter => { sprintf "%02d/%02d in %04d", .month, .day, .year });
# Outputs 12/25 in 2019

$d.later(:1day)
# Outputs 2019-12-26

I expected the output after invoking the .later method to be "12/26 in 2019".

When looking at Date.pm6 over on GitHub -- https://github.com/rakudo/rakudo/blob/master/src/core/Date.pm6 -- I see that the .later method creates new Date objects using .new without referencing to the formatter that's set. I.e. should line 151

self.new-from-daycount(self.daycount + $multiplier * $amount )

rather be something like

self.new-from-daycount(self.daycount + $multiplier * $amount, &!formatter )

?

If so this is missing in lots of places, not only in .later, but .succ, .pred etc.

like image 552
Jo Christian Oterhals Avatar asked Jul 30 '19 11:07

Jo Christian Oterhals


2 Answers

Looks like this was an oversight in the dispatch to new-from-daycount, which got fixed with: https://github.com/rakudo/rakudo/commit/ac11774d73 .

I think this should fix all issues. Please do make an issue at https://github.com/rakudo/rakudo/issues/new if not.

like image 113
Elizabeth Mattijsen Avatar answered Sep 21 '22 22:09

Elizabeth Mattijsen


@elizabeth - this fixed the issue when adding days, but not other intervals. I guess the same fix has to be done on multiple locations.

This fixed the problem for iterating days, but not for months, years, etc.

> my $d = Date.new(2019,12,24, formatter => { sprintf "%04d/%02d", .year, .month };
2019/12

> $d.later(:1day);
2019/12

...but...

> $d.later(:1month);  
2020-01-24
like image 25
Jo Christian Oterhals Avatar answered Sep 17 '22 22:09

Jo Christian Oterhals