-
Notifications
You must be signed in to change notification settings - Fork 227
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
Timezone test case is broken #433
Comments
Hey @dmytro-strukov i'm not sure I understand the problem here. When i try your code the behavior is the same with timecop or without. It's not clear to me from your question why you can't use Time.utc(2024, 1, 1). I'm not sure where you're trying to use it. Your issue says "It does not change the time zone" but i'm not sure what timezone you expect to change. |
@joshuacronemeyer Let me clarify, In the test case Regarding Timecop.freeze(Time.utc(2000, 1, 1))
puts Time.now
=> 2000-01-01 04:00:00 +0400 # I suppose it should be UTC timezone, not +0400 (my current local timezone) And let's compare it with the next: utc_tz = ActiveSupport::TimeZone.find_tzinfo('UTC')
=> #<TZInfo::DataTimezone: Etc/UTC>
Timecop.freeze(Time.new(2024, 1, 1, 0, 0, 0, utc_tz))
puts Time.now
=> 2024-01-01 00:00:00 +0000 |
@dmytro-strukov Are you running these commands from an environment where Rails is included? Rails does a lot of fancy stuff with timezones so that the timezone set in configuration is respected but can be overridden for the entire thread running a request as well. I think a nice-but-kind-of-old summary of Rails view of this is https://thoughtbot.com/blog/its-about-time-zones When I run your code in a non-rails environment: Timecop.freeze(Time.utc(2000, 1, 1)) it will work as expected.
|
@joshuacronemeyer I tried in the IRB console your example. Yes, this issue occurs only in the Rails environment, I tested on a couple of projects Rails 7.x.x and Ruby 3.x, and on the oldest versions like Rails 5.x.x and Ruby 2.x |
Have you looked at using Time.current instead of Time.now? I think that is the intended way to get current time in a rails app. That method will respect all the layers of rails timezone configuration.
|
@joshuacronemeyer Yes, sure 👍 . Do you want to add info regarding this behavior into README and work on the test case? |
Yea it would be nice to have a section in the README about rails best practices. I'm not sure what you mean about the test case but i'm open to whatever you think will help make behavior clear. |
@joshuacronemeyer In method time, we have the condition to check if we pass def time(time_klass = Time) #:nodoc:
if @time.respond_to?(:in_time_zone)
time = time_klass.at(@time.dup.localtime)
else
time = time_klass.at(@time)
end In the test case, verifications seem unuseful, and very unclear, let me explain: t = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Tokyo'])
Timecop.freeze(t) do
local_now = Time.now
# => 2000-01-01 04:00:00 +0400
assert_equal t, local_now
assert_equal t.getlocal.zone, local_now.zone Ok, here I suppose we want to check that local timezone wasn't changed. Time.zone = "Hawaii"
...
zoned_now = Time.zone.now
# => 1999-12-31 14:00:00.000000000 HST -10:00
assert_equal t, zoned_now # very implicit
assert_equal 'HST', zoned_now.zone So..if we before globally set |
It does not change the time zone:
@travisjeffery @joshuacronemeyer Is it expected behavior? I wanted to use in my test cases
Time.utc(2024, 1, 1)
, but because we uselocaltime
here https://github.com/travisjeffery/timecop/blob/master/lib/timecop/time_stack_item.rb#L78 it does not workThe text was updated successfully, but these errors were encountered: