-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix rails 7 deprecation on ActiveRecord::Base.default_timezone #124
Fix rails 7 deprecation on ActiveRecord::Base.default_timezone #124
Conversation
if default_timezone_utc? | ||
Time.now.utc | ||
else | ||
Time.zone.now | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm if the default_timezone is UTC then aren't Time.now.utc
and Time.zone.now
the same thing? Could we simplify this to just Time.zone.now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooh, I hear you saying, can we delete this?
irb(main):009:0> Time.zone.now.class
=> ActiveSupport::TimeWithZone
irb(main):010:0> Time.now.utc.class
=> Time
Let me see if there is a reason it was written like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah if we're concerned about Time
vs ActiveSupport::TimeWithZone
we could always Time.zone.now.to_time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was localtime originally... maybe it's not needed at all.
❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ ✂️
We use ActiveSupport's time extension. If the zone has been set, such as utc, we'll use that, otherwise, it will use Time.now, which will be local.
400138d
to
8e374a3
Compare
Checked commit jrafanie@8e374a3 with ruby 2.7.8, rubocop 1.56.3, haml-lint 0.51.0, and yamllint |
else | ||
Time.zone.now | ||
end | ||
Time.current |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@agrare this handles if Time.zone is not set (nil) and returns Time.now which should be localtime if not set. If set to utc, it will return utc time via Time.zone.now.
Rails 7 added ActiveRecord.default_timezone and 7.1 will remove it from ActiveRecord::Base.
Extract a memoized method to hide this logic.
Fixes this deprecation in the 7.0 test suite: