Moon
Moon

Reputation: 22565

How to preserve timezone when using Time.to_s(:db)?

I'm trying to convert Time object with PST timezone.

zone = ActiveSupport::TimeZone.new("Pacific Time (US & Canada)")
now = Time.now.in_time_zone(zone)

when I output now, it's correct. when I do Time.now.in_time_zone(zone).to_s(:db), it outputs GMT (original date).

How do I fix it?

Update: Looks like..the following code works

zone = ActiveSupport::TimeZone.new("Pacific Time (US & Canada)")
now = Time.now.in_time_zone(zone).strftime("%Y-%m-%d %H:%M:%S")

Upvotes: 2

Views: 3286

Answers (1)

BvuRVKyUVlViVIc7
BvuRVKyUVlViVIc7

Reputation: 11811

Database dates are usually stored in utc time format..

ActiveSupport is returning UTC, not GMT on to_s(:db), see http://apidock.com/rails/ActiveSupport/TimeWithZone/to_s

EDIT: Maybe this helps you? http://chris.finne.us/2011/09/23/rails-3-tosdb-when-database-time-is-not-utc/

Upvotes: 2

Related Questions