查找日期范围内的差距

时间:2017-01-18 03:25:21

标签: ruby-on-rails datetime google-calendar-api

嗨伙计我使用谷歌日历为用户提取空闲/忙碌时间表。我希望能够获取这些日期范围,然后将所有间隙输出到某种新的数组中?

任何指向的方向都会非常感激。

1 个答案:

答案 0 :(得分:1)

步骤1:将所有日期(作为整数)放入已排序(按时间)的二维数组中,注意每个日期是时间范围的开头还是结尾。例如:

array = [[1484715564, 'start'], [1484715565, 'start'], [1484715569, 'end'], [1484715587, 'end'], ...]

然后,您需要做的就是跟踪您是否经历了与end一样多的start s,如果有,请记下它!

num_starts = 0
gap_start = 0
gaps = []
array.each do |date, which_end|
  if which_end == 'start'
    num_starts += 1
    if num_starts == 1
      gaps << [gap_start, date]
    end
  else
    num_starts -= 1
    if num_starts == 0
      gap_start = date
    end
  end
end
相关问题