How can I convert float number to timezone?

人盡茶涼 提交于 2019-12-13 05:31:51

问题


How can I convert float number to timezone?

Facebook breaks the timezone down using float.

timezone    float (min: -24) (max: 24)
            The person's current timezone offset from UTC

user.timezone = auth.extra.raw_info.timezone captures a user's timezone location upon signing up via Facebook, for example timezone: -5.0.

I want to convert this though to timezone: "Eastern Time (US & Canada)". A user could then adjust his timezone via <%= f.time_zone_select :timezone, ActiveSupport::TimeZone.us_zones %>.

Potential Solution

t.float    "timezone"
t.string   "time_zone" # somehow turning it into a string after Facebook sets it?

回答1:


You can use the ActiveSupport::TimeZone class pass in the float as your argument. http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html.

Disclaimer: Part of the answer comes from the first part of this question, How to get timezone from facebook with omniauth?.

user.timezone = auth.extra.raw_info.timezone
user.time_zone = ActiveSupport::TimeZone.new(user.timezone).name

You will have to account for times when user.timezone is nil, or else it will throw errors.



来源:https://stackoverflow.com/questions/36319711/how-can-i-convert-float-number-to-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!