Normally, the best thing to do would be to work directly with the database's date/time functions.
Perl's DateTime's "now" feature is in this format
Not exactly, that's just the default stringification. Note that DateTime objects can be compared directly, they overload the comparison operators.
If you're really stuck with parsing a string, my personal favorite module is DateTime::Format::Strptime. Here's one way to go about it:
use warnings;
use strict;
use DateTime;
use DateTime::Format::Strptime;
my $strp = DateTime::Format::Strptime->new(on_error=>'croak',
pattern => '%a %b %d, %Y - %H:%M:%S', time_zone => 'UTC');
my $dt = $strp->parse_datetime('Tue Oct 23, 2018 - 19:57:12');
print $dt->strftime('%Y-%m-%d %H:%M:%S %z'), "\n";
my $dt_one_day_ago = DateTime->now->subtract(days=>1);
print "$dt is ", $dt < $dt_one_day_ago ? '<' : '>=',
" $dt_one_day_ago\n";
The above takes the time of day into account as well, if that's what you want. Also, make sure to set the time zones correctly!
(BTW, there is also DateTime::Format::MySQL and DateTime::Format::DBI, although I haven't used these myself.) |