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

Just wanted to say thanks to everyone for their help on this. I think I've finally got there now. For the record the code I'm using is as follows. I'm converting the datetime into Australia/Sydney timezone before doing the comparison.
use DateTime::Format::DateParse; my $p1 = shift; my $p2 = shift; my $first_dt = DateTime::Format::DateParse->parse_datetime("$p1"); my $second_dt = DateTime::Format::DateParse->parse_datetime("$p2"); my $first_dt_syd = $first_dt->set_time_zone('Australia/Sydney'); my $second_dt_syd = $second_dt->set_time_zone('Australia/Sydney'); my $diff = $first_dt_syd->delta_days( $second_dt_syd )->delta_days;

Thanks again.

Replies are listed 'Best First'.
Re^4: How to compare date/times in different timezones?
by Jim (Curate) on Aug 29, 2011 at 15:48 UTC

    Excellent!

    The rule of thumb for most date and time calculations is to normalize both moments to UTC first, then perform the math. But in this case, it really only makes sense to compare dates from the perspective of a specific local time. It's easy to imagine a practical application for wanting to know if two moments at two different places in the world occurred on the same date reckoned from Sydney, Australia. It's not as easy to imagine a practical application for wanting to know if two moments occurred on the same date reckoned from UTC, which is abstract. Who would care?

    Consider this very light refactoring:

    #!perl use strict; use warnings; use DateTime; use DateTime::Format::DateParse; @ARGV == 2 or die "Usage: perl $0 <timestamp 1> <timestamp 2>\n"; my $ts1 = shift; my $ts2 = shift; my $tz = 'Australia/Sydney'; my $dt1 = DateTime::Format::DateParse->parse_datetime($ts1) ->set_time_zone($tz); my $dt2 = DateTime::Format::DateParse->parse_datetime($ts2) ->set_time_zone($tz); my $diff = $dt1->delta_days($dt2)->delta_days(); print "$dt1 delta $dt2 is $diff days\n"; exit 0;