Time zone bug for :datetime attributes?

I’ve noticed a time zone bug in the following code…

https://github.com/rails/rails/blob/f33d52c95217212cbacc8d5e44b5a8e3cdc6f5b3/activemodel/lib/active_model/type/helpers/time_value.rb#L59

That’s creating a new time with either :utc or :local. The issue is when using :local, that creates a new time based on the systems local time zone, and ignores the time set in the app via config.time_zone = ‘Central Time (US & Canada)’

So if the app is set for CST (via config.time_zone), but the local system time zone in EST, the time will be created for EST even if the app is set for CST.

So in code such as

class FormObject
include ActiveModel::  Model
include ActiveModel::  Attributes
include ActiveRecord::  AttributeAssignment
attribute :received_at, :datetime
end

obj = FormObject.new received_at: '2020-01-02 00:50'
obj.received_at # 2020-01-02 00:50:00 -0500 => should be 2020-01-02 00:50:00 -0600

The resulting time is wrong because the time zone set in the app is being ignored.

Any reason this would be occurring the way it is or is it a bug?

Regards,

Andrew

1 Like