【问题标题】:Rails: convert UTC DateTime to another time zoneRails:将UTC DateTime转换为另一个时区
【发布时间】:2011-02-11 08:32:57
【问题描述】:

在 Ruby/Rails 中,如何将 UTC 日期时间转换为另一个时区?

【问题讨论】:

    标签: ruby-on-rails ruby datetime runtime


    【解决方案1】:
    time.in_time_zone(time_zone)
    

    例子:

    zone = ActiveSupport::TimeZone.new("Central Time (US & Canada)")
    Time.now.in_time_zone(zone)
    

    或者只是

    Time.now.in_time_zone("Central Time (US & Canada)")
    

    您可以通过以下方式找到 ActiveSupport 时区的名称:

    ActiveSupport::TimeZone.all.map(&:name)
    # or for just US
    ActiveSupport::TimeZone.us_zones.map(&:name)
    

    【讨论】:

    • 在 irb 中,当我尝试 Time.now.in_time_zone('CST') 时,我收到错误“未定义的方法 'in_time_zone'”。为了让它工作,我需要什么 Rails 类?
    • 要在 Rails 外部使用这个,require 'active_support/time' 首先。
    • 您可以通过运行rake time:zones:all 列出所有时区。另见rake -D time。在config/application.rb中设置默认时区。
    • @AdamEberlin 在 Rails 中,您通常使用 ActiveSupport::TimeWithZone,而不是 DateTime。所有这些代码都适用于 ruby​​ DateTime 对象以及 Time 对象。
    • 您还可以从ActiveSupport::TimeZone documentation 页面获取所有时区的列表。
    【解决方案2】:

    如果Time.zone是您想要的时区,那么您可以使用@date.to_time.to_datetime

    > @date
    => Tue, 02 Sep 2014 23:59:59 +0000
    > @date.class
    => DateTime
    > @date.to_time
    => 2014-09-02 12:59:59 -1100
    > @date.to_time.to_datetime
    => Tue, 02 Sep 2014 12:59:59 -1100 
    

    【讨论】:

    • 不适用于 Rails 5 `Time.zone # => Europe/Paris my_date # => Wed, 29 Mar 2017 19:00:20 +0200 my_date.to_time # => "2017- 03-29T17:00:20.000+00:00" my_date.to_time.to_datetime # => 2017 年 3 月 29 日,星期三 17:00:20 +0000 `
    【解决方案3】:

    在纯 ruby​​ 中,只有 require 'date',使用 new_offset 方法:

    require 'date'
    
    d=DateTime.parse('2000-01-01 12:00 +0200')
    l=d.new_offset('-0700')
    u=l.new_offset('UTC')
    puts "#{u.strftime('%a %F %T %Z')} ❖ #{l.strftime('%a %F %T %Z')}"
    

    使用 Mac OS X 10.13 标准的 ruby​​ 2.3.7 进行测试。

    【讨论】:

      【解决方案4】:

      尝试使用 TimeZone 操作的 ActiveSupport 的 TimeWithZone 对象。 ActiveSupport 还提供了 in_time_zone 方法,用于将 UTC 时间转换为指定的 TimeZone 时区。 mckeed 的回答显示了代码。

      【讨论】:

      • 将 TimeWithZone 导入我的班级的语法是什么?还是我默认使用 Rails 获得它?
      • 我相信你默认得到它。 mckeed 有你需要的代码,但你不会在 irb 中看到它。你需要在 Rails 中运行它。
      • 你是对的 - 谢谢,Fred - 默认情况下它确实带有 Rails。我正抓着稻草,试图让它在 irb 中发挥作用。
      【解决方案5】:

      以防万一,如果您在 Rails 中处理 ActiveRecord 对象。

      最好将Time.use_zone 用于覆盖config.time_zone 中设置的默认时区的每个请求的时区

      更多细节我在https://stackoverflow.com/a/25055692/542995解释

      【讨论】:

        【解决方案6】:

        我在 Rails 4 中使用 simple_form,我只是将输入字段添加为

        <%= f.input :time_zone, :as => :time_zone %>
        

        随着迁移

        class AddTimeZoneColumnToTextmessage < ActiveRecord::Migration
          def change
            add_column :textmessages, :time_zone, :string
          end
        end
        

        【讨论】:

        • 这并不能回答您如何转换 DateTime 对象的问题
        猜你喜欢
        • 2011-02-02
        • 2019-06-04
        • 2015-10-16
        • 2013-04-07
        • 2016-09-28
        • 2012-08-06
        • 1970-01-01
        • 2013-08-25
        • 1970-01-01
        相关资源
        最近更新 更多