提取轨道中两个时间范围之间的空闲时间

时间:2019-03-02 03:10:22

标签: ruby-on-rails

我需要帮助!我的排程应用程式有这种逻辑问题。给定8AM到12PM之间的时间,并且为9 AM-10AM设置了时间表,如何提取8 AM-9AM和10 AM-12PM的空闲时间?

代码如下:

class VacantTimeFinder
  def initialize(args={})
    @schedules = args[:schedules]
    @eight_am = DateTime.new(2000,1,1,8,0,0,DateTime.now.zone)
    @twelve_pm = DateTime.new(2000,1,1,12,0,0,DateTime.now.zone)
  end

  def find!
    vacant_times = []
    @schedules.each do |schedule|
      starts_at = schedule.starts_at
      ends_at   = schedule.ends_at
      if (starts_at..ends_at).overlaps?(@eight_am..@twelve_am)
        #logic goes here...
      end
    end
    vacant_times #formated as ["8AM - 9AM", "10AM - 12PM"]
  end
end

否则将不胜感激。

1 个答案:

答案 0 :(得分:0)

类似的东西:

result = []
e = @eight
schedules.sort.each do |s|
  result << [e, s.start]
  e = s.end
end
result << [e, @twelve]
result