如何将浮点数转换为时区?

How can I convert float number to timezone?

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

Facebook 使用浮点数分解时区。

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.

我想将其转换为 timezone: "Eastern Time (US & Canada)"。然后用户可以通过 <%= 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?

您可以使用 ActiveSupport::TimeZone class 传入浮点数作为参数。 http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html.

免责声明:部分答案来自本题第一部分,.

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

您必须考虑 user.timezone 为零的次数,否则会引发错误。