似乎在红宝石 DateTime 范围内只有 1 天的粒度。
now = DateTime.new(2013,1,1)
#=> #<DateTime: 2013-01-01T00:00:00+00:00 ((2456294j,0s,0n),+0s,2299161j)>
two_days_later = DateTime.new(2013,1,3)
#=> #<DateTime: 2013-01-03T00:00:00+00:00 ((2456296j,0s,0n),+0s,2299161j)>
range = (now..two_days_later)
#=> #<DateTime: 2013-01-01T00:00:00+00:00 ((2456294j,0s,0n),+0s,2299161j)>..#<DateTime: 2013-01-03T00:00:00+00:00 ((2456296j,0s,0n),+0s,2299161j)>
tomorrow = DateTime.new(2013,1,2)
#=> #<DateTime: 2013-01-02T00:00:00+00:00 ((2456295j,0s,0n),+0s,2299161j)>
range.include?(tomorrow)
#=> true
later_this_morning = now + 0.1
#=> #<DateTime: 2013-01-01T02:24:00+00:00 ((2456294j,8640s,0n),+0s,2299161j)>
range.include? later_this_morning
#=> false
# but .....
range.max
#<DateTime: 2013-01-03T00:00:00+00:00 ((2456296j,0s,0n),+0s,2299161j)>
所需的行为是它只会排除日期范围的最后一秒,以便最大值为2013-01-02T23:59:59
,并且今天或明天的任何 DateTime 都将包含在该范围内。
有没有办法改变 DateTime 范围的粒度?