【问题标题】:Rails issues with timezone on datetime日期时间时区的 Rails 问题
【发布时间】:2013-08-30 02:56:13
【问题描述】:

我有一个带有事件的 Rails 应用程序。我的事件表有一个名为start_date 的字段,它属于日期时间类型。目前,当我使用慢性保存事件来解析 start_date 字段时,日期存储不正确(或至少返回不正确)。

示例:我保存了一个事件,并在 start_date 的文本字段中输入了9/14/13 at 9am。它保存了数据,刷新了页面,文本字段中的值为9/14/13 at 11am。 mysql表中的值为2013-09-14 16:00:00

这里发生了什么?这只发生在我的服务器上,而不是在本地运行时发生。

应用程序.rb:

config.time_zone = 'Central Time (US & Canada)'

models/event.rb:

class Event < ActiveRecord::Base
    belongs_to :user
    attr_accessible :description, :name, :start_date_string

    validate :name_not_blank
    validate :start_date_not_blank

    # Validate that the name is not blank
    def name_not_blank
        errors.add(:name, "Name can't be blank") unless !name.blank?
    end

    # Validate that the name is not blank
    def start_date_not_blank
        errors.add(:start_date, "Date and Time can't be blank") unless !start_date.blank?
    end

    # getter
    def start_date_string
        @start_date_string || start_date.in_time_zone.try(:strftime, "%m/%d/%Y at %I:%M %P")
    end

    # setter
    def start_date_string=(value)
        @start_date_string = value
        self.start_date = parse_start_date
    end

    # parse the start_date
    def parse_start_date
        Chronic.parse(start_date_string)
    end     
end

helpers/application_helper.rb:

# Formats a date to words (i.e. March 20, 1980 at 10:00 am)
def spell_date_and_time(date) 
    date.strftime("%B %d, %Y at %I:%M %P")
end

edit.html.erb

<span class="timestamp"><%= spell_date_and_time(event.start_date) %></span>

【问题讨论】:

  • 顺便说一句,真的不需要parse_start_date,你可以只使用start_date_string setter。如果您使用的是文本字段,还要注意 nil 和空白时间值。

标签: mysql ruby-on-rails ruby-on-rails-3


【解决方案1】:

假设'Central Time (US &amp; Canada)' 是正确的(即您不在东部时区),那么:

> Time.zone = "UTC" 
> Chronic.time_class = Time.zone 
> Chronic.parse("June 15 2006 at 5:45 AM") 
=> Thu, 15 Jun 2006 05:45:00 UTC +00:00

你可以把Chronic.time_class = Time.zone放到config/initializers/chronic.rb

【讨论】:

    【解决方案2】:

    我有同样的问题,我只是通过在我的 application.rb 中评论这一行来解决它:

    config.active_record.default_timezone = :local
    

    我是根据我在 application.rb 中读到的:

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    

    【讨论】:

      【解决方案3】:

      抱歉,我正在使用平板电脑,所以必须简短。我遇到了这样的问题并通过使用类似的东西来纠正它

      Time.now.utc 
      

      我确实看到您在 application.rb config.time_zone 中有,所以这可能不起作用。

      如果没有,这是一个很好的答案,也许会有所帮助

      Why doesn't `config.time_zone` seem to do anything?

      【讨论】:

        【解决方案4】:

        我有同样的问题,通过在 application.rb 中添加这个解决了它-

        config.time_zone = 'Central Time (US & Canada)'
        config.active_record.default_timezone = :local
        

        【讨论】:

        • 这似乎将记录保存在当前时区(例如,我将其保存为上午 8:00,而数据库中为上午 8:00)。出于某种原因,在我的应用中查看时仍然显示上午 10:00。
        猜你喜欢
        • 2017-09-30
        • 1970-01-01
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        • 2021-03-09
        • 2019-01-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多