Forum: Ruby Comparing times?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
36194a022cb2afb981bfdf720fc67d82?d=identicon&s=25 Eric Armstrong (Guest)
on 2006-06-10 02:12
(Received via mailing list)
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...
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Timothy Hunter (Guest)
on 2006-06-10 02:24
(Received via mailing list)
Eric Armstrong 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.
31af45939fec7e3c4ed8a798c0bd9b1a?d=identicon&s=25 Matthew Smillie (notmatt)
on 2006-06-10 04:10
(Received via mailing list)
On Jun 10, 2006, at 1:10, Eric Armstrong 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.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-06-11 21:28
(Received via mailing list)
On Jun 9, 2006, at 8:10 PM, Eric Armstrong 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 * 10**6 + 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)
B81659fa35f4cf2da37bca8664ffc7d0?d=identicon&s=25 Craig Kim (Guest)
on 2006-06-11 21:57
(Received via mailing list)
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 Capaldo" <logancapaldo@gmail.com>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Sunday, June 11, 2006 3:26 PM
Subject: Re: Comparing times?
This topic is locked and can not be replied to.