Range#=3D=3D=3D , need #succ method in Ruby1.9.3
under  only #<=3D>,  use Range#cover?

2012/1/18 Peter Vandenabeele <peter / vandenabeele.com>:
> On Tue, Jan 17, 2012 at 6:12 PM, Christopher Graves <gravescl / gmail.com>w=
rote:
>
>> Peter Vandenabeele wrote in post #1041349:
>> > On Tue, Jan 17, 2012 at 4:37 PM, Peter Vandenabeele
>> > <peter / vandenabeele.com>wrote:
>> >
>> >>> >
>> >>> Thanks Robert,
>> >>>
>> >>> By collect range, how would I say subtract 15 minutes from the curre=
nt
>> >>> time so that I can collect all matches from 14:31:02 through 14:46:0=
2?
>> >>>
>> >>
>> >> 1.9.3p0 :006 > Time.now.to_s
>> >> =A0=3D> "2012-01-17 16:36:06 +0100"
>> >> 1.9.3p0 :007 > (Time.now - (15*60)).to_s
>> >> =A0=3D> "2012-01-17 16:21:09 +0100"
>> >>
>> >
>> > Actually, for log files, it may be better to use uniform UTC times. So=
,
>> > no confusion on summer/winter time or servers in different part of
>> > the world etc.
>> >
>> > 1.9.3p0 :029 > Time.now.utc.to_s
>> > =A0=3D> "2012-01-17 15:43:25 UTC"
>> > 1.9.3p0 :030 > (Time.now - (15*60)).utc.to_s
>> > =A0=3D> "2012-01-17 15:28:30 UTC"
>> >
>> > HTH,
>> >
>> > Peter
>>
>> Unfortunately I have no control over the format of the logs. I am trying
>> to compare the current time to the string value in the logs.
>> Any ideas on how to get past the crossing midnight issue?
>>
>> I came up with is for gathering start and end times but the above looks
>> nicer.
>>
>> s_time =3D Time.now
>> e_time =3D s_time - 900
>>
>
> Maybe s_time and e_time are swapped here ?
>
>
>> Either way I now have the start time and end time so how would I make a
>> range out of that counting in seconds?
>>
>
> Well, reading the Time documentation I see Time#to_f that may be a useful
> number. And then, you could make a range with
>
> s_time.to_f..e_time.to_f
>
> Take note that comparisons on floats have their own problems
> if the delta between them is very small and they are not integer.
>
> 1.9.3-p0 :001 > e_time =3D Time.now.utc
> =A0=3D> 2012-01-17 17:51:46 UTC
> 1.9.3-p0 :002 > s_time =3D e_time - 900
> =A0=3D> 2012-01-17 17:36:46 UTC
>
> 1.9.3-p0 :003 > range =3D s_time.to_f..e_time.to_f
> =A0=3D> 1326821806.2174225..1326822706.2174225
>
> 1.9.3-p0 :004 > log_time =3D Time.now.utc-300
> =A0=3D> 2012-01-17 17:47:22 UTC
>
> 1.9.3-p0 :007 > range =3D=3D=3D log_time.to_f
> =A0=3D> true # that log_time is in the range :-)
>
> 1.9.3-p0 :008 > range =3D=3D=3D Time.now.to_f
> =A0=3D> false # now is obviously outside the range :-)
>
> 1.9.3-p0 :009 > # but, you could simply compare times themselves ... not =
!
> 1.9.3-p0 :010 > =A0 time_range =3D s_time..e_time
> =A0=3D> 2012-01-17 17:36:46 UTC..2012-01-17 17:51:46 UTC
> 1.9.3-p0 :011 > time_range =3D=3D=3D Time.now
> TypeError: can't iterate from Time
> =A0 =A0from (irb):11:in `each'
> =A0 =A0from (irb):11:in `include?'
> =A0 =A0from (irb):11:in `include?'
> =A0 =A0from (irb):11:in `=3D=3D=3D'
> =A0 =A0from (irb):11
> =A0 =A0from /home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
>
>
> Strange, obviously, for Float too ... (can't iterate from Float),
> but, I can determine if a value is inside a range of Floats.
> Why not for Time then ? Time has a <=3D> operator ...
>
> 1.9.3-p0 :014 > =A0 range.each{|f| puts f}
> TypeError: can't iterate from Float
> =A0 =A0from (irb):14:in `each'
> =A0 =A0from (irb):14
> =A0 =A0from /home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
>
>
> Being relatively newb, I still find it amazing that parts f the script I
>> think would be the simplest turns out to be the most difficult.
>>
>
> I may have confused you even more ... (sorry for that).
>
> I was under the impression you wanted to compare the lines
> based on the string representation (in which case those issues
> of midnight and mm/dd/yyyy representation etc. would play).
>
> But if you import the date+time of each line with the code that
> Robert proposed above, you end up with "clean" Time instances
> that will work properly.
>
> Sorry for the confusion ...
>
> HTH,
>
> Peter