Skip to content

Instantly share code, notes, and snippets.

@ohnishiakira
Created October 10, 2013 12:24
Show Gist options
  • Save ohnishiakira/6917488 to your computer and use it in GitHub Desktop.
Save ohnishiakira/6917488 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# coding: utf-8
require 'pp'
require 'date'
def each_by_time(a, b)
min_count = ((b - a) * 24*60).to_i + 1
min_count.times.map{|t|
a + Rational(t, 24*60)
}
end
a = DateTime.new 2013, 10, 10, 12, 0, 0
b = DateTime.new 2013, 10, 10, 12, 10, 0
pp each_by_time(a, b)
# =>
# [#<DateTime: 2013-10-10T12:00:00+00:00 ((2456576j,43200s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:01:00+00:00 ((2456576j,43260s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:02:00+00:00 ((2456576j,43320s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:03:00+00:00 ((2456576j,43380s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:04:00+00:00 ((2456576j,43440s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:05:00+00:00 ((2456576j,43500s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:06:00+00:00 ((2456576j,43560s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:07:00+00:00 ((2456576j,43620s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:08:00+00:00 ((2456576j,43680s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:09:00+00:00 ((2456576j,43740s,0n),+0s,2299161j)>,
# #<DateTime: 2013-10-10T12:10:00+00:00 ((2456576j,43800s,0n),+0s,2299161j)>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment