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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.