Iterating between two DateTimes, with a one hour step

前端 未结 4 2060
轻奢々
轻奢々 2020-12-13 06:40

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 .. \         


        
相关标签:
4条回答
  • 2020-12-13 06:51

    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

    0 讨论(0)
  • 2020-12-13 06:52

    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] 
    
    0 讨论(0)
  • 2020-12-13 07:13

    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.

    0 讨论(0)
  • 2020-12-13 07:14

    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

    0 讨论(0)
提交回复
热议问题