#!perl
use strict;
use warnings;
use DateTime;
use DateTime::Format::Natural;
my $parser = DateTime::Format::Natural->new();
my $first_timestamp = 'Mon, 27 Aug 2007 14:34:55 GMT';
my $second_timestamp = 'Sat, 27 Aug 2011 07:03:02 +1000';
$first_timestamp = $parser->extract_datetime($first_timestamp);
$second_timestamp = $parser->extract_datetime($second_timestamp);
my $first_dt = $parser->parse_datetime($first_timestamp);
my $second_dt = $parser->parse_datetime($second_timestamp);
print $first_dt->delta_days($second_dt)->days(), " days difference\n";
exit 0;
This prints "5 days difference". To me, this isn't what you're after. Is it? (N.B. I used a different first timestamp than the one you presented.)
Frankly, your problem confounds me. When it's 12:30 a.m. on January 1 in New York City (U.S. Eastern Time), it's 11:30 p.m. on December 31 in Chicago (U.S. Central Time). By definition, this moment in time occurs on two different dates in those two time zones. In fact, they're different days of the week, different dates of the month, different months and different years.
UPDATE: Epic fail. DateTime::Format::Natural doesn't parse the timestamps correctly at all. Psshh!
DateTime::Format::DateParse is better.
#!perl
use strict;
use warnings;
use feature qw( say );
use DateTime;
use DateTime::Format::DateParse;
use DateTime::Format::Strptime;
my $first_timestamp = 'Fri, 27 Jul 2007 14:34:55 GMT';
my $second_timestamp = 'Wed, 27 Jul 2011 07:03:02 +1000';
say $first_timestamp;
say $second_timestamp;
my $first_dt
= DateTime::Format::DateParse->parse_datetime($first_timestamp);
my $second_dt
= DateTime::Format::DateParse->parse_datetime($second_timestamp);
$first_dt->set_time_zone('UTC');
$second_dt->set_time_zone('UTC');
my $strp = DateTime::Format::Strptime->new(pattern => '%F %T %z');
say $strp->format_datetime($first_dt);
say $strp->format_datetime($second_dt);
say $first_dt->delta_days($second_dt)->delta_days(),
' days difference';
exit 0;
This prints…
Fri, 27 Jul 2007 14:34:55 GMT
Wed, 27 Jul 2011 07:03:02 +1000
2007-07-27 14:34:55 +0000
2011-07-26 21:03:02 +0000
1460 days difference
Sorry. You wanted clarification, but you got more confusion.
|