Convert all datetimes to the universal UTC timezone and then compare. No need to reinvent the wheel!

use Modern::Perl; use DateTime; use DateTime::Format::Flexible; my $first_dt = DateTime::Format::Flexible->parse_datetime( 'Fri, 26 Au +g 2011 14:34:55 GMT'); my $second_dt = DateTime::Format::Flexible->parse_datetime( 'Sat, 27 A +ug 2011 07:03:02 +1000'); my $first_dt_utc = $first_dt->set_time_zone('UTC'); my $second_dt_utc = $second_dt->set_time_zone('UTC'); say $first_dt_utc->delta_days( $second_dt_utc )->delta_days, ' days of + difference';
It returns "0 days of difference".

The delta_days method takes into account only the date part of the DateTime object and returns a DateTime::Duration object and the delta_days method (of the DateTime::Duration-object, nothing to do with the delta_days method of the DateTime-object! Speaking of confusion here) converts that into a number of days.

Or without any intermediary steps or variables:

use Modern::Perl; use DateTime; use DateTime::Format::Flexible; say DateTime::Format::Flexible->parse_datetime( 'Fri, 26 Aug 2011 14:3 +4:55 GMT')->set_time_zone('UTC')->delta_days(DateTime::Format::Flexib +le->parse_datetime( 'Sat, 27 Aug 2011 07:03:02 +1000')->set_time_zone +('UTC'))->delta_days, ' days of difference';
Update: Changed the ->days method to the ->delta_days method as per Jim's excellent suggestion.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to Re: How to compare date/times in different timezones? by CountZero
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.