# Comparing times?

**URL:** <https://www.ruby-forum.com/t/comparing-times/62983>\
**Category:** Ruby\
**Created:** [June 10, 2006, 2:12am UTC](https://www.ruby-forum.com/t/comparing-times/62983 "2006-06-10T02:12:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [June 10, 2006, 2:12am UTC](https://www.ruby-forum.com/t/comparing-times/62983/1 "2006-06-10T02:12:01Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [June 10, 2006, 2:24am UTC](https://www.ruby-forum.com/t/comparing-times/62983/2 "2006-06-10T02:24:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [June 10, 2006, 4:10am UTC](https://www.ruby-forum.com/t/comparing-times/62983/3 "2006-06-10T04:10:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [June 11, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/comparing-times/62983/4 "2006-06-11T21:28:02Z")

</div>

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 \* 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)

---

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [June 11, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/comparing-times/62983/5 "2006-06-11T21:57:00Z")

</div>

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](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Sunday, June 11, 2006 3:26 PM  
Subject: Re: Comparing times?
