Skip to content

Instantly share code, notes, and snippets.

@jayroh
Created May 13, 2014 19:08
Show Gist options
  • Save jayroh/4bf92b410d22c5369508 to your computer and use it in GitHub Desktop.
Save jayroh/4bf92b410d22c5369508 to your computer and use it in GitHub Desktop.
time helper
# spec_helper.rb
RSpec.configure do |config|
# ...
config.include(MyWebsite::Helpers)
end
# spec/support/my_website/helpers.rb
module MyWebsite
module Helpers
def method_missing(meth, *args, &block)
matched = meth.to_s.match(/^in_(.*)_at_(.*)(am|pm)(_on_(\D*)(\d{1,2}))?$/)
old_zone = Time.zone.name
if matched
location = matched[1]
hour = matched[2].split('').insert(-3, ':').join
am_pm = matched[3]
date = matched[4]
month = matched[5]
day = matched[6]
if date.present?
date = "#{month} #{day} at "
else
date = ''
end
location_map = {
'sydney' => 'Sydney',
'london' => 'UTC',
'boston' => 'America/New_York',
'ny' => 'America/New_York',
'chicago' => 'America/Chicago',
'denver' => 'America/Denver',
'la' => 'America/Los_Angeles',
}
old_zone = Time.zone.name
Time.zone = location_map[location]
Chronic.time_class = Time.zone
this_time = Chronic.parse("#{date}#{hour}#{am_pm}").utc
if block_given?
Timecop.freeze(this_time) do
block.call
end
else
this_time
end
else
super
end
ensure
Time.zone = old_zone
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment