Sergio
Sergio

Reputation: 272

Rails: Set config.time_zone to the server's time zone?

For compatibility reasons I need to store and display all dates in the server's local time zone. To do this I configured the rails application like this in application.rb:

# Do not convert database DATETIME / DATE columns to UTC:
config.active_record.default_timezone = :local

It works okay when I store dates they are stored in the server's local timezone. However when retrieving the data Rails converts it to UTC, so for example Post.find(1).posted returns 2016-02-02 18:06:48 UTC but it is actually stored as 2016-02-02 13:06:48 in the database.

How can I keep Rails from converting it to UTC?

I thought that setting the application time zone to the server's local time zone will help so I wanted to set config.time_zone to the server's local time zone and tried with:

config.time_zone = Time.now.zone

But the problem with it is that it returns a three character timezone code like COT and config.time_zone is expecting a Time Zone name like Bogota

How can I translate the three character code to the full time zone name? or, how can I get the local time zone name? or, how can I configure Rails to not convert the database dates to UTC?

Upvotes: 5

Views: 10341

Answers (3)

februaryInk
februaryInk

Reputation: 725

I needed to do something similar for testing. Some of our tests relied on the days selected in a browser calendar, but late at night the server browser and the Rails backend were on different days due to time zone settings.

I ended up using the following:

config/environments/test.rb

...

config.time_zone = ActiveSupport::TimeZone[Time.now.strftime('%z').gsub('0', '').to_i]

...

This grabs the offset of the server time zone as an integer number of hours and calculates the configured time zone based on that. This has limited usefulness, as there are multiple time zones per offset. But as I only needed the times to match for testing, it was adequate for my use case.

It would be more helpful if the time zone could be derived from the server time zone short code (Time.now.strftime('%Z')), but I didn't find a solution for that.

Upvotes: 2

smathy
smathy

Reputation: 27961

Just enter "Bogota" as the string for config.time_zone and with config.active_record.default_timezone set to :local you should get what you need. DB in local timezone (which, I hope, is Bogata's TZ) and the model attributes in Bogata's TZ too.

Update

Based on a comment, the OP wants to be able to set the timezone in Ubuntu at an OS level and then use that setting to configure Rails without any additional step.

So @Sergio, the good news is that you can set config.time_zone to the IANA timezone name that you'd be familiar with in using the Ubuntu tools, ie. you can do:

config.time_zone = "America/Bogota"

So all you need to do is work out how to get that from your OS, I think it should be the contents of /etc/timezone so:

config.time_zone = File.read("/etc/timezone").chomp

Upvotes: 8

Leonel Galán
Leonel Galán

Reputation: 7167

What about Time.zone.tzinfo.identifier, for me that's "America/New_York". I use it on Postgres, but I suspect that would give you the string you expect.

Upvotes: 1

Related Questions