← Back to Upcase

Time.zone.now Issue


(Scott Hollinshead) #1

In my application.rb file I have config.time_zone = 'London'

In Rails console when I do Time.zone.now I get the correct time.

irb(main):016:0> Time.zone.now
=> Wed, 16 Oct 2013 13:01:41 BST +01:00

I have this method in my user model

def send_password_reset
  self.password_reset_token = SecureRandom.hex(30)
  self.password_reset_sent_at = Time.zone.now
  save!
  UserMailer.password_reset(self).deliver
end

When I do User.first the date has been set one hour behind

2013-10-16 12:01:41

But when I do

user = User.first
user.password_reset_sent_at

It returns Wed, 16 Oct 2013 13:01:41 BST +01:00

What have I missed?

Update

This is my migration encase I have done something wrong there.

class AddPasswordResetToUsers < ActiveRecord::Migration
  def change
    add_column :users, :password_reset_token, :string
    add_column :users, :password_reset_sent_at, :datetime
  end
end

(George Brocklehurst) #2

Rails stores times in UTC (without the timezone offset). What you’re seeing in the database is the UTC time, which appears to be an hour off because your timezone is BST (British Summer Time, UTC + 1 hour).