简体   繁体   English

Rails在datetime上出现时区问题

[英]Rails issues with timezone on datetime

I have a rails app with events. 我有一个带有事件的rails应用程序。 My event table has a field called start_date which is of datetime type. 我的事件表有一个名为start_date的字段,它是datetime类型。 Currently when i save an event using chronic to parse the start_date field, the date gets incorrectly stored (or at least returns incorrectly). 目前,当我使用chronic保存事件来解析start_date字段时,日期会被错误地存储(或者至少返回错误)。

Example: I saved an event and in the textfield for start_date i entered 9/14/13 at 9am . 示例:我保存了一个事件,并在start_date的文本字段中,我9/14/13 at 9am进入9/14/13 at 9am It saved the data, refreshed the page, and the value in the textfield was 9/14/13 at 11am . 它保存了数据,刷新了页面,文本字段中的值是9/14/13 at 11am The value in the mysql table is 2013-09-14 16:00:00 . mysql表中的值是2013-09-14 16:00:00

What is going on here? 这里发生了什么? This only happens on my server and not when running locally. 这只发生在我的服务器上,而不是在本地运行时。

application.rb: application.rb中:

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

models/event.rb: 车型/ 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: 助手/ 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 edit.html.erb

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

Assuming 'Central Time (US & Canada)' is correct (ie you are not on the Eastern time zone), then: 假设'Central Time (US & 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 

You can put Chronic.time_class = Time.zone it in config/initializers/chronic.rb 您可以将Chronic.time_class = Time.zone放在config/initializers/chronic.rb Chronic.time_class = Time.zone

Sorry, I'm on the tablet so this will have to be brief. 对不起,我在平板电脑上,所以这必须简短。 I've had a problem like this and corrected it by using something like 我有这样的问题,并通过使用类似的东西纠正它

Time.now.utc 

I do see that you have in the application.rb config.time_zone though, so this may not work. 我确实看到你在application.rb config.time_zone中有这个,所以这可能不起作用。

If it doesn't, here's a good SO answer, maybe it will help 如果没有,这是一个很好的答案,也许它会有所帮助

Why doesn't `config.time_zone` seem to do anything? 为什么`config.time_zone`似乎没有做任何事情?

I had same issue, solved it by adding this in application.rb- 我有同样的问题,通过在application.rb-中添加它来解决它。

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

I had same issue and I just fixed it by commenting this line in my application.rb: 我有同样的问题,我只是通过在我的application.rb中注释这一行来修复它:

config.active_record.default_timezone = :local

I did this according to what I read in the application.rb: 我是根据我在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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM