Search code examples
ruby-on-rails-4timezonelocaltimetzinfo

Rails TZInfo incorrect offset for Moscow local time


I have set the Rails time zone to 'Moscow' in application.rb.

When I ask for the local time it returns a value that is one hour ahead of the actual local time.

How can I workaround that incorrect time to local conversion?

2.0.0-p481 :003 > Time.zone
 => #<ActiveSupport::TimeZone:0x000000058a5750 @name="Moscow", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Europe/Moscow>, @current_period=#<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionDefinition: #<TZInfo::TimeOrDateTime: 1301180400>,#<TZInfo::TimezoneOffset: 14400,0,MSK>>,nil>>
2.0.0-p481 :004 > Time.now
 => 2015-09-14 10:37:39 +0000
2.0.0-p481 :005 > Time.current
 => Mon, 14 Sep 2015 14:37:44 MSK +04:00
2.0.0-p481 :006 > tz = TZInfo::Timezone.get('Europe/Moscow')
 => #<TZInfo::DataTimezone: Europe/Moscow>

Here Time.current should return Mon, 14 Sep 2015 13:37:44 MSK +03:00

PS

Rails.version == '4.2.1'


Solution

  • It looks like you are using an out of date copy of IANA Time Zone database. The Europe/Russia time zone set the clocks back from +04:00 to +03:00 permanently in October 2014. This change was included in the 2014f release of the Time Zone database.

    Rails (via TZInfo) will either be obtaining time zone data from your system's zoneinfo directory or from the tzinfo-data gem. You can check which data source is being used by running:

    TZInfo::DataSource.get.to_s
    

    If this returns "Zoneinfo DataSource: /path/to/zoneinfo" then out of date data is being read from the named directory. You'll probably be able to update your time zone data by installing an update from your operating system distributor.

    If DataSource.get returns "Ruby DataSource", then you are using an old version of the tzinfo-data gem (prior to version 1.2014.6). To update, remove any version constraints on the gem 'tzinfo-data' line in your Gemfile and then run bundle update.

    If you are using your system zoneinfo directory, but cannot update it, then you can switch to using the tzinfo-data gem by adding gem 'tzinfo-data' to your Gemfile and running bundle install.