require 'date'
# dummy dates
dates = "2010-07-17 16:09:00 -0700
2010-07-17 16:10:00 -0700
2010-07-17 16:12:00 -0700
2010-07-17 17:44:00 -0700
2010-07-18 02:12:00 -0700"
datetimes = dates.lines.map{|s| DateTime.parse(s) }
continuous_datetimes = [datetimes.min]
while continuous_datetimes.last < datetimes.max
continuous_datetimes.push( continuous_datetimes.last + 1.0/24.0/60.0 )
end
continuous_datetimes.first(10).each{|d| puts d}
# 2010-07-17T16:09:00-07:00
# 2010-07-17T16:10:00-07:00
# 2010-07-17T16:11:00-07:00
# 2010-07-17T16:12:00-07:00
# 2010-07-17T16:13:00-07:00
# 2010-07-17T16:14:00-07:00
# 2010-07-17T16:15:00-07:00
# 2010-07-17T16:16:00-07:00
# 2010-07-17T16:17:00-07:00
# 2010-07-17T16:18:00-07:00
编辑
以下代码反映了您的哈希结构。
设置:
require 'date'
# dummy dates
hash = {
DateTime.parse("2010-07-17 16:09:00 -0700") => 5,
DateTime.parse("2010-07-17 16:10:00 -0700") => 22,
DateTime.parse("2010-07-17 16:12:00 -0700") => 3
}
实际代码:
new_hash = hash.inject(Hash[ *hash.first ]) do |tmp,kv|
while tmp.keys.last < kv.first
tmp[ tmp.keys.last + 1.0/24.0/60.0 ] = kv.last
end
tmp
end
输出:
new_hash.each {|k,v| puts "#{k} => #{v}" }
# 2010-07-17T16:09:00-07:00 => 5
# 2010-07-17T16:10:00-07:00 => 22
# 2010-07-17T16:11:00-07:00 => 3
# 2010-07-17T16:12:00-07:00 => 3