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.


In reply to Re^3: How to compare date/times in different timezones? by Jim
in thread How to compare date/times in different timezones? by philled

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.