how can i get a list of DateTime objects which match the following criteria:
- they are between two instances of DateTime
- they fall on a fraction of an hour/min. eg. they are a full quarter of an hour
with active support a possible solution is:
(my_datetime_ob_a.to_i .. my_datetime_ob_b.to_i).each { |timestamp|
puts timestamp if (timestamp % (3600/4) == 0)
}
this is not nice: too many iterations and too much conver开发者_StackOverflow中文版sion (you would need to reconvert timestamp to a DateTime object.
If you're running 1.9, you can use Date#step
in following manner:
require 'date'
t1 = DateTime.now
t2 = t1+3 # three days from now
# 0.25 being 1/4 of a day - 6 hours
p t1.step(t2, 0.25).to_a
#=>[#<DateTime: 2010-07-24T21:49:38+02:00 (7857287443636107/3200000000,1/12,2299161)>, #<DateTime: 2010-07-25T03:49:38+02:00 (2455402.57613628,1/12,2299161)>, #<DateTime: 2010-07-25T09:49:38+02:00 (2455402.82613628,1/12,2299161)>, #<DateTime: 2010-07-25T15:49:38+02:00 (2455403.07613628,1/12,2299161)>, #<DateTime: 2010-07-25T21:49:38+02:00 (2455403.32613628,1/12,2299161)>, #<DateTime: 2010-07-26T03:49:38+02:00 (2455403.57613628,1/12,2299161)>, #<DateTime: 2010-07-26T09:49:38+02:00 (2455403.82613628,1/12,2299161)>, #<DateTime: 2010-07-26T15:49:38+02:00 (2455404.07613628,1/12,2299161)>, #<DateTime: 2010-07-26T21:49:38+02:00 (2455404.32613628,1/12,2299161)>, #<DateTime: 2010-07-27T03:49:38+02:00 (2455404.57613628,1/12,2299161)>, #<DateTime: 2010-07-27T09:49:38+02:00 (2455404.82613628,1/12,2299161)>, #<DateTime: 2010-07-27T15:49:38+02:00 (2455405.07613628,1/12,2299161)>, #<DateTime: 2010-07-27T21:49:38+02:00 (2455405.32613628,1/12,2299161)>]
Here is an alternative approach:
- Find the first timestamp >=
my_datetime_ob_a
which is a full quarter of an hour (i.e. round up to 15 minute interval) - While this timestamp is <=
my_datetime_ob_b
put it in your list, add 15 minutes and loop
Implementation:
def round_to_quarter_hour(datetime)
secs_past_quarter_hour = datetime.to_i % 15.minutes
if secs_past_quarter_hour == 0
datetime
else
secs_to_advance_to_next_quarter_hour = 15.minutes -
secs_past_quarter_hour
datetime.advance :seconds => secs_to_advance_to_next_quarter_hour
end
end
def quarter_hours_between(datetime_a, datetime_b)
results = []
datetime = round_to_quarter_hour(datetime_a)
while datetime <= datetime_b
results << datetime
datetime = datetime.advance :minutes => 15
end
results
end
Example:
>> d1
=> Sat, 24 Jul 2010 17:28:06 +0100
>> d2
=> Sat, 24 Jul 2010 18:03:00 +0100
>> quarter_hours_between(d1, d2)
=> [Sat, 24 Jul 2010 17:30:00 +0100, Sat, 24 Jul 2010 17:45:00 +0100,
Sat, 24 Jul 2010 18:00:00 +0100]
精彩评论