1

I'm using a Rails application to access a database from a legacy application. This application stores all datetime fields using Europe/Madrid timezone.

I tried to set this in my application.rb:

config.time_zone = 'Madrid'
config.active_record.default_timezone = :local

Unfortunately it didn't worked as I expected. It looks like Rails is using the timezone of my system ( America/Sao_Paulo ). So a date that is stored as:

2015-09-09 18:38:01

Will be shown with a difference of 5 hours in Rails:

2015-09-09 23:38:01

Which is the difference between São Paulo and Madrid. It should show 18:38:01 in Rails too since my app's default timezone is Madrid.

Is there a way to force the ActiveRecord timezone without changing my OS timezone?

--- Edit ---

Here is the output that I get:

2.1.3 :004 > Time.zone.name
 => "Madrid"
2.1.3 :005 > LogSent.last.sent_date
  LogSent Load (0.4ms)  SELECT  `log_sent`.* FROM `log_sent`  ORDER BY `log_sent`.`id` DESC LIMIT 1
 => Wed, 09 Sep 2015 23:38:01 CEST +02:00

I'm expecting "Wed, 09 Sep 2015 18:38:01 CEST +02:00" which is the value stored in database, in Madrid timezone.

Fernando
  • 4,459
  • 4
  • 26
  • 39
  • Check this out http://stackoverflow.com/questions/6118779/how-to-change-default-timezone-for-active-record-in-rails – NM Pennypacker Sep 25 '15 at 12:47
  • @NickM I already have that but it's not working. Check this http://pastebin.com/BQnE1G55. It should return "Wed, 09 Sep 2015 18:38:01 CEST +02:00" ( the value stored in database, in Madrid timezone ) instead of "Wed, 09 Sep 2015 23:38:01 CEST +02:00" – Fernando Sep 25 '15 at 14:18

2 Answers2

1

I know this was 3 years ago, but maybe somebody else will have the same question like I did.

When you put config.active_record.default_timezone = :local in your configuration, AR assumes that DB has local timezone, local here meaning the one on your computer. It's pretty hard to change that behaviour, you'd have to monkey-patch both ActiveRecord and ActiveModel.

Trick, however, is to convince Ruby (not Rails) that Madrid is your local time. You can do that by setting TZ environment variable to Madrid.

You can put in your config/initializers/timezone.rb the following

ENV["TZ"] = "Madrid"

Or just set the env variable before running the server as normal, and don't forget about config.active_record.default_timezone = :local.

This way all values read and written to DB will be in Madrid timezone

lobanovadik
  • 1,018
  • 8
  • 14
  • This is the correct answer when you have `datetime` fields in MySQL using a particular time zone, running in a server that is set in UTC, and you can't do anything to fix it without a major refactor in a legacy app. – cbrecabarren Sep 06 '21 at 19:08
0

How do you save sent_date attribute?

Remember that if you use DateTime.now, it will retrieve the current DateTime of your system (Sao Paulo time). Instead, you should use Time.zone.now, which will retrieve the current DateTime configured in application.rb

Daniel Batalla
  • 1,184
  • 1
  • 11
  • 22
  • It's saved by a legacy PHP application. It's not saved by Rails. Unfortunately changing the PHP application is not possible, but at least I know that the date is always in Madrid timezone. – Fernando Sep 25 '15 at 19:10