rubyrufus-scheduler

Windows: Rufus: `ENV['TZ'] = 'Continent/City' Error


I want t schedule a job running every hour on my windows machine and used rufus-scheduler for this. However, I am not able to proceed due to the timezone error explained below.

require 'rufus-scheduler'

require 'rufus-scheduler'
require 'tzinfo/data'

ENV['TZ'] = 'Asia/Chennai'

scheduler = Rufus::Scheduler::singleton

scheduler.every '1h' do
  # job here
end

I am constantly thrown with this error.

 C:/Ruby22/lib/ruby/gems/2.2.0/gems/rufus-scheduler- 3.3.4/lib/rufus/scheduler/zotime.rb:41:in `initialize': cannot determine timezone from nil (etz:"Asia/India",tnz:"Asi",tzid:"constant") (ArgumentError)
 Try setting `ENV['TZ'] = 'Continent/City'` in your script (see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rufus-scheduler-  3.3.4/lib/rufus/scheduler/zotime.rb:206:in `new'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rufus-scheduler- 3.3.4/lib/rufus/scheduler/zotime.rb:206:in `now'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.3.4/lib/rufus/scheduler.rb:561:in `start'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.3.4/lib/rufus/scheduler.rb:109:in `initialize'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.3.4/lib/rufus/scheduler.rb:116:in `new'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.3.4/lib/rufus/scheduler.rb:116:in `singleton'
    from scheduler.rb:9:in `<main>'

Can anyone help on this?


Solution

  • According to the Wikipedia article linked in the error message, the only valid timezone for India (and thus IST) is Asia/Kolkata.

    Thus, if you set

    ENV['TZ'] = 'Asia/Kolkata'
    

    you should get the correct timezone.

    That said, it is probably a good idea to setup your operating system to set the correct local timezone in the first place. This allows other software to work correctly out of the box without requiring specific timezone configuration in every one separately.