Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting the Time Zone Dynamically #230

Closed
adeeb1 opened this issue Jan 16, 2017 · 10 comments
Closed

Setting the Time Zone Dynamically #230

adeeb1 opened this issue Jan 16, 2017 · 10 comments

Comments

@adeeb1
Copy link

adeeb1 commented Jan 16, 2017

I ran into the "Cannot determine timezone from nil" error and wanted to post my solution. It involves setting the ENV['TZ'] variable, as you suggested, but I don't like to hardcode values like this, especially if your time zone changes from development to production environments. You can let Ruby get the time zone more explicitly.

ENV['TZ'] = Time.zone.name
scheduler = Rufus::Scheduler.new
scheduler.every '2s' do
  puts "#{Time.now} Hello #{ENV['TZ']}!"
end
@adeeb1 adeeb1 changed the title [FAQ] Setting the Time ZOne [FAQ] Setting the Time Zone Dynamically Jan 16, 2017
@jmettraux
Copy link
Owner

Good morning,

$ ruby -v
ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-darwin14]
$ ruby -e "Time.zone.name"
-e:1:in `<main>': undefined method `zone' for Time:Class (NoMethodError)

@adeeb1
Copy link
Author

adeeb1 commented Jan 17, 2017

Thanks for the quick reply! It looks like Time.zone is specific to ActiveSupport: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html. I think my approach would work for Rails, but without Rails you'd need to manually specify the time zone as you have described in your FAQ.

@jmettraux
Copy link
Owner

jmettraux commented Jan 17, 2017

What kind of zone string does your Time.zone.name returns?

If it's something like "CST" it is very ambiguous and leads to problems like #228

Beware of not shooting yourself in the foot.

@adeeb1
Copy link
Author

adeeb1 commented Jan 17, 2017

Good point. Mine returns "UTC" and seems to be working fine. ActiveSupport::TimeZone is a wrapper over TZInfo. What may be even safer is to use the tzinfo identifier instead:

ENV['TZ'] = Time.zone.tzinfo.identifier # "Etc/UTC"

@jmettraux
Copy link
Owner

Could you please try something in your system for me?

Please set your system zone to Shangai then tell me what Time.zone.tzinfo.identifier returns.

Thanks in advance.

@adeeb1
Copy link
Author

adeeb1 commented Jan 17, 2017

It doesn't look like it uses your local time; mine returns "UTC." The docs say you have to set the time zone in Rails' application.rb; otherwise it will default to UTC. Shanghai isn't listed as a time zone:

> rake time:zones:all
...

* UTC +08:00 *
Beijing
Chongqing
Hong Kong
Irkutsk
Kuala Lumpur
Perth
Singapore
Taipei
Ulaanbaatar
...

I set the time zone to "Beijing," and it returned the Shanghai identifier, which makes sense since it says "Beijing Time" next to it on Wikipedia.

> rails c
> Time.zone = "Beijing"
=> "Beijing"
> Time.zone.tzinfo.identifier
=> "Asia/Shanghai"

@jmettraux
Copy link
Owner

OK, how about changing the operating system zone?

@adeeb1
Copy link
Author

adeeb1 commented Jan 17, 2017

The same as above. I changed the time zone of my OS, and it still returned "UTC" because Rails defaults to UTC if you don't explicitly specify a time zone in your Rails app.

@jmettraux
Copy link
Owner

OK. Thank you very much!

@jmettraux jmettraux changed the title [FAQ] Setting the Time Zone Dynamically Setting the Time Zone Dynamically Jan 27, 2017
@jmettraux
Copy link
Owner

(removed the [FAQ] prefix as it might confuse people)

jmettraux added a commit that referenced this issue Jan 28, 2017
gh-230, gh-233

Let's hope Rails provide a usable and up-to-date timezone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants