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

Re: Formatting STDIN for date format

by boftx (Chaplain)
on Sep 19, 2013 at 06:09 UTC ( #1054774=note: print w/ replies, xml ) Need Help??


in reply to Formatting STDIN for date format

Here is what I use to handle the most common date formats (assume the usual use strict; and use warnings; as well as use Carp; and use DateTime; are present):

# Utility function to convert a date string to a DT object sub _str2dt { # string that is supposed to be yyyy-mm-dd or mm/dd/yyyy # single digit months/days are allowed with this. my $date = shift; # convert date param to DT object my ($y,$m,$d); if ( $date =~ m/(^\d{4})\-(\d{1,2})\-(\d{1,2})($|\D+)/ ) { $y = $1, $m = $2, $d = $3; } elsif ( $date =~ m/^(\d{1,2})\/(\d{1,2})\/(\d{4})($|\D+)/ ) { $y = $3, $m = $1, $d = $2; } else { croak "Unable to parse date string: $date"; } eval { $date = DateTime->new( year => $y, month => $m, day => $d ) +; } or croak "Invalid date: $date"; return $date; }


Comment on Re: Formatting STDIN for date format
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-08-28 01:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (254 votes), past polls