Skip to content

Instantly share code, notes, and snippets.

@zinovyev
Created September 4, 2019 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zinovyev/81d308872e1a8b4f2c2120de0a36ad11 to your computer and use it in GitHub Desktop.
Save zinovyev/81d308872e1a8b4f2c2120de0a36ad11 to your computer and use it in GitHub Desktop.
Find out wich hours are convenient for the meeting for different people working from the defferent timezones: `ruby convenient_hours.rb +2 +3 +8`
#! /usr/bin/env ruby
def format_time_zone(tz)
format('%06.2f', tz.gsub(/:/, '.')).gsub(/^0/, '+').gsub(/\./, ':')
end
TIMEZONES = ARGV.map { |tz| format_time_zone(tz) }.freeze
WORKING_HOURS = (7..19).freeze
def build_time(hour, zone)
Time.new(2019, 1, 1, hour, 0, 0, zone)
end
def working_hours?(time)
WORKING_HOURS.include?(time.hour)
end
def check_for_convenient_hours(hour)
initial_time = build_time(hour, TIMEZONES[0])
convenient_for_all = true
convenient_times = TIMEZONES.map do |tz|
local_time = initial_time.dup.localtime(tz)
is_convenient = working_hours?(local_time)
convenient_for_all &= is_convenient
[tz, local_time.hour]
end
convenient_times if convenient_for_all
end
def print_convenient_hours(convenient_hours)
convenient_hours.reduce('') do |acc, (tz, hour)|
acc << ' ' unless acc == ''
acc << "[#{tz}]: #{format('%04.2f', hour)}"
end
end
WORKING_HOURS.each do |hour|
if convenient_hours = check_for_convenient_hours(hour)
puts print_convenient_hours(convenient_hours)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment