Problems with Rails with timezone in datetime

I have a rails application with events. My event table has a field called start_date that has a date and time type. Currently, when I save an event using the start_date field, which is chronic for parsing, the date receives the wrong save (or at least returns incorrectly).

Example: I saved the event and in the text box for start_date I entered 9/14/13 at 9am . He saved the data, refreshed the page, and the value in the text box was 9/14/13 at 11am . The value in the mysql table is 2013-09-14 16:00:00 .

What's going on here? This only happens on my server, not on local startup.

application.rb:

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

/ event.rb models:

 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 (ie 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> 
+4
source share
4 answers

Assuming 'Central Time (US & Canada)' correct (i.e. you are not in the eastern time zone), then:

 > 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 in config/initializers/chronic.rb

+3
source

Sorry, I'm on a tablet, so this should be brief. I had such a problem and I fixed it using something like

 Time.now.utc 

I see what you have in application.rb config.time_zone, so this may not work.

If not, here is a good SO answer, maybe this will help

Why does `config.time_zone` do nothing?

0
source

I had the same problem, it was solved by adding this to application.rb-

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

I had the same problem and I just fixed it by commenting on this line in my .rb application:

 config.active_record.default_timezone = :local 

I did this according to what I read in 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. 
0
source

Source: https://habr.com/ru/post/1499728/


All Articles