How can I convert float number to timezone?

2019-09-06 07:05发布

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条回答
劫难
2楼-- · 2019-09-06 07:54

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.

查看更多
登录 后发表回答