Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Date::Manip ParseRecur help needed

by johngg (Canon)
on Jan 10, 2020 at 14:21 UTC ( [id://11111290]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11111290]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found