Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to convert date like: 19 Aug 12:00 PM EDT to a perl structure

by ichudov (Initiate)
on Aug 16, 2015 at 14:07 UTC ( [id://1138746]=perlquestion: print w/replies, xml ) Need Help??

ichudov has asked for the wisdom of the Perl Monks concerning the following question:

I am parsing a particular website. This website has dates in the following format: 19 Aug 12:00 PM EDT. I want to read those dates and convert them to MySQL compatible date. I normally use DateTime::Precise but this format seems to be really odd. DateTime::Precise does not seem to be able to handle it. Would anyone have suggestions on what module to use. Thanks
  • Comment on How to convert date like: 19 Aug 12:00 PM EDT to a perl structure

Replies are listed 'Best First'.
Re: How to convert date like: 19 Aug 12:00 PM EDT to a perl structure
by pme (Monsignor) on Aug 16, 2015 at 14:41 UTC
Re: How to convert date like: 19 Aug 12:00 PM EDT to a perl structure
by soonix (Canon) on Aug 16, 2015 at 14:59 UTC
    Witnout a year this will be difficult. However, this looks like the output of ls, and HTTP::Date claims to handle this - see parse_date.
Re: How to convert date like: 19 Aug 12:00 PM EDT to a perl structure
by anonymized user 468275 (Curate) on Aug 16, 2015 at 17:00 UTC
    Although Perl offers the widest range of date conversion facilities - we are only scratching the surface so far, I feel it is only responsible to add that MySQL is also well-stacked with date-conversion functions. So whether to use Perl functions or MySQL functions for this depends more on the OTHER functionality you are dealing with (e.g. are stored procedures involved -- particularly if they have error-handling that if activated might obviate the need to convert in some cases -- and what other functionality is already implemented in Perl versus MySQL whose development should be encapsulated in just one place or the other?). So apart from the excellent suggestions by other posters, based on the limited information so far, it remains possible that you might want to include such MySQL functions as STR_TO_DATE in your list of contending solutions before making your choice of where/how to do this.

    Update: reworded to make post more concise.

    One world, one people

Re: How to convert date like: 19 Aug 12:00 PM EDT to a perl structure
by shmem (Chancellor) on Aug 16, 2015 at 20:25 UTC

    My timezone is CEST.

    qwurx [shmem] ~> perl -MDate::Parse -le 'print str2time pop' '19 Aug 1 +2:00 PM EDT' 1440000000 qwurx [shmem] ~> perl -MDate::Parse -le 'print scalar localtime str2ti +me pop' '19 Aug 12:00 PM EDT' Wed Aug 19 18:00:00 2015

    Structure:

    qwurx [shmem] ~> perl -MData::Dumper -MDate::Parse -le '@hash{qw(sec m +in hour mday mon year wday yday isdst)} = localtime str2time pop;prin +t Data::Dumper->Dump([\%hash],[qw(timehash)])' '19 Aug 12:00 PM EDT' $timehash = { 'isdst' => 1, 'min' => 0, 'yday' => 230, 'mon' => 7, 'year' => '115', 'sec' => 0, 'wday' => 3, 'hour' => 18, 'mday' => 19 };
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: How to convert date like: 19 Aug 12:00 PM EDT to a perl structure
by Anonymous Monk on Aug 16, 2015 at 14:43 UTC
Re: How to convert date like: 19 Aug 12:00 PM EDT to a perl structure
by Intermediate Dave (Novice) on Aug 17, 2015 at 01:44 UTC
    You could also just "roll your own" with a few lines of code. Make a hash where the three-letter abbreviation for each month is the key, and the value is its numerical equivalent. Then...
    print "$months{$2}/$1/2015" if $ugly_date =~ /([0-3]?[[0-9]) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) .*/ # Returns 8/19/2015>
    Though you'd probably want to set a $current_year variable with a call to your system
Re: How to convert date like: 19 Aug 12:00 PM EDT to a perl structure
by ichudov (Initiate) on Aug 16, 2015 at 15:20 UTC
    Guys, I am aware that the year is missing, which adds one more layer of nonsense into this.
      From DateTime::Format::HTTP "If the year is missing, then we assume that the date is the first matching date before current month."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1138746]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found