If two timestamps share a date from which time zone's perspective: any time zone or a specific time zone?

The former requires checking if the time stamps are within 24 hours of each other.

The latter requires comparing the dates of the time stamps after converting the time stamps to the specific time zone.

use strict; use warnings; use DateTime::Format::DateParse qw( ); sub same_date_anywhere { my ($dt1, $dt2) = @_; ($dt1, $dt2) = ($dt2, $dt1) if $dt1 < $dt2; $dt1 = $dt1->clone(); $dt1->subtract( hours => 24 ); return $dt1 < $dt2; } sub same_date_somewhere { my ($dt1, $dt2, $tz) = @_; for my $dt ($dt1, $dt2) { ( $dt = $dt->clone() ) ->set_time_zone($tz) ->truncate( to => 'days' ); } return $dt1 == $dt2; } my $dt1 = DateTime::Format::DateParse->parse_datetime("Fri, 26 Aug 201 +1 14:34:55 GMT"); my $dt2 = DateTime::Format::DateParse->parse_datetime("Sat, 27 Aug 201 +1 07:03:02 +1000"); say(same_date_anyhere($dt1, $dt2) ?1:0); say(same_date_somewhere($dt1, $dt2, $dt1->time_zone) ?1:0); say(same_date_somewhere($dt1, $dt2, $dt2->time_zone) ?1:0); say(same_date_somewhere($dt1, $dt2, "America/New_York") ?1:0); say(same_date_somewhere($dt1, $dt2, "UTC") ?1:0);

Untested.


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