Created
May 30, 2015 00:30
-
-
Save bradpauly/2f62a4cf118baa99d75e to your computer and use it in GitHub Desktop.
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
require 'TZInfo' | |
class TimeZoneSearch | |
def initialize(zones_to_search) | |
@zones_to_search = zones_to_search | |
end | |
def zones_with_local_hour(hour_to_match, utc_time = Time.now.utc.to_datetime) | |
zone_matches = [] | |
utc_minute = utc_time.minute | |
# Round to the closest 15 minute mark. | |
minute_to_check = utc_minute - (utc_minute % 15) | |
# UTC time to the closest 15 minute mark excluding seconds. | |
utc_time_to_check = Time.utc(utc_time.year, utc_time.month, utc_time.day, utc_time.hour, minute_to_check, 0) | |
@zones_to_search.each do |zone_name| | |
local_zone = time_zone(zone_name) | |
local_time = local_zone.utc_to_local(utc_time_to_check).to_datetime | |
# Check for 0 minute because offset minutes are multiples of 15. | |
if local_time.minute == 0 && local_time.hour == hour_to_match | |
zone_matches << local_zone.identifier | |
end | |
end | |
zone_matches | |
end | |
def time_zone(name) | |
TZInfo::Timezone.get(name) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment