Created
January 16, 2015 15:30
-
-
Save brandonbr/dd8838c22eabd7684d7f to your computer and use it in GitHub Desktop.
TIme zone with helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#in spec_helper.rb | |
require 'timezone.rb' | |
#usage | |
across_time_zones step: 2.hours do | |
#do stuff here | |
end | |
# add this file in spec | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment