2

I've got 2 Time Without Time Zone columns in my table (start_time and end_time). When an event occurs, I need to see which of these columns has a start_time < Time.now < end_time. The problem seems to be that Rails is not saving the Time in UTC and is instead saving the time exactly as I enter it in my field. My local timezone is UTC -5, so if I put in an end_time of 09:00:00, I would expect that to get saved to the database as 14:00:00, but it's getting saved as 09:00:00, so when I later run my start_time < Time.now < end_time query, I'm getting the wrong results (I'm off by those 5 hours).

Here's an example (the model is called TimeParameter):

1.9.3-p392 :001 > TimeParameter.create(start_time: "00:30:00", end_time: "09:00:00")
   (7.0ms)  SELECT * FROM geometry_columns WHERE f_table_name='time_parameters'
   (0.2ms)  BEGIN
  SQL (9.8ms)  INSERT INTO "time_parameters" ("created_at", "day_of_week", "end_time", "start_time", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_at", Tue, 23 Jul 2013 14:29:06 UTC +00:00], ["day_of_week", nil], ["end_time", 2000-01-01 09:00:00 UTC], ["start_time", 2000-01-01 00:30:00 UTC], ["updated_at", Tue, 23 Jul 2013 14:29:06 UTC +00:00]]
   (308.9ms)  COMMIT
 => #<TimeParameter id: 24, start_time: "2000-01-01 00:30:00", end_time: "2000-01-01 09:00:00", day_of_week: nil, created_at: "2013-07-23 14:29:06", updated_at: "2013-07-23 14:29:06"> 

Local time is ~09:30, UTC is ~14:30 Notice that the updated_at and created_at reflect the 14:30 UTC time but my start_time and end_time are not converted to UTC before being saved.

Ruby version: 1.9.3-p392

Rails version: 3.2.13

4

0 に答える 0