It does seem that, at least according to DateTime, the time 13th March 2016 2 a.m. does not exist:
use warnings; use strict; use DateTime; use DateTime::Format::Strptime; my $str = "03/13/2016 09:59:59"; my $strp = DateTime::Format::Strptime->new( pattern => '%m/%d/%Y %H:%M:%S', time_zone=>'UTC', on_error=>'croak'); my $dt = $strp->parse_datetime($str); print $dt->strftime('%Y-%m-%d %H:%M:%S %Z')," is ", $dt->clone->set_time_zone('America/Los_Angeles') ->strftime('%Y-%m-%d %H:%M:%S %Z'),"\n"; $dt->add(seconds=>1); print $dt->strftime('%Y-%m-%d %H:%M:%S %Z')," is ", $dt->clone->set_time_zone('America/Los_Angeles') ->strftime('%Y-%m-%d %H:%M:%S %Z'),"\n"; __END__ 2016-03-13 09:59:59 UTC is 2016-03-13 01:59:59 PST 2016-03-13 10:00:00 UTC is 2016-03-13 03:00:00 PDT
I am converting it to UTC I encounter an error when run the time 03/113/2016 00:02:00. 13th March 2016 2Am is daylight saving.
Do you know how this time was recorded? Do you have any other times from 2:00am through 2:59am? Because if you do, I think you might have a bigger problem. I would strongly recommend looking at fixing the source of the data first.
I'm also confused: according to your substr, is your time format really SS:MM:HH SS:HH:MM?? In any case, I'd recommend using a "proper" parser like DateTime::Format::Strptime.
But if it's only 2016-03-13T02:00:00 that is incorrect, then I'd suggest "patching" this manually. Parse the date in the "floating" time zone, "fix" the time*, and then set the time zone. (If you have lots of incorrect date/time values like this, this might cumbersome, hence my above suggestion to fix it at the source instead.)
use warnings; use strict; use DateTime; use DateTime::Format::Strptime; my $str = "03/13/2016 02:00:00"; my $strp = DateTime::Format::Strptime->new( pattern => '%m/%d/%Y %H:%M:%S', time_zone=>'floating', on_error=>'croak'); my $dt = $strp->parse_datetime($str); if ($dt->year==2016 && $dt->month==3 && $dt->day==13 && $dt->hour==2) { # any minute and second $dt->add(hours=>1); } $dt->set_time_zone('America/Los_Angeles'); print $dt->strftime('%Y-%m-%d %H:%M:%S %Z'),"\n"; __END__ 2016-03-13 03:00:00 PDT
* Update: However, If you have records, for example, at 2:15am and 3:15am, the above code will make them look like the same time! That's why I said above that this only makes sense if you only have 02:00:00 as a time to fix. For example, I imagine it's possible the software that wrote these records makes the DST jump incorrectly from 02:00:59 to 03:01:00, which means there shouldn't be any records between those times.
In reply to Re: DateTime Invalid local time for date in time zone (updated)
by haukex
in thread DateTime Invalid local time for date in time zone
by sannag
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |