Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Date Conversion: yyddd to yyyymmdd

by roho (Monsignor)
on Jan 05, 2011 at 03:43 UTC ( #880505=perlquestion: print w/ replies, xml ) Need Help??
roho has asked for the wisdom of the Perl Monks concerning the following question:

I have super searched and googled to no avail, trying to find a module that converts a date in the form yyddd (or yyyyddd) to yyyymmdd (e.g, convert 11004 to 20110104). Does anyone know of a module that performs this conversion? I'm sure this wheel has already been invented somewhere. Thanks.

"Its not how hard you work, its how much you get done."

Comment on Date Conversion: yyddd to yyyymmdd
Re: Date Conversion: yyddd to yyyymmdd
by Anonymous Monk on Jan 05, 2011 at 03:53 UTC
    Does anyone know of a module that performs this conversion? I'm sure this wheel has already been invented somewhere. Thanks.

    Pick pretty much any perl module, DateTime, Date::Manip, Time::Piece, and there are way to parse and format the date any way you want. The easiest way is with strftime/strptime

Re: Date Conversion: yyddd to yyyymmdd
by GrandFather (Cardinal) on Jan 05, 2011 at 04:07 UTC

    There are plenty of date and time related modules, but in this case Date::EzDate may in fact be easy:

    my $date = Date::EzDate->new(); $date->{yeartwodigits} = 11; $date->{yeardaybase1} = 32; $date->{'myformat'} = '{year}{month number base 1}{day of month}'; print $date->{'myformat'};

    Make sure you read the docs about the 'year two digits' handling of the assumed century however!

    True laziness is hard work
Re: Date Conversion: yyddd to yyyymmdd
by jwkrahn (Monsignor) on Jan 05, 2011 at 07:44 UTC
    $ perl -le' use Time::Local qw/ timegm_nocheck /; my $in_date = q/11004/; my ( $in_year, $day_of_year ) = $in_date =~ /\A(\d\d|\d\d\d\d)(\d\d\d) +\z/; $in_year = ( ( 4 == length $in_year ? "" : "20" ) . $in_year ) - 1900; my ( $day, $month, $year ) = ( gmtime timegm_nocheck 0, 0, 0, $day_of_ +year, 0, $in_year )[ 3, 4, 5 ]; printf "%d %d %d\n", $year + 1900, $month + 1, $day; ' 2011 1 4
      Booo :) booo Doing your own century math is so 2001 1993 :) and oh look, Time::Piece->strptime seems to be broken

        “I was dreamin’ when I wrote this, so sue me if I code too fast ...”

        – (with apologies to...) Prince; 1999.

Re: Date Conversion: yyddd to yyyymmdd
by Ratazong (Prior) on Jan 05, 2011 at 08:48 UTC
    As other monks have shown above, there is more than one way to do it. I like Date::Calc (especially for its good documentation and examples!), so here is a way on how to do the conversion using that module:
    use Date::Calc qw(Add_Delta_Days); my $input = "11004"; # yyddd $input =~ /(\d\d)(\d\d\d)/; my ($y, $m, $d) = Add_Delta_Days("20$1",1,1, $2 -1); # based on an ex +ample in the Date::Calc-docu my $output = sprintf("%4d%02d%02d", $y, $m, $d);
    HTH, Rata
Re: Date Conversion: yyddd to yyyymmdd
by ambrus (Abbot) on Jan 05, 2011 at 13:12 UTC

    The Date::Manip module accepts that format of dates by default (that is, it parses it fine without having to tell it the format), so let's use that.

    $ perl -we 'use Date::Manip; print UnixDate($ARGV[0], "%Q\n")' 11004 20110104 $ perl -we 'use Date::Manip::Date 6.0; print Date::Manip::Date->new($A +RGV[0])->printf("%Q\n")' 11004 20110104 $
Re: Date Conversion: yyddd to yyyymmdd
by Cristoforo (Deacon) on Jan 05, 2011 at 16:37 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://880505]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-07-29 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls