Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I specifically set the timezone of the two dates to Australia/Sydney and America/Los_Angeles.

Be careful with this, since using set_time_zone may actually change the time value. If you have parsed times that include time zone information, then you do not need to use set_time_zone unless you specifically want to represent the time in another zone, but this is not necessary for comparisons! Only if your time did not include time zone information and it is in the special "floating" zone should you use set_time_zone, because as per the DateTime docs, comparisons are best between objects in a specific time zone (even if it's just UTC). See also my post here.

Then I get the UTC offset of the two dates, to compare

It sounds like you might be over-thinking the solution - DateTime allows for comparison of objects even if they are in different time zones. See its docs: subtract_datetime_absolute "is the only way to accurately measure the absolute amount of time between two datetimes, since units larger than a second do not represent a fixed number of seconds." For example (from here):

my $diff_sec = $dt2->subtract_datetime_absolute($dt1)     ->in_units('seconds');

So your steps should be:

  1. Parse your strings into DateTime objects (e.g. DateTime::Format::Strptime)
  2. For debugging, print out your objects including TZ info (e.g.  $dt->strftime('%Y-%m-%d %H:%M:%S.%3N %Z (%z)'))
  3. If and only if any of the objects are in the "floating" zone, set their time zone.
  4. Use subtract_datetime_absolute to compare them, as shown above.

Since the DateTime family are my favorite modules for date/time processing, I've written about them a lot, you should be able to find some good snippets among my posts. Update: In fact, this post should give you everything you need!


In reply to Re: Comparing DateTime objects in different timezones by haukex
in thread Comparing DateTime objects in different timezones by Cody Fendant

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2022-08-11 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?