Comparing times?


#1

This is driving me crazy. I know I
saw a way to do an “almost” comparison,
where “x == y +/- some delta”. But I’m
hanged if I can find it. I’ve looked
everywhere…

On a related subject, when Ruby gives me
a File time, is it to the nearest second,
millisecond, or what, or is it O/S dependent?

The goal, of course, is to compare times
on two files to see if they are no more
than a second apart…


#2

Eric A. wrote:

This is driving me crazy. I know I
saw a way to do an “almost” comparison,
where “x == y +/- some delta”. But I’m
hanged if I can find it. I’ve looked
everywhere…

Well, test/unit has assert_in_delta. Is that what you’re thinking of? In
any case, it seems to me that

(x - y).abs < delta

would do the trick quite neatly.


#3

On Jun 10, 2006, at 1:10, Eric A. wrote:

On a related subject, when Ruby gives me
a File time, is it to the nearest second,
millisecond, or what, or is it O/S dependent?

File#mtime and other File time methods return a normal Time object,
which is O/S dependent but can be conveniently treated as a number of
seconds.

The goal, of course, is to compare times
on two files to see if they are no more
than a second apart…

file_a = File.new(“somefile”)
file_b = File.new(“someotherfile”)
(file_a.mtime - file_b.mtime).abs < 1 ? puts “close” : puts “not
close enough”

matthew smillie.


#4

On Jun 9, 2006, at 8:10 PM, Eric A. wrote:

The goal, of course, is to compare times
on two files to see if they are no more
than a second apart…

(t1.to_f - t2.to_f).abs < 1.0

#tv_sec will give it to you in integer seconds (same as #to_i)

#tv_usec will give you the fractional number of microseconds (stuff
after the decimal point)

t1.tv_sec * 106 + t1.tv_usec should be the time in microseconds.
(Divide it by 10.0
6 and you should get the the same thing as #to_f)


#5

The time resolution is OS dependent. However, on Unix, the resolution is
down to seconds. If you need to compare time, it’s best to do so by
first
converting them to epoch time (seconds since 1/1/1970).

----- Original Message -----
From: “Logan C.” removed_email_address@domain.invalid
To: “ruby-talk ML” removed_email_address@domain.invalid
Sent: Sunday, June 11, 2006 3:26 PM
Subject: Re: Comparing times?