Reputation: 683
My rails application has a Photo model which has a DATETIME column called taken_at.
I'm trying to understand why I get a different timestamp (num of secs since epoch) using the following methods:
Photo.find(3095).taken_at.to_i # => 1307292495
Photo.select("id, UNIX_TIMESTAMP(taken_at) as timestamp").where(:id => 3095).first['timestamp'] # => 1307317695
Photo.find(3095).taken_at.strftime('%s') # => "1307267295"
I'm using MySQL as the database and I'm using rails 4.1.7
Upvotes: 2
Views: 359
Reputation: 683
So the reason that using UNIX_TIMESTAMP(taken_at) in MYSQL gives a different result is because MySQL assumes that taken_at is a date stored in the server's time zone and converts it to an internal value in UTC before making the timestamp calculation. But taken_at isn't a date stored in the server's time zone, it is a date stored in UTC because ActiveRecord stores all datetimes as UTC in the database and then converts them back to the local time zone when you read the record back from the database.
references:
Upvotes: 2