Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Coolest way to decode YYYYMMDD?

by sundialsvc4 (Monsignor)
on Jun 03, 2013 at 03:07 UTC ( #1036648=perlquestion: print w/ replies, xml ) Need Help??
sundialsvc4 has asked for the wisdom of the Perl Monks concerning the following question:

So, what’s the coolest voodoo to convert '19830501' into the DateTime value, “May 1,1983?”   (Hurry up and salivate!   Mega-XP™ awaits!!)   ;-)

Comment on Coolest way to decode YYYYMMDD?
Re: Coolest way to decode YYYYMMDD?
by jdporter (Canon) on Jun 03, 2013 at 03:56 UTC

    Not sure what you consider cool. Here's my balance between terse and obvious:

    /(....)(..)(..)/; $dt = DateTime->new( year => $1, month => $2, day => $3 );

    I suppose one could try to get a little clever by doing something like

    my %th; @th{qw( year month day )} = /(....)(..)(..)/; $dt = DateTime->new( %th );

    I don't know if that's any "cooler", though.

Re: Coolest way to decode YYYYMMDD?
by moritz (Cardinal) on Jun 03, 2013 at 04:59 UTC
Re: Coolest way to decode YYYYMMDD?
by smls (Friar) on Jun 03, 2013 at 06:14 UTC
    use DateTime; use List::MoreUtils qw(mesh); my $string = "19830501"; my $date = DateTime->new( mesh @{['year', 'month', 'day']}, @{[unpack "A4A2A2", $string]} );
    Is that "cool" enough? :)

      You can disable a prototype rather than bending over backwards to comply to it.

      use DateTime qw( ); use List::MoreUtils qw( mesh ); my $string = "19830501"; my $date = DateTime->new(&mesh( [qw( year month day )] => [ unpack "A4A2A2", $string ] ));
Re: Coolest way to decode YYYYMMDD?
by smls (Friar) on Jun 03, 2013 at 06:33 UTC
    For the record, I believe the "official" way to do it is:
    use DateTime::Format::Strptime; my $string = "19830501"; my $format = DateTime::Format::Strptime->new( pattern => '%Y%m%d' ); my $date = $format->parse_datetime( $string );
Re: Coolest way to decode YYYYMMDD?
by erix (Vicar) on Jun 03, 2013 at 06:53 UTC

    Why all the contortions? Keep it simple and talk to your database:

    $ echo "select to_char(to_date('19830501?','YYYYMMDD'), 'FMMon FMdd, y +yyy?')" \ | perl -MDBI -ne ' print DBI->connect("dbi:Pg:")->selectall_arrayref($_)->[0]->[0], + "\n"; '; May 1, 1983?

    ;-)

    P.S. (Looks I am the first one to spot the terminal question mark in your example? Does that get me Mega-XP too?)

      The question mark is ambiguous in this context but it's proper English style see below. Liked your direct approach. :P

        it's proper English style.

        Actually... No, it's not. While there is some (little) conflict over when/whether periods & commas should go inside, all authorities agree that "question marks and exclamation points follow [i.e. go outside] closing quotation marks unless they belong within the quoted matter." (Chicago Manual)

        I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

      If DateTime is not a requirement, and short month names are ok (difficult to say with "May" as the only example), then I offer this:

      printf('"%s %d,%d?"',substr(gmtime(34e6*('19830501'=~/(....)(..)(..)/) +[1]),4,3),$3,$1);
Re: Coolest way to decode YYYYMMDD?
by hdb (Parson) on Jun 03, 2013 at 08:30 UTC

    ++ to all the good technical solutions provided so far. I am surprised that no one has really spotted the underlying philosophical problems implicit in the question. First of all, why using a DateTime object, when only a date has been specified? It seems obvious that at least a time has to be added to the date information before the question can be answered. Completeness of question is one of the highest principles (see for example the works of Douglas Adams). Which time shall we add? Noon, midnight, else? Can we be sure which one is "the best time of the day"™? Time creates other problems, of course, as Time is subject to relativity. Now Einstein has a lot of things to say about this matter, but he had no great tools such as Perl, CPAN and other unnamed utilities at hand. Having clarified the greater issues of the question, let's look at the details: the string '19830501' and the requested answer “May 1,1983?” suggests a pattern of yyyymmdd to be translated into month name, day of month, comma, four digit year, question mark. Splitting up the input first. A recursive solution seems to be the best approach as we first need to split the string into two halves of equal length, and the apply the same to the second half that we have obtained. Recursion is a sound principle applied to all important problems such as Towers of Hanoi™ and others.
    While the OP explicitly mentions DateTime to be employed solving his riddle, it seems rather uncool to use the most obvious module available. There are so many modules available on CPAN, so just using only one and the most obvious cannot be a good strategy. Finding the name of the 5th month, for example, is far better done asking "Google"™ Employing useful tools like LWP::UserAgent and HTML::Parser will easily translate "name of 5th month in gregorian calendar" into "May".
    One thing is still puzzling, now that we have solved most of the question: why is the input surrounded by single quotes while the required answer has double quotes? Perl is reacting rather different to the types of quotes. The interpolation feature of double quotes allows for writing complicated strings in a short concise way whereas single quotes help the programmer to ignore issues with characters like $, %, and @. By the way, these characters are also called sigils in Perl, stemming from Latin sigillum. The significance of these characters in Perl cannot be underestimated. In any case, the unresolved question of the quotes prevents to provide any usable code here, but I assume that the above exposition will be helpful for you to find the coolest solution yourself.

      “Unabashed Obfuscation™,” to that level of pure-artistry, definitely calls for an up-vote.   :-)   Touché.   When you get elected, remember us.

Re: Coolest way to decode YYYYMMDD?
by ww (Bishop) on Jun 03, 2013 at 19:36 UTC
    Which would be coolest?

    A solution in:

    • Assembler
    • Fortran
    • Cobol
    • Mumps...?

    If you didn't program your executable by toggling in binary, it wasn't really programming!

      The coolest way would be to not decode it at all.

      The great benefit of that format is that for 90% of uses -- sorting; comparing; matching; inclusion & exclusion -- you don't have to waste bazillions of cycles constructing O'WoE, heavyweight objects that lie, you just use them as strings and it all works out.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Coolest way to decode YYYYMMDD?
by gurpreetsingh13 (Scribe) on Jun 05, 2013 at 07:04 UTC
    If you are on unix or something similar, simply without using DateTime object - use this one liner
    perl -e 'print `date --date="19830501" +\%b\\ \%d\\,\%Y`;'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-07-23 09:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (138 votes), past polls