如何将浮点数转换为时区

如何将浮点数转换为时区

本文介绍了如何将浮点数转换为时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将float数字转换为时区?

How can I convert float number to timezone?

Facebook使用float细分时区.

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在通过Facebook注册后捕获用户的时区位置,例如timezone: -5.0.

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

我想将其转换为timezone: "Eastern Time (US & Canada)".然后,用户可以通过<%= f.time_zone_select :timezone, ActiveSupport::TimeZone.us_zones %>调整其时区.

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 %>.

潜在的解决方案

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

推荐答案

您可以使用float中的ActiveSupport::TimeZone类传递作为参数. http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html.

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

免责声明:答案的一部分来自该问题的第一部分,如何使用omniauth从Facebook获取时区?.

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

您必须考虑user.timezone为零的时间,否则它将引发错误.

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

这篇关于如何将浮点数转换为时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:03