Brian
Brian

Reputation: 7155

Iterating between two DateTimes, with a one hour step

I'm looking for an efficient way, in Ruby 1.9.x/Rails 3.2.x, to iterate between two DateTime objects, with a one-hour step.

('2013-01-01'.to_datetime .. '2013-02-01'.to_datetime).step(1.hour) do |date|
   ...
end

I understand that an issue with this is that 1.hour is just the number of seconds, but my attempts to convert that to a DateTime object and use that as the step doesn't work either.

I looked at "Beware of Ruby Sugar". It mentions, near the bottom, that DateTime has a direct step method. I confirmed this by running methods on a DateTime object, but I cannot find any documentation on step in DateTime, in either Ruby's or Rails' documents.

Upvotes: 45

Views: 21430

Answers (4)

Matias
Matias

Reputation: 597

Maybe late but, you can do it without Rails, for example to step with hours:

Ruby 2.1.0

require 'time' 

hour_step = (1.to_f/24)

date_time = DateTime.new(2015,4,1,00,00)
date_time_limit = DateTime.new(2015,4,1,6,00)

date_time.step(date_time_limit,hour_step).each{|e| puts e}

2015-04-01T00:00:00+00:00
2015-04-01T01:00:00+00:00
2015-04-01T02:00:00+00:00
2015-04-01T03:00:00+00:00
2015-04-01T04:00:00+00:00
2015-04-01T05:00:00+00:00
2015-04-01T06:00:00+00:00

Or minutes:

#one_minute_step = (1.to_f/24/60)

fifteen_minutes_step = (1.to_f/24/4)

date_time = DateTime.new(2015,4,1,00,00)
date_time_limit = DateTime.new(2015,4,1,00,59)

date_time.step(date_time_limit,fifteen_minutes_step).each{|e| puts e}

2015-04-01T00:00:00+00:00
2015-04-01T00:15:00+00:00
2015-04-01T00:30:00+00:00
2015-04-01T00:45:00+00:00

I hope it helps.

Upvotes: 17

Ian Hummel
Ian Hummel

Reputation: 71

Here's something I came up with recently:

require 'active_support/all'

def enumerate_hours(start, end_)
  Enumerator.new { |y| loop { y.yield start; start += 1.hour } }.take_while { |d| d < end_ }
end

enumerate_hours(DateTime.now.utc, DateTime.now.utc + 1.day)
# returns [Wed, 20 Aug 2014 21:40:46 +0000, Wed, 20 Aug 2014 22:40:46 +0000, Wed, 20 Aug 2014 23:40:46 +0000, Thu, 21 Aug 2014 00:40:46 +0000, Thu, 21 Aug 2014 01:40:46 +0000, Thu, 21 Aug 2014 02:40:46 +0000, Thu, 21 Aug 2014 03:40:46 +0000, Thu, 21 Aug 2014 04:40:46 +0000, Thu, 21 Aug 2014 05:40:46 +0000, Thu, 21 Aug 2014 06:40:46 +0000, Thu, 21 Aug 2014 07:40:46 +0000, Thu, 21 Aug 2014 08:40:46 +0000, Thu, 21 Aug 2014 09:40:46 +0000, Thu, 21 Aug 2014 10:40:46 +0000, Thu, 21 Aug 2014 11:40:46 +0000, Thu, 21 Aug 2014 12:40:46 +0000, Thu, 21 Aug 2014 13:40:46 +0000, Thu, 21 Aug 2014 14:40:46 +0000, Thu, 21 Aug 2014 15:40:46 +0000, Thu, 21 Aug 2014 16:40:46 +0000, Thu, 21 Aug 2014 17:40:46 +0000, Thu, 21 Aug 2014 18:40:46 +0000, Thu, 21 Aug 2014 19:40:46 +0000, Thu, 21 Aug 2014 20:40:46 +0000, Thu, 21 Aug 2014 21:40:46 +0000] 

Upvotes: 4

Alistair A. Israel
Alistair A. Israel

Reputation: 6567

Similar to my answer in "How do I return an array of days and hours from a range?", the trick is to use to_i to work with seconds since the epoch:

('2013-01-01'.to_datetime.to_i .. '2013-02-01'.to_datetime.to_i).step(1.hour) do |date|
  puts Time.at(date)
end

Note that Time.at() converts using your local time zone, so you may want to specify UTC by using Time.at(date).utc

Upvotes: 76

Pinkgriptape92
Pinkgriptape92

Reputation: 27

I'm not entirely sure if this will help out but check out this stack overflow page, question seems similar.

calculate difference in time in days, hours and minutes

Upvotes: -1

Related Questions