Time zone change on DateTime method (Rails 4.2.8)

255 Views Asked by At

I am trying to upgrade from rails 4.2.7 to 4.2.8 and found a case where the DateTime method of rails switches the time zone to local on reload from database which was not seen in rails 4.2.7. On rails 4.2.8, the unit test of my app rely on the DateTime method to get beginning of the year,

DateTime.new(2019,1,1,0,0,0)
Tue, 01 Jan 2019 00:00:00 +0000

This resultant value, is passed as part of an object to,
Class:Machinist::ActiveRecord::Blueprint where the reload happens.

def reload(options = nil)
  clear_aggregation_cache
  clear_association_cache
  self.class.connection.clear_query_cache

  fresh_object =
    if options && options[:lock]
      self.class.unscoped { self.class.lock(options[:lock]).find(id) }
    else
      self.class.unscoped { self.class.find(id) }
    end

  @attributes = fresh_object.instance_variable_get('@attributes')
  @new_record = false
  self
end

The instance_variable_get method at,

@attributes = fresh_object.instance_variable_get('@attributes')

switches the time zone on Rails 4.2.8 to local(Mon, 31st Dec 2018 16:00:00 -0800) but not on 4.2.7 where just the time zone is switched and not the time (Tue, 1st Jan 2019 00:00:00 -0800). A similar method, Time.new() has got no problem with this as the time zone is set to local by default,

Time.new(2019,1,1,0,0,0)
2019-01-01 00:00:00 -0800

Trying to pinpoint what changed on rails 4.2.8 that doesn't let the time zones persist to UTC for the DateTime method on the reload. Any insights on this are helpful. Thanks

0

There are 0 best solutions below