1

The simple thing I want is to store a DateTime in UTC in the database and retrieve it in UTC again, but rails seems to assume I stored the DateTime in my local time zone and adds the 8 hours difference (PST). I have a DateTime field in my model. If I send a request (PUT) to update this field in a particular instance of my model, somehow timezones get mixed up for whatever reason:

Sending to rails via PUT: 2012-02-17T03:46:58Z

Returned via subsequent GET: 2012-02-17T11:46:58Z

The time difference is exactly 8 hours and could be explained by my timezone which is PST (-08:00).

I am using Datamapper 1.2 on Rails 3.1.3.

I set my application config timezone to UTC explicitly.

I also tried to use dm-zone-types. Did not change anything for me.

A git-bisect on my repo revealed, that this misbehavior was introduced as I switched the database to postgres from the original sqlite. This commit only changed the database.yml and the gemfile. Nothing else.

Any ideas?

kaihowl
  • 372
  • 2
  • 13
  • Have you taken a look at your Postgres database and seen what the raw database value for your datetime field is ? – Thong Kuah Feb 16 '12 at 10:59
  • I'm having the exact same problem (using 'dm-zone-types' right now). I'm on Sinatra, using Datamapper 1.2 and dm-zone-types 0.3. The time seems to be stored as UTC (according to the return value from the "dump" method in zoned_time.rb), but after retrieving it, the time zone is my local zone with the wrong time. POST: 2012-02-16 11:25:52 +0100, "CET" Value in database: 2012-02-16 10:25:52 +0000, "UTC" GET: 2012-02-16 10:25:52 +0100, "CET" – Tobias Kreß Feb 16 '12 at 10:51
  • The raw value in the database is "2012-02-17 03:46:58". That's the UTC time and the value I would like to receive. Somewhere between pulling the value from the database and sending it to the client, rails seems to do some trickery I can't quite understand. Thanks for your input! – kaihowl Feb 16 '12 at 16:22
  • I think I fixed it by setting the env var TZ to UTC [\[1\]](http://stackoverflow.com/questions/5876563/how-do-i-tell-datamapper-to-use-timestamp-with-time-zone-for-datetime-properti). But I find this solution rather hacky and feel dirty using it, especially as this is a configuration that is not straightforward to be put into version control. Any better ideas? – kaihowl Feb 16 '12 at 18:34
  • Have you tried setting Time.zone = 'UTC'? This is different to the config value you say you've set which is for how it is stored in the DB only. – toxaq Sep 30 '12 at 03:46
  • Additionally you can look here for how to completely stop rails fiddling with a date http://api.rubyonrails.org/classes/ActiveRecord/Timestamp.html – toxaq Sep 30 '12 at 03:46

1 Answers1

2

I have found a hacky solution myself:

Set the servers timezone in the environment variable TZ to 'UTC'. In order to persist this configuration option, I decided to put the environment variable setting in config/boot.rb:

ENV['TZ'] = "UTC"

I still feel dirty and this solution still gives me the willies. So any better/cleaner solution is highly appreciated!

kaihowl
  • 372
  • 2
  • 13