Re: Coolest way to decode YYYYMMDD?
by jdporter (Paladin) on Jun 03, 2013 at 03:56 UTC
|
/(....)(..)(..)/;
$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.
| [reply] [d/l] [select] |
Re: Coolest way to decode YYYYMMDD?
by erix (Prior) on Jun 03, 2013 at 06:53 UTC
|
$ 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?)
| [reply] [d/l] |
|
|
printf('"%s %d,%d?"',substr(gmtime(34e6*('19830501'=~/(....)(..)(..)/)
+[1]),4,3),$3,$1);
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
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 .
| [reply] |
|
|
|
|
Re: Coolest way to decode YYYYMMDD?
by moritz (Cardinal) on Jun 03, 2013 at 04:59 UTC
|
$ perl -MDateTime::Format::DateParse -wE 'say DateTime::Format::DateP
+arse->parse_datetime("19830501")'
1983-05-01T00:00:00
Does that qualify as cool?
| [reply] [d/l] |
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? :) | [reply] [d/l] |
|
|
use DateTime qw( );
use List::MoreUtils qw( mesh );
my $string = "19830501";
my $date = DateTime->new(&mesh(
[qw( year month day )] => [ unpack "A4A2A2", $string ] ));
| [reply] [d/l] |
Re: Coolest way to decode YYYYMMDD?
by hdb (Monsignor) 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.
| [reply] [d/l] [select] |
|
|
“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 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 );
| [reply] [d/l] |
Re: Coolest way to decode YYYYMMDD?
by ww (Archbishop) on Jun 03, 2013 at 19:36 UTC
|
| [reply] |
|
|
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.
| [reply] |
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`;'
| [reply] [d/l] |