37

in-rails-prefer-use_zone-to-zone

 4 years ago
source link: https://github.com/tamouse/swaac/blob/master/posts/2020/01/in-rails-prefer-use_zone-to-zone.md
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Rails better practice for dealing with altering the current process's timezone

In Rails, Prefer `.use_zone` to `.zone=`

I had thought to write a post on this, but never got around to it.

Prathamesh Sonpatki beat me to it :)

Essentially, this is the practice I use:

Time.use_zone(current_user.timezone) do 
  # Do Time and DateTime operations under the auspices of the
  # current user's timezone setting.
  
  Time.current # The current time in the current user's timezone
  Time.zone.now # Equivalent to the above
end

It's better than just supposing that Time.zone has been set somewhere in the current process, or that Time.zone has been set to what the code in the block expects it to be!

In Testing

The biggest source of error that I've encountered for this isn't in requests, but actually in the spec test cases for the product. Some test cases blythely set Time.zone in a before action, but never reset it in an after action.

If possible, wrap the time zone use inside an around action:

around(:each) do |example|
  Time.use_zone(some_timezone) do
    yield example
  end
end

With Timecop

Since Timecop is essentially mocking the system clock, you can use it with .use_zone with impunity.

around do |example|
  Timecop.freeze do
    @account = Fabricate(:account)
    Time.use_zone(account.timezone) do
      yield example
    end
  end
end

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK