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?

6
  • 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" Commented Feb 16, 2012 at 10:51
  • Have you taken a look at your Postgres database and seen what the raw database value for your datetime field is ? Commented Feb 16, 2012 at 10:59
  • 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! Commented Feb 16, 2012 at 16:22
  • I think I fixed it by setting the env var TZ to UTC [1]. 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? Commented Feb 16, 2012 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. Commented Sep 30, 2012 at 3:46

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.