Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Adding Days To YYYYMMDD Date Format

by Kenosis (Priest)
on May 18, 2012 at 22:51 UTC ( [id://971371]=note: print w/replies, xml ) Need Help??


in reply to Re: Adding Days To YYYYMMDD Date Format
in thread Adding Days To YYYYMMDD Date Format

This is elegant and very readable, tobyink! Nice work...

  • Comment on Re^2: Adding Days To YYYYMMDD Date Format

Replies are listed 'Best First'.
Re^3: Adding Days To YYYYMMDD Date Format
by tobyink (Canon) on May 18, 2012 at 23:01 UTC

    I meant to add... I've uploaded a new version of DateTimeX::Auto so now it's possible to just do:

    use DateTimeX::Auto qw(:auto); my $future = '2012-05-19' + 'P3Y4M2D'; # says '2015-09-21' say $future;

    The duration format isn't the most readable, but it's an ISO standard (ISO 8601 duration), so fairly well documented if you Google for it.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      That *::Auto has more magic than *::Easy! And, MooseX::Types::DateTimeX lists the caveat about *::Easy that it has too much DWIMery.
      I have a similar issue. Is there a way to construct a for loop that will generate dates in format YYYMMDD starting on the first business date of the year and skip Saturdays and Sundays?

      Example, 20120103 20120104 20120105 20120106 20120109 20120110 20120111 20120112 etc.

        Sounds like a job for Date::Manip, which is kind of the "kitchen sink" of date modules.

        Date::Manip::Date::is_business_day would probably do the trick. It also has a 'holiday' method which could let you skip holidays as well, if you wanted to.

        Otherwise, search cpan for other possibilities.

        use 5.010; use strict; use warnings FATAL => qw(all); use DateTimeX::Auto qw(:auto); { # slightly evil, but works... package DateTime; use overload '~~', sub { $_[0]->ymd eq $_[1]->ymd } } my $not_business_days = [ sub { $_[0]->day_abbr =~ /^S/ }, # weekends qw( 2012-01-02 2012-04-06 2012-04-09 2012-05-07 2012-06-04 2012-06-05 2012-08-27 2012-12-25 2012-12-26 ), ]; my $date = '2012-01-01'; my $end = '2013-01-01'; while ($date < $end) { say $date->ymd("") unless $date ~~ $not_business_days; $date->add(days => 1); }
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found