0

At 10 a.m. an object becomes active and visible to the user.
seconds_since_midnight.to_i at 10:00 is 36000, but that is not timezone aware.
application_controller sets a tenant and its timezone, based on one of its attributes

before_action :set_host_shop_timezone

def set_host_shop_timezone
  [...]
  @shop = Shop.find(@site.shop_id)
  Time.zone = @shop.timezone
end

The local (server) is set at GMT +1, while application.rb does not specify any # config.time_zone = 'UTC'

Assertions are needed for:

travel_to Time.zone.local(2025, 8, 8, 9, 59, 58)
travel_to Time.zone.local(2025, 8, 8, 10, 01, 59)

Console confirms:

Time.zone.local(2025, 8, 8, 9, 59, 58).seconds_since_midnight.to_i
=> 35998
Time.zone.local(2025, 8, 8, 10, 01, 59).seconds_since_midnight.to_i
=> 36119

The tests

  test 'time before trip-wire ' do
    @shop = shops(:one)
    travel_to Time.zone.local(2025, 8, 8, 9, 59, 58)
    get root_path, headers: { "host": 'www.one.com' }
    puts Time.zone
    puts Time.zone.local(2025, 8, 8, 9, 59, 58).seconds_since_midnight.to_i
  end

  test 'time after trip-wire ' do
    @shop = shops(:one)
    travel_to Time.zone.local(2025, 8, 8, 10, 00, 12)
    get root_path, headers: { "host": 'www.one.com' }
    puts Time.zone
    puts Time.zone.local(2025, 8, 8, 10, 00, 12).seconds_since_midnight.to_i
  end

return expected results for timezone and seconds since midnight

(GMT+01:00) CET
35998
[...]
(GMT+01:00) CET
36659

but the response.body is wrong in the test (but not in the browser view).
The root_path action intercepts

 puts Time.now
 puts Time.now.seconds_since_midnight.to_i

and returns for each test:

2025-08-08 11:59:58 +0200
43198
[...]
2025-08-08 12:00:12 +0200
43212

with the offset being 2 hours (GMT + summer time).

Where is this test mistaken?

1 Answer 1

0

Referring to the documentation, the problem can be ascertained through toggling of configuration and adjusting the test.

puts Time.current
    travel_to Time.zone.local(2025, 8, 8, 9, 59, 58)
puts Time.current

setting config.time_zone = 'Rome' the values can be confusing, particularly since data should be stored in UTC format:

2025-11-23 18:36:01 +0100
2025-08-08 09:59:58 +0200

best remain with default # config.time_zone = 'UTC'. The test will show:

2025-11-23 18:02:37 UTC
2025-08-08 09:59:58 UTC

Thus the issue is travel_to travels to the time of the application's timezone, not that of the tenant.

setting the timezone in the application controller is moot . The instruction is pointless clutter and be removed # Time.zone = @shop.timezone. What matters is that the logic take into account the offset to UTC. Thus two travels are required:

  puts Time.current
travel_to Time.zone.local(2025, 8, 8, 9, 59, 58)
  puts Time.current
offset = 0 - Time.now.utc_offset
  puts offset
travel offset.seconds
  puts Time.current

utc_offset must be set to negative: Berlin? ahead => positive utc_offset, need to subtract. New York? behind => negative utc_offset, need to add.
The result is what is needed to conduct the test:

2025-11-23 19:02:26 UTC
2025-08-08 09:59:58 UTC
-7200
2025-08-08 07:59:58 UTC

Two travels:

travel_to Time.zone.local(2025, 8, 8, 10, 00, 12)
offset = 0 - Time.now.utc_offset
travel offset.seconds

Recommended testing: play around with different fixture values for @shop.timezone & dates that straddle summer & winter time creating gap weeks (i.e. NAmerica & Europe in late march and october).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.