Home > Software design >  TZInfo::PeriodNotFound XYZ is an invalid local time
TZInfo::PeriodNotFound XYZ is an invalid local time

Time:03-07

I have a method called convert_time_with_zone and it has been called hundreds of thousands of times, if not millions, over the last few months. However, today it suddenly resulted in an error.

Here's my method:

  def convert_time_with_zone(time)
    ActiveSupport::TimeZone.new(time_zone).local_to_utc(time)
  end

Here's the error I'm experiencing:

[6] pry(#<Assessment>)> current_day
=> 2022-03-06 02:00:00  0000
[7] pry(#<Assessment>)> future_day
=> 2022-03-13 02:00:00  0000
[8] pry(#<Assessment>)> convert_time_with_zone(current_day)
=> 2022-03-06 07:00:00 UTC
[9] pry(#<Assessment>)> convert_time_with_zone(future_day)
TZInfo::PeriodNotFound: 2022-03-13 02:00:00 is an invalid local time.
from /usr/local/bundle/gems/tzinfo-2.0.4/lib/tzinfo/timezone.rb:500:in `period_for_local'
Caused by TZInfo::PeriodNotFound: 2022-03-13 02:00:00 is an invalid local time.
from /usr/local/bundle/gems/tzinfo-2.0.4/lib/tzinfo/timezone.rb:500:in `period_for_local'
[10] pry(#<Assessment>)> 

I don't understand why, in the exact same format as the working one, the other one doesn't work. The only difference between the two is one is March 6th whereas the other is March 13th.

CodePudding user response:

There is no 2022-03-13 02:00 in that, presumably US, time zone.

That is when daylight savings time "leaps ahead" in the US. On that day the local clock goes from 01:59:59 to 03:00:00.

Similarly, 2022-11-06 01:00 will happen twice when clocks "fall back" making converting local times in that range ambiguous.

  • Related