Project

General

Profile

Actions

Patch #35208

closed

Use `Time.use_zone` instead of `Time.zone=`

Added by Go MAEDA over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Code cleanup/refactoring
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:

Description

When temporarily overriding a time zone, it is safer to use Time.use_zone than to update Time.zone directly because Time.use_zone will reset the time zone to its original value after the execution of the given block is done.

And RuboCop Rails 2.10.0 will display Rails/TimeZoneAssignment warning when it detects code that directly updates Time.zone.

Index: test/unit/mailer_test.rb
===================================================================
--- test/unit/mailer_test.rb    (リビジョン 20961)
+++ test/unit/mailer_test.rb    (作業コピー)
@@ -386,11 +386,10 @@
     issue = Issue.find(3)
     user = User.find(1)
     %w(UTC Paris Tokyo).each do |zone|
-      Time.zone = zone
-      assert_match /^redmine\.issue-3\.20060719190727\.1@example\.net/, Mailer.token_for(issue, user)
+      Time.use_zone(zone) do
+        assert_match /^redmine\.issue-3\.20060719190727\.1@example\.net/, Mailer.token_for(issue, user)
+      end
     end
-  ensure
-    Time.zone = zone_was
   end

   test "#issue_add should notify project members" do

Related issues

Related to Redmine - Patch #35207: Update RuboCop Rails to 2.14ClosedGo MAEDA

Actions
Actions #1

Updated by Go MAEDA over 3 years ago

  • Related to Patch #35207: Update RuboCop Rails to 2.14 added
Actions #2

Updated by Go MAEDA over 3 years ago

  • Status changed from New to Closed
  • Assignee set to Go MAEDA
  • Target version set to 5.0.0

Committed the patch.

Actions

Also available in: Atom PDF