Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Thank you for getting back on my question.

So, as documented in the "Invalid Local Times" and "Error Handling" sections, DateTime was throwing an exception by "die"ing with a string explaining the problem. haukex solution would do something very similar on $dt->set_time_zone('America/Los_Angeles'); if it didn't add an hour before setting the time zone. If it is possible that there may be other invalid timestamps based on DST then you can also catch the exception, try to verify that the problem is DST and handle the error accordingly. I am presenting, below, an exception handling solution based on your OP but you might want to similarly adjust the haukex solution.

#!/usr/bin/env perl use strict; use warnings; use DateTime; use Try::Tiny; my $occuranceTime = "00:02:00"; my $occuranceDate = "03/13/2016"; my ($recordSec, $recordHour, $recordMin) = split ':', $occuranceTime; my ($recordMonth, $recordDate, $recordYear) = split '/', $occuranceDat +e; my $recordOnPST = try { DateTime->new( year => $recordYear, month => $recordMonth, day => $recordDate +, hour => $recordHour, minute => $recordMin, second => $recordSe +c, time_zone => 'America/Los_Angeles' ); } catch { if ( /^Invalid local time for date in time zone/ and $recordMonth == 3 and $recordHour == 2 ) { warn "Looks like invalid DST timestamp\n"; return DateTime->new( year => $recordYear, month => $recordMonth, day => $record +Date, hour => $recordHour +1, minute => $recordMin, second => $r +ecordSec, time_zone => 'America/Los_Angeles' ); } else { die $_; # try to rethrow other exception } return undef; # in case you don't want to return adjusted time }; if ($recordOnPST) { my $recordOnUTC = $recordOnPST -> clone -> set_time_zone('UTC'); print "$recordOnPST \n"; print "$recordOnUTC \n"; }
Ron

In reply to Re^3: DateTime Invalid local time for date in time zone by mr_ron
in thread DateTime Invalid local time for date in time zone by sannag

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 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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found