Range#=== , need #succ method in Ruby1.9.3 under only #<=>, 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>wrote: > >> 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 current >> >>> time so that I can collect all matches from 14:31:02 through 14:46:02? >> >>> >> >> >> >> 1.9.3p0 :006 > Time.now.to_s >> >> > "2012-01-17 16:36:06 +0100" >> >> 1.9.3p0 :007 > (Time.now - (15*60)).to_s >> >> > "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 >> > > "2012-01-17 15:43:25 UTC" >> > 1.9.3p0 :030 > (Time.now - (15*60)).utc.to_s >> > > "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 = Time.now >> e_time = 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 = Time.now.utc > > 2012-01-17 17:51:46 UTC > 1.9.3-p0 :002 > s_time = e_time - 900 > > 2012-01-17 17:36:46 UTC > > 1.9.3-p0 :003 > range = s_time.to_f..e_time.to_f > > 1326821806.2174225..1326822706.2174225 > > 1.9.3-p0 :004 > log_time = Time.now.utc-300 > > 2012-01-17 17:47:22 UTC > > 1.9.3-p0 :007 > range === log_time.to_f > > true # that log_time is in the range :-) > > 1.9.3-p0 :008 > range === Time.now.to_f > > 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 > time_range = s_time..e_time > > 2012-01-17 17:36:46 UTC..2012-01-17 17:51:46 UTC > 1.9.3-p0 :011 > time_range === Time.now > TypeError: can't iterate from Time > ¨Âòï¨éò⩺±±ºéàåáãè§ > ¨Âòï¨éò⩺±±ºéàéîãìõäå¿§ > ¨Âòï¨éò⩺±±ºéàéîãìõäå¿§ > ¨Âòï¨éò⩺±±ºéཽ½> ¨Âòï¨éò⩺±± > ¨Âòï¯èïíå¯ðåôåòö¯®òöí¯òõâéåó¯òõâù±®¹®³ð°¯âéî¯éò⺱¶ºéî à¼íáéî¾§ > > > 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 <=> operator ... > > 1.9.3-p0 :014 > range.each{|f| puts f} > TypeError: can't iterate from Float > ¨Âòï¨éò⩺±´ºéàåáãè§ > ¨Âòï¨éò⩺±´ > ¨Âòï¯èïíå¯ðåôåòö¯®òöí¯òõâéåó¯òõâù±®¹®³ð°¯âéî¯éò⺱¶ºéî à¼íáéî¾§ > > > 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