SHARE
TWEET

Untitled

a guest Sep 15th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env ruby
  2.  
  3. def format_time_zone(tz)
  4.   format('%06.2f', tz.gsub(/:/, '.')).gsub(/^0/, '+').gsub(/\./, ':')
  5. end
  6.  
  7. TIMEZONES = ARGV.map { |tz| format_time_zone(tz) }.freeze
  8.  
  9. WORKING_HOURS = (7..19).freeze
  10.  
  11. def build_time(hour, zone)
  12.   Time.new(2019, 1, 1, hour, 0, 0, zone)
  13. end
  14.  
  15. def working_hours?(time)
  16.   WORKING_HOURS.include?(time.hour)
  17. end
  18.  
  19. def check_for_convenient_hours(hour)
  20.   initial_time = build_time(hour, TIMEZONES[0])
  21.   convenient_for_all = true
  22.   convenient_times = TIMEZONES.map do |tz|
  23.     local_time = initial_time.dup.localtime(tz)
  24.     is_convenient = working_hours?(local_time)
  25.     convenient_for_all &= is_convenient
  26.  
  27.     [tz, local_time.hour]
  28.   end
  29.  
  30.   convenient_times if convenient_for_all
  31. end
  32.  
  33. def print_convenient_hours(convenient_hours)
  34.   convenient_hours.reduce('') do |acc, (tz, hour)|
  35.     acc << ' ' unless acc == ''
  36.     acc << "[#{tz}]: #{format('%04.2f', hour)}"
  37.   end
  38. end
  39.  
  40. WORKING_HOURS.each do |hour|
  41.   if convenient_hours = check_for_convenient_hours(hour)
  42.     puts print_convenient_hours(convenient_hours)
  43.   end
  44. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top