floraison / fugit

time tools (cron, parsing, durations, ...) for Ruby, rufus-scheduler, and flor
MIT License
353 stars 29 forks source link

TZInfo::PeriodNotFound exception when using Time.zone #62

Closed codez closed 2 years ago

codez commented 2 years ago

Issue description

When using Time.zone instead of ENV['TZ'], Cron#next_time throws a TZInfo::PeriodNotFound exception.

How to reproduce

require 'fugit'
require 'active_support/core_ext/time/zones'
Time.zone = 'America/Santiago'  # it works with ENV['TZ'] = 'America/Santiago'
t = EtOrbi::EoTime.parse('2021-08-18 01:00:00')
puts Fugit::Cron.parse('0 8 15 * *').next_time(t).to_s

Error and error backtrace (if any)

 11: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/fugit-1.5.1/lib/fugit/cron.rb:251:in `next_time'
 10: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/fugit-1.5.1/lib/fugit/cron.rb:251:in `loop'
  9: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/fugit-1.5.1/lib/fugit/cron.rb:261:in `block in next_time'
  8: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/fugit-1.5.1/lib/fugit/cron.rb:107:in `inc_day'
  7: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/et-orbi-1.2.4/lib/et-orbi/make.rb:59:in `make_time'
  6: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/et-orbi-1.2.4/lib/et-orbi/make.rb:108:in `make_from_array'
  5: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/et-orbi-1.2.4/lib/et-orbi/make.rb:44:in `parse'
  4: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/tzinfo-2.0.3/lib/tzinfo/timezone.rb:648:in `local_to_utc'
  3: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/tzinfo-2.0.3/lib/tzinfo/timestamp.rb:142:in `for'
  2: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/tzinfo-2.0.3/lib/tzinfo/timezone.rb:652:in `block in local_to_utc'
  1: from /Users/pascal/.rvm/gems/ruby-2.7.2/gems/tzinfo-2.0.3/lib/tzinfo/timezone.rb:500:in `period_for_local'
     TZInfo::PeriodNotFound (2021-09-05 00:00:00 is an invalid local time.)

Expected behaviour

The timezone setting in Time.zone should be used as fallback when ENV['TZ'] is nil and the correct output ("2021-09-15 08:00:00 -0300") should be returned.

Context

Darwin Pascals-BigPro.fritz.box 20.6.0 Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64 x86_64
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin20]
[:env_tz, nil]
(secs:1631862880.412856,utc~:"2021-09-17 07:14:40.4128561019897461",ltz~:"CEST")
(etz:nil,tnz:"CEST",tziv:"2.0.4",tzidv:nil,rv:"2.7.2",rp:"x86_64-darwin20",win:false,rorv:nil,astz:nil,eov:"1.2.4",eotnz:#<TZInfo::TimezoneProxy: Africa/Ceuta>,eotnfz:"+0200",eotlzn:"Africa/Ceuta",eotnfZ:"CEST",debian:nil,centos:nil,osx:"zoneinfo/Europe/Zurich")
"1.5.1"
[:now, 2021-09-17 09:15:13.611169 +0200, :zone, "CEST"]

)

jmettraux commented 2 years ago

Thanks.

Moved to https://github.com/floraison/et-orbi/issues/31

jmettraux commented 2 years ago

The time zone in Time.zone is used and it is what is causing the problem in ::TZInfo::TimeZone.local_to_utc. Back here.

jmettraux commented 2 years ago

https://github.com/codez/delayed_cron_job/issues/30

jmettraux commented 2 years ago

Hello,

I've just released https://rubygems.org/gems/fugit/versions/1.5.2

It solves the issue (at least for me). If you spot anything wrong, please do tell me.

Thanks and best regards!

codez commented 2 years ago

Thank you very much for your work!

jmettraux commented 2 years ago

@codez je t'en prie, merci vielmal again!