Reputation: 4840
How to: iterate over table rows where each row is labeled by a time. First row would be start time, last row would be end time, and the iteration should create every row in between at 15 minute intervals.
start_time: '06:00', end_time: '07:00'
06:00
06:15
06:30
06:45
07:00
start_time = Time.local(2013, 5, 25, 06, 00)
end_time = Time.local(2013, 5, 25, 20, 00)
begin
start_time += 15.minutes
puts start_time
end while start_time < end_time
This returns nil...but, shouldn't...it should be returning values
Upvotes: 1
Views: 1366
Reputation: 4840
Here is what I came up with that does just what I need. Inspired by jethroos answer.
def cal_times
start_time = Time.local(2013, 5, 25, 06, 00)
end_time = Time.local(2013, 5, 25, 20, 00)
times = [start_time.strftime('%H:%M')]
begin
start_time += 15.minutes
times << start_time.strftime('%H:%M')
end while start_time < end_time
times
end
Upvotes: 1
Reputation: 2124
i'm not sure if i understand the question right, but here is an example how to generate times in 15min intervalls
1.9.3p392 :029 > a = Time.now
=> 2013-05-25 23:39:44 +0200
1.9.3p392 :030 > a = a - a.sec - a.min%15*60
=> 2013-05-25 23:30:00 +0200
1.9.3p392 :031 > b = a + 1.hour
=> 2013-05-26 00:30:00 +0200
1.9.3p392 :032 > begin
1.9.3p392 :033 > a += 15.minutes
1.9.3p392 :034?> puts a
1.9.3p392 :035?> end while a < b
2013-05-25 23:45:00 +0200
2013-05-26 00:00:00 +0200
2013-05-26 00:15:00 +0200
2013-05-26 00:30:00 +0200
maybe this helps a bit
Upvotes: 0