From c301095a65a96993ce42dedc67fc2a95f37aac92 Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Fri, 15 Mar 2024 09:59:22 -0700 Subject: [PATCH 1/7] failing test: ActiveSupport Date.current behavior with Time.zone set to UTC I suspect https://github.com/travisjeffery/timecop/blob/e134761e942c612f8199f2d83793995653f810cb/lib/timecop/time_stack_item.rb#L59 calling localtime to be causing this issue. My system localtime is -7, so this cast is changing the date to the previous day. --- test/active_support_date_current.rb | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test/active_support_date_current.rb diff --git a/test/active_support_date_current.rb b/test/active_support_date_current.rb new file mode 100644 index 0000000..a5c1d00 --- /dev/null +++ b/test/active_support_date_current.rb @@ -0,0 +1,30 @@ +require_relative "test_helper" +require 'timecop' +require 'active_support' +require 'active_support/core_ext/date/calculations' + +class TestActiveSupportDateCurrent < Minitest::Test + def test_date_current_same_as_date_today + old_zone = Time.zone + begin + Time.zone = nil + Timecop.freeze("2024-03-15") do + assert_equal Date.current, Date.today + end + ensure + Time.zone = old_zone + end + end + + def test_date_current_same_as_date_today_with_time_zone + old_zone = Time.zone + begin + Time.zone = "UTC" + Timecop.freeze("2024-03-15") do + assert_equal Date.current, Date.today + end + ensure + Time.zone = old_zone + end + end +end \ No newline at end of file From d83f2bdfe89b0b8d8ecd679ad433a8ea8a829479 Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Wed, 14 Aug 2024 09:56:34 -0700 Subject: [PATCH 2/7] add _test suffix to pick up from rake test --- ...upport_date_current.rb => active_support_date_current_test.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/{active_support_date_current.rb => active_support_date_current_test.rb} (100%) diff --git a/test/active_support_date_current.rb b/test/active_support_date_current_test.rb similarity index 100% rename from test/active_support_date_current.rb rename to test/active_support_date_current_test.rb From a4f13157761249350b6846f2173e9d067ec99442 Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Wed, 14 Aug 2024 10:08:48 -0700 Subject: [PATCH 3/7] rm passing test Focus only on the failing test case with zone --- test/active_support_date_current_test.rb | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/active_support_date_current_test.rb b/test/active_support_date_current_test.rb index a5c1d00..916b350 100644 --- a/test/active_support_date_current_test.rb +++ b/test/active_support_date_current_test.rb @@ -4,18 +4,6 @@ require 'active_support/core_ext/date/calculations' class TestActiveSupportDateCurrent < Minitest::Test - def test_date_current_same_as_date_today - old_zone = Time.zone - begin - Time.zone = nil - Timecop.freeze("2024-03-15") do - assert_equal Date.current, Date.today - end - ensure - Time.zone = old_zone - end - end - def test_date_current_same_as_date_today_with_time_zone old_zone = Time.zone begin From 603b64dded557e703408c09499f99c015e7e6b99 Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Wed, 14 Aug 2024 12:22:03 -0700 Subject: [PATCH 4/7] cast date to Time.zone Fixes failing test test_date_current_same_as_date_today_with_time_zone --- lib/timecop/time_stack_item.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/timecop/time_stack_item.rb b/lib/timecop/time_stack_item.rb index ef49150..0767f71 100644 --- a/lib/timecop/time_stack_item.rb +++ b/lib/timecop/time_stack_item.rb @@ -96,7 +96,9 @@ def scaled_time end def date(date_klass = Date) - date_klass.jd(time.__send__(:to_date).jd) + t = time + t = t.respond_to?(:in_time_zone) ? t.in_time_zone : t + date_klass.jd(t.__send__(:to_date).jd) end def datetime(datetime_klass = DateTime) From f85bd8de2ceac64adba24f975f0b17e4e7193896 Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Thu, 15 Aug 2024 12:31:11 -0700 Subject: [PATCH 5/7] Revert "cast date to Time.zone" This reverts commit 603b64dded557e703408c09499f99c015e7e6b99. --- lib/timecop/time_stack_item.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/timecop/time_stack_item.rb b/lib/timecop/time_stack_item.rb index 0767f71..ef49150 100644 --- a/lib/timecop/time_stack_item.rb +++ b/lib/timecop/time_stack_item.rb @@ -96,9 +96,7 @@ def scaled_time end def date(date_klass = Date) - t = time - t = t.respond_to?(:in_time_zone) ? t.in_time_zone : t - date_klass.jd(t.__send__(:to_date).jd) + date_klass.jd(time.__send__(:to_date).jd) end def datetime(datetime_klass = DateTime) From adf5ef7cdc784dfcf3533101d1b84af168d36af0 Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Thu, 15 Aug 2024 12:35:44 -0700 Subject: [PATCH 6/7] debug ci --- test/active_support_date_current_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/active_support_date_current_test.rb b/test/active_support_date_current_test.rb index 916b350..6952e9f 100644 --- a/test/active_support_date_current_test.rb +++ b/test/active_support_date_current_test.rb @@ -5,10 +5,19 @@ class TestActiveSupportDateCurrent < Minitest::Test def test_date_current_same_as_date_today_with_time_zone + puts "Before Timecop.freeze" + puts "ENV['TZ']: #{ENV['TZ']}" + puts "Time.zone: #{Time.zone}" + puts + old_zone = Time.zone begin Time.zone = "UTC" Timecop.freeze("2024-03-15") do + puts "Within Timecop.freeze" + puts "ENV['TZ']: #{ENV['TZ']}" + puts "Time.zone: #{Time.zone}" + assert_equal Date.current, Date.today end ensure From dcdab1fbeea183575481b2fa05a03f1f4022d603 Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Thu, 15 Aug 2024 20:00:22 +0000 Subject: [PATCH 7/7] set ENV['TZ'] system time to differ from Time.zone --- test/active_support_date_current_test.rb | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/test/active_support_date_current_test.rb b/test/active_support_date_current_test.rb index 6952e9f..0f01532 100644 --- a/test/active_support_date_current_test.rb +++ b/test/active_support_date_current_test.rb @@ -5,23 +5,11 @@ class TestActiveSupportDateCurrent < Minitest::Test def test_date_current_same_as_date_today_with_time_zone - puts "Before Timecop.freeze" - puts "ENV['TZ']: #{ENV['TZ']}" - puts "Time.zone: #{Time.zone}" - puts - - old_zone = Time.zone - begin + each_timezone do Time.zone = "UTC" Timecop.freeze("2024-03-15") do - puts "Within Timecop.freeze" - puts "ENV['TZ']: #{ENV['TZ']}" - puts "Time.zone: #{Time.zone}" - assert_equal Date.current, Date.today end - ensure - Time.zone = old_zone end end end \ No newline at end of file