# Re: Fuzzy Time (#99)

**URL:** <https://www.ruby-forum.com/t/re-fuzzy-time-99/74440>\
**Category:** Ruby\
**Created:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/re-fuzzy-time-99/74440 "2006-11-07T10:44:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/re-fuzzy-time-99/74440/1 "2006-11-07T10:44:04Z")

</div>

From: Robert Conn [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> I haven’t had time to do any “extra credit” stuff, or  
> even test this fully, but here goes -

For what it’s worth, here’s the test code I’ve written to test  
submissions. I’m not 100% sure that it’s correctly testing things yet,  
but I think it is 🙂

The interesting bit for me has been trying to identify any solution that  
ignores requirement #2, such as solutions that always show the correct  
time, or that are always a bit fast or slow. The statistics are my best  
attempt at that so far.

To use, if you have defined your class name as “FuzzyTime”, just include  
this file after your class definition. (Beware long incorrectly wrapped  
lines ahead.)

require ‘test/unit’

$DEBUG = false

class Time  
def short  
strftime(“%H:%M”)  
end  
def to\_fuzzy  
s = short  
s[4] = “~”  
s  
end  
end

class FuzzyTimeTester \< Test::Unit::TestCase  
def test\_running  
runs, num\_ahead, num\_behind = 0, 0, 0  
20.times{  
t = Time.at( Time.new + rand( 3600 \* 300 ) )  
advance\_seconds = rand( 200 ) + 17  
end\_time = t + 60 \* 60 \* 24  
ft = FuzzyTime.new( t )  
last\_value = nil  
while( t \< end\_time )  
assert\_equal t, ft.actual  
t0 = Time.at( t - 60 \* 5 )  
t2 = Time.at( t + 60 \* 5 )  
legal\_values = [t0, t, t2].map{ |x| x.to\_fuzzy }.uniq

```
    if last_value
      y,mon,day = t.year, t.mon, t.day
      h,m = last_value.scan(/\d+/).map{ |s| s.to_i }
      m *= 10
      if (m -= 10) < 0
        m %= 60
        if (h -= 1) < 0
          h %= 24
        end
      end
      illegal_old_value = Time.local( y,mon,day,h,m ).to_fuzzy
      legal_values -= [illegal_old_value]
      if $DEBUG
        puts "Now: #{t.short}; legal: #{legal_values.inspect} (was 

```

#{last\_value}, can’t be #{illegal\_old\_value})"  
end  
end

```
    s = ft.to_s
    assert legal_values.include?( s ), "#{s} not in 

```

#{legal\_values.inspect}"

```
    a = t.to_fuzzy
    if s != a
      ahour, amin = a.scan( /\d+/ ).map{ |x| x.to_i }
      fhour, fmin = s.scan( /\d+/ ).map{ |x| x.to_i }
      if fmin > amin || fhour > ahour || ( fhour == 0 && ahour == 23 

```

)  
num\_ahead += 1  
else  
num\_behind +=1  
end  
end  
runs += 1  
last\_value = s  
ft.advance( advance\_seconds )  
t += advance\_seconds  
end  
puts “Variation: %.2f%% ahead, %.2f%% behind” % [ 100.0 \*  
num\_ahead / runs, 100.0 \* num\_behind / runs ]  
}  
end  
end

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/re-fuzzy-time-99/74440/2 "2006-11-07T10:44:23Z")

</div>

On Mon, Oct 30, 2006 at 10:34:56AM +0900, Gavin K. wrote:

> correct time, or that are always a bit fast or slow. The statistics  
> are my best attempt at that so far.
> 
> To use, if you have defined your class name as “FuzzyTime”, just  
> include this file after your class definition. (Beware long  
> incorrectly wrapped lines ahead.)

Woo Hoo! I ran it on mine and it passed. My variation was as follows.  
Although  
I’m not sure what it really means. That I’m roughtly 80% the same as  
the  
‘actual’ time in fuzzy format, 2% behind and 18% ahead ?

```
Variation: 21.68% ahead, 1.41% behind
Variation: 19.69% ahead, 2.47% behind
Variation: 18.85% ahead, 2.85% behind
Variation: 19.76% ahead, 1.94% behind
Variation: 19.14% ahead, 2.19% behind
Variation: 18.67% ahead, 2.52% behind
Variation: 19.06% ahead, 2.24% behind
Variation: 18.83% ahead, 2.46% behind
Variation: 18.71% ahead, 2.74% behind
Variation: 18.42% ahead, 2.92% behind
Variation: 18.70% ahead, 2.60% behind
Variation: 18.56% ahead, 2.66% behind
Variation: 18.33% ahead, 2.90% behind
Variation: 18.30% ahead, 2.95% behind
Variation: 18.78% ahead, 2.72% behind
Variation: 18.73% ahead, 2.76% behind
Variation: 18.67% ahead, 2.79% behind
Variation: 18.49% ahead, 2.94% behind
Variation: 18.47% ahead, 3.03% behind
Variation: 18.35% ahead, 3.08% behind

```

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:46am UTC](https://www.ruby-forum.com/t/re-fuzzy-time-99/74440/3 "2006-11-07T10:46:20Z")

</div>

On Oct 29, 2006, at 8:34 PM, Gavin K. wrote:

> For what it’s worth, here’s the test code I’ve written to test  
> submissions. I’m not 100% sure that it’s correctly testing things  
> yet, but I think it is 🙂

It looks like your test code expects FuzzyTime.to\_s to use 24-hour  
style, by default. My submission defaults to 12-hour style, so it  
failed, right out of the gate. ☹

Tom
