Last active
August 29, 2015 14:04
-
-
Save andflett/7bd0c66ea45a08368efc to your computer and use it in GitHub Desktop.
Time zone overlap
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
host = user = User.new | |
# Store in real datetime models with timezone data, convert to UTC, and base overlap on current week | |
# to mitigate the risk of leap years, timezone shifts (summer time) etc. | |
host.time_zone = "Europe/London" | |
host.availability = [ | |
ActiveSupport::TimeZone.new(host.time_zone).parse('Wednesday 8am').utc..ActiveSupport::TimeZone.new(host.time_zone).parse('Wednesday 6pm').utc, | |
ActiveSupport::TimeZone.new(host.time_zone).parse('Friday 8am').utc..ActiveSupport::TimeZone.new(host.time_zone).parse('Friday 6pm').utc, | |
] | |
user.time_zone = "America/New_York" | |
user.availability = [ | |
ActiveSupport::TimeZone.new(user.time_zone).parse('Wednesday 8am').utc..ActiveSupport::TimeZone.new(user.time_zone).parse('Wednesday 6pm').utc, | |
ActiveSupport::TimeZone.new(user.time_zone).parse('Friday 8am').utc..ActiveSupport::TimeZone.new(user.time_zone).parse('Friday 6pm').utc, | |
] | |
def has_timeslot_overlap?(host, user, start_hour, end_hour) | |
user.availability.each do |available| | |
host.availability.each do |host_available| | |
available.overlaps?(host_available) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment