http://qs1969.pair.com?node_id=1218337


in reply to Re^2: DateTime Invalid local time for date in time zone
in thread DateTime Invalid local time for date in time zone

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