Booo :) booo
#!/usr/bin/perl -- use strict; use warnings; use Time::Piece; use DateTime::Format::Strptime; Main(@ARGV); exit(0); sub Main { #~ yyddd (or yyyyddd) to yyyymmdd (e.g, convert 11004 to 20110104) +. Yo( 11004 => 20110104 ); Yo( 2011004 => 20110104 ); Yo( 11005 => 20110105 ); } ## end sub Main BEGIN { #~ http://www.unix.com/man-page/FreeBSD/3/strftime/ my @Y = ( [ yyddd => sub { my ($in) = @_; if ( $in =~ /^\d{5}$/ ) { #~ $in = Time::Piece->strptime( $in, '%y%j' ); $in = DateTime::Format::Strptime->new( pattern => '%y% +j' ) ->parse_datetime($in)->epoch; $in = gmtime($in)->strftime('%Y-%m-%d'); return $in; } ## end if ( $in =~ /^\d{5}$/ ) } ], [ yyyyddd => sub { my ($in) = @_; if ( $in =~ /^\d{7}$/ ) { #~ $in = Time::Piece->strptime( $in, '%Y%j' ); $in = DateTime::Format::Strptime->new( pattern => '%Y% +j' ) ->parse_datetime($in)->epoch; $in = gmtime($in)->strftime('%Y-%m-%d'); return $in; } ## end if ( $in =~ /^\d{7}$/ ) } ], ); sub Y { for my $Y (@Y) { if ( my $p = $Y->[1]->(@_) ) { return $p; } } } ## end sub Y } ## end BEGIN sub Yo { my ( $in, $ou ) = @_; print "$in\n->$ou\n->"; print Y($in), "\n\n"; } __END__ 11004 ->20110104 ->2011-01-04 2011004 ->20110104 ->2011-01-04 11005 ->20110105 ->2011-01-05
Doing your own century math is so 2001 1993 :) and oh look, Time::Piece->strptime seems to be broken

In reply to Re^2: Date Conversion: yyddd to yyyymmdd by Anonymous Monk
in thread Date Conversion: yyddd to yyyymmdd by roho

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.