in reply to Re: How to compare date/times in different timezones?
in thread How to compare date/times in different timezones?

Thanks for this. It looked like an excellent and simple solution. Unfortunately for me, I'm running CentOS 5.5 and there doesn't seem to be an RPM for perl-DateTime-Format-Flexible. While it's possible to get hold of a noarch version, that then generates dependency errors with perl(:MODULE_COMPAT_5.12.3) yadda yadda yadda...

Is there a similar solution that doesn't use DateTime::Format::Flexible?

  • Comment on Re^2: How to compare date/times in different timezones?

Replies are listed 'Best First'.
Re^3: How to compare date/times in different timezones?
by Jim (Curate) on Aug 28, 2011 at 16:37 UTC
    Is there a similar solution that doesn't use DateTime::Format::Flexible?

    Use DateTime::Format::Natural instead.

    According to the DateTime project wiki, it's what you're supposed to use instead of DateTime::Format::Flexible in any case. (See Confusing Modules.)

    ikegami already demonstrated using yet another DateTime module, DateTime::Format::DateParse, in his reply to your original post.

    Here's CountZero's script refactored using DateTime::Format::Natural.

    #!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.

Re^3: How to compare date/times in different timezones?
by Anonymous Monk on Aug 28, 2011 at 12:24 UTC

    cpan DateTime::Format::Flexible doesn't work for you?