Fix timezone issue with database in Rails on Heroku -


my rails app hosted on heroku postgress database. model i'm asking user chose day , time, combine date before saving model:

  def create_timestamp     self.date = day.to_datetime + time.seconds_since_midnight.seconds   end 

when chose instance today @ 20:50:00 , store in database, record looks this:

<report id: 1, account_id: 1, date: "2016-11-05 20:50:00", description: "test", created_at: "2016-11-05 19:50:57", updated_at: "2016-11-05 19:50:57", deleted_at: nil, user_id: 1, report_category_id: 2, time: "2000-01-01 20:50:00", day: "2016-11-05"> 

as might notice, created_at date different, because it's in different timezone. while created_at stored in utc +0000, custom date, uses local timezone cet +0100.

so when type in console: report.find(1).date, returns 2016-11-05 21:50:00 +0100.

how can store correct date in initial set, or make database return correct timezone when querying?

thanks

what you're doing this:

>> date = date.new(2016, 11, 6) => sun, 06 nov 2016 >> time = time.new(2000, 1, 1, 20, 50, 0) => 2000-01-01 20:50:00 +0100 >> date.to_datetime + time.seconds_since_midnight.seconds => sun, 06 nov 2016 20:50:00 +0000 

to_datetime converts time-zone-less date datetime representing midnight utc on date, , add 20 hours , 50 minutes.

instead of midnight utc, want midnight in local time zone starting point. this, example:

>> date.in_time_zone + time.seconds_since_midnight.seconds => sun, 06 nov 2016 20:50:00 cet +01:00 

rails should smart enough convert utc when storing in database , cet when retrieving database.

to clear difference, compare:

>> date => sun, 06 nov 2016 >> date.to_datetime => sun, 06 nov 2016 00:00:00 +0000 >> date.in_time_zone => sun, 06 nov 2016 00:00:00 cet +01:00 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -