Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 找出日期范围的差距_Ruby On Rails_Datetime_Google Calendar Api - Fatal编程技术网

Ruby on rails 找出日期范围的差距

Ruby on rails 找出日期范围的差距,ruby-on-rails,datetime,google-calendar-api,Ruby On Rails,Datetime,Google Calendar Api,嗨,伙计们,我用谷歌日历为用户调出忙/闲时间表。我希望能够获取这些日期范围,然后将所有间隙输出到某种新数组中 任何指向的方向都将不胜感激。步骤1:将所有日期(作为整数)放入一个按时间排序的二维数组中,注意每个日期是时间范围的开始还是结束。例如: array = [[1484715564, 'start'], [1484715565, 'start'], [1484715569, 'end'], [1484715587, 'end'], ...] 然后,你所需要做的就是记录你是否经历了与开始一样

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


任何指向的方向都将不胜感激。

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

array = [[1484715564, 'start'], [1484715565, 'start'], [1484715569, 'end'], [1484715587, 'end'], ...]
然后,你所需要做的就是记录你是否经历了与开始一样多的
end
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
num\u start=0
间隙_开始=0
差距=[]
array.each do| date,哪个|结束|
如果哪个_end==“start”
num_开始+=1
如果num_开始==1

间隙难道你不能迭代这些天/小时,然后将空闲时间存储在一个数组中吗?是的,但我不确定如何检查重叠,因为我使用的是日期范围?