http://qs321.pair.com?node_id=11111290


in reply to Date::Manip ParseRecur help needed

A solution using the, core since 5.10, Time::Piece module and its associated Time::Seconds for date arithmetic. I accept the strftime() default format for brevity but you could craft the output any way you like.

johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -MTime::Piece -MT +ime::Seconds -E ' my $ffJan = Time::Piece->strptime( q{2020/01/01}, q{%Y/%m/%d} ); $ffJan += ONE_DAY for 1 .. 5 - $ffJan->day_of_week(); my $end = Time::Piece->strptime( q{2020/06/01}, q{%Y/%m/%d} ); while ( $ffJan->epoch() < $end->epoch() ) { say $ffJan->strftime(); $ffJan += ONE_WEEK; }' Fri, 03 Jan 2020 00:00:00 UTC Fri, 10 Jan 2020 00:00:00 UTC Fri, 17 Jan 2020 00:00:00 UTC Fri, 24 Jan 2020 00:00:00 UTC Fri, 31 Jan 2020 00:00:00 UTC Fri, 07 Feb 2020 00:00:00 UTC Fri, 14 Feb 2020 00:00:00 UTC Fri, 21 Feb 2020 00:00:00 UTC Fri, 28 Feb 2020 00:00:00 UTC Fri, 06 Mar 2020 00:00:00 UTC Fri, 13 Mar 2020 00:00:00 UTC Fri, 20 Mar 2020 00:00:00 UTC Fri, 27 Mar 2020 00:00:00 UTC Fri, 03 Apr 2020 00:00:00 UTC Fri, 10 Apr 2020 00:00:00 UTC Fri, 17 Apr 2020 00:00:00 UTC Fri, 24 Apr 2020 00:00:00 UTC Fri, 01 May 2020 00:00:00 UTC Fri, 08 May 2020 00:00:00 UTC Fri, 15 May 2020 00:00:00 UTC Fri, 22 May 2020 00:00:00 UTC Fri, 29 May 2020 00:00:00 UTC

I hope this is helpful.

Update: It makes more sense to calculate the cut-off epoch once at the start rather than every loop.

johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -MTime::Piece -MT +ime::Seconds -E ' my $endEpoch = Time::Piece->strptime( q{2020/06/01}, q{%Y/%m/%d} )->ep +och(); my $ffJan = Time::Piece->strptime( q{2020/01/01}, q{%Y/%m/%d} ); $ffJan += ONE_DAY for 1 .. 5 - $ffJan->day_of_week(); while ( $ffJan->epoch() < $endEpoch ) { say $ffJan->strftime(); $ffJan += ONE_WEEK; }' Fri, 03 Jan 2020 00:00:00 UTC Fri, 10 Jan 2020 00:00:00 UTC Fri, 17 Jan 2020 00:00:00 UTC Fri, 24 Jan 2020 00:00:00 UTC Fri, 31 Jan 2020 00:00:00 UTC Fri, 07 Feb 2020 00:00:00 UTC Fri, 14 Feb 2020 00:00:00 UTC Fri, 21 Feb 2020 00:00:00 UTC Fri, 28 Feb 2020 00:00:00 UTC Fri, 06 Mar 2020 00:00:00 UTC Fri, 13 Mar 2020 00:00:00 UTC Fri, 20 Mar 2020 00:00:00 UTC Fri, 27 Mar 2020 00:00:00 UTC Fri, 03 Apr 2020 00:00:00 UTC Fri, 10 Apr 2020 00:00:00 UTC Fri, 17 Apr 2020 00:00:00 UTC Fri, 24 Apr 2020 00:00:00 UTC Fri, 01 May 2020 00:00:00 UTC Fri, 08 May 2020 00:00:00 UTC Fri, 15 May 2020 00:00:00 UTC Fri, 22 May 2020 00:00:00 UTC Fri, 29 May 2020 00:00:00 UTC

Cheers,

JohnGG