Skip to content

Instantly share code, notes, and snippets.

@dnagir
Last active December 16, 2019 15:37
Show Gist options
  • Save dnagir/5962765 to your computer and use it in GitHub Desktop.
Save dnagir/5962765 to your computer and use it in GitHub Desktop.
Rspec time zones sledgehammer
# spec/support/timezone.rb
module TimeZoneHelpers
extend ActiveSupport::Concern
def self.randomise_timezone!
offsets = ActiveSupport::TimeZone.all.group_by(&:formatted_offset)
zones = offsets[offsets.keys.sample] # Random offset to better vary the time zone differences
Time.zone = zones.sample # Random zone from the offset (can be just 1st, but let's do random)
puts "Current rand time zone: #{Time.zone}. Repro: Time.zone = #{Time.zone.name.inspect}"
end
module ClassMethods
def context_with_time_zone(zone, &block)
context ", in the time zone #{zone.to_s}," do
before { @prev_time_zone = Time.zone; Time.zone = zone }
after { Time.zone = @prev_time_zone }
self.instance_eval(&block)
end
end
def across_time_zones(options, &block)
options.assert_valid_keys(:step)
step_seconds = options.fetch(:step)
offsets = ActiveSupport::TimeZone.all.group_by(&:utc_offset).sort_by {|off, zones| off }
last_offset = -10.days # far enough in the past
offsets.each do |(current_offset, zones)|
if (current_offset - last_offset) >= step_seconds
last_offset = current_offset
context_with_time_zone(zones.sample, &block)
end
end
end
end
end
RSpec.configure do |config|
config.before(:suite) do
TimeZoneHelpers.randomise_timezone!
end
config.include TimeZoneHelpers
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment