# Ruby Time - Sports Time

**URL:** <https://rubytalk.org/t/ruby-time-sports-time/60723>\
**Category:** ruby-talk\
**Created:** [4 November 2010 07:50 UTC](https://rubytalk.org/t/ruby-time-sports-time/60723 "2010-11-04T07:50:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)\
**Post date:** [4 November 2010 07:50 UTC](https://rubytalk.org/t/ruby-time-sports-time/60723/1 "2010-11-04T07:50:13Z")

</div>

Hi I am reading the time classes for Ruby. However I can't figure out  
what time method would be best for sports times. I am referring to  
this link [http://www.ruby.ch/ProgrammingRuby/htmlC/ref\_c\_time.html](http://www.ruby.ch/ProgrammingRuby/htmlC/ref_c_time.html)

I want the times stored to be in a mm:ss:hh format or ss:hh (where hh  
= hundreths of seconds).

The times stored will be of times occuring over distance, and some  
will be imported from internet sources and some will come from direct  
user input.

I am currently planning to use sequel and sqlite to store and query my  
data.

Any suggestions on the best and most reliable format? I forgot to  
mention that sums and calculations including averages will be  
performed on the times.

Thanks

Sayth

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 November 2010 09:11 UTC](https://rubytalk.org/t/ruby-time-sports-time/60723/2 "2010-11-04T09:11:53Z")

</div>

> Hi I am reading the time classes for Ruby. However I can't figure out  
> what time method would be best for sports times. I am referring to  
> this link [http://www.ruby.ch/ProgrammingRuby/htmlC/ref\_c\_time.html](http://www.ruby.ch/ProgrammingRuby/htmlC/ref_c_time.html)
> 
> I want the times stored to be in a mm:ss:hh format or ss:hh (where hh  
> = hundreths of seconds).

First we should distinguish between storage and representation. You  
may want to print (i.e. use a representation) times in the format you  
give but that does not necessarily mean that you also want or need to  
store them that way.

> The times stored will be of times occuring over distance, and some  
> will be imported from internet sources and some will come from direct  
> user input.
> 
> I am currently planning to use sequel and sqlite to store and query my  
> data.

Second, it seems you want to store \*durations\* not \*points in time\*,  
which is what class Time is for. For storing durations and doing the  
kind of math with them that you indicate it's best to base it on some  
numeric type, see below.

> Any suggestions on the best and most reliable format? I forgot to  
> mention that sums and calculations including averages will be  
> performed on the times.

I see two reasonable approaches:

1. Use what Time arithmetic provides (Floats)

irb(main):011:0\> t1=Time.now;sleep 1.2;t2=Time.now;t2 - t1  
=\> 1.205

Then you would have to create a method duration\_to\_string(d) which  
creates the string representation that you want, e.g.

def duration\_to\_string(d)  
&nbsp;&nbsp;min,sec = d.divmod 60  
&nbsp;&nbsp;sec,msec = sec.divmod 1  
&nbsp;&nbsp;sprintf "%d:%02d:%03d", min, sec, msec \* 1000  
end

2. Create a custom class Duration that contains a Float for the  
duration, implements all the math operators and which implemens #to\_s  
with the code of duration\_to\_string above.

Downside of this approach is that you then normally would want to  
change Time#+, Time#- etc. to return an instance of Duration and not  
Float to have it all nicely integrated. With some effort you can get  
that done and probably also keep most code compatible. That would be  
a nice experiment.

You can find more info on approach 2 here:  
[http://blog.rubybestpractices.com/posts/rklemme/019-Complete\_Numeric\_Class.html](http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html)

Kind regards

robert

> **···**
>
> On Thu, Nov 4, 2010 at 8:50 AM, flebber \<flebber.crue@gmail.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)\
**Post date:** [4 November 2010 10:40 UTC](https://rubytalk.org/t/ruby-time-sports-time/60723/3 "2010-11-04T10:40:14Z")

</div>

> \> Hi I am reading the time classes for Ruby. However I can't figure out  
> \> what time method would be best for sports times. I am referring to  
> \> this linkhttp://www.ruby.ch/ProgrammingRuby/htmlC/ref\_c\_time.html
> 
> \> I want the times stored to be in a mm:ss:hh format or ss:hh (where hh  
> \> = hundreths of seconds).
> 
> First we should distinguish between storage and representation. You  
> may want to print (i.e. use a representation) times in the format you  
> give but that does not necessarily mean that you also want or need to  
> store them that way.
> 
> \> The times stored will be of times occuring over distance, and some  
> \> will be imported from internet sources and some will come from direct  
> \> user input.
> 
> \> I am currently planning to use sequel and sqlite to store and query my  
> \> data.
> 
> Second, it seems you want to store \*durations\* not \*points in time\*,  
> which is what class Time is for. For storing durations and doing the  
> kind of math with them that you indicate it's best to base it on some  
> numeric type, see below.
> 
> \> Any suggestions on the best and most reliable format? I forgot to  
> \> mention that sums and calculations including averages will be  
> \> performed on the times.
> 
> I see two reasonable approaches:
> 
> 1. Use what Time arithmetic provides (Floats)
> 
> irb(main):011:0\> t1=Time.now;sleep 1.2;t2=Time.now;t2 - t1  
> =\> 1.205
> 
> Then you would have to create a method duration\_to\_string(d) which  
> creates the string representation that you want, e.g.
> 
> def duration\_to\_string(d)  
> min,sec = d.divmod 60  
> sec,msec = sec.divmod 1  
> sprintf "%d:%02d:%03d", min, sec, msec \* 1000  
> end
> 
> 2. Create a custom class Duration that contains a Float for the  
> duration, implements all the math operators and which implemens #to\_s  
> with the code of duration\_to\_string above.
> 
> Downside of this approach is that you then normally would want to  
> change Time#+, Time#- etc. to return an instance of Duration and not  
> Float to have it all nicely integrated. With some effort you can get  
> that done and probably also keep most code compatible. That would be  
> a nice experiment.
> 
> You can find more info on approach 2 here:[http://blog.rubybestpractices.com/posts/rklemme/019-Complete\_Numeric\_\.\.\](http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_%5C.%5C.%5C).
> 
> Kind regards
> 
> robert
> 
> --  
> remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/

I have two questions regarding th numerical approaches.

> 1. Use what Time arithmetic provides (Floats)  
> irb(main):011:0\> t1=Time.now;sleep 1.2;t2=Time.now;t2 - t1  
> =\> 1.205

This method minuses t1 - t2 to calculate duration. However the  
duration will already be defined or imported. So to me this means that  
I need to go down the second option of creating a class based on the  
reverse of this.

> def duration\_to\_string(d)  
> &nbsp;&nbsp;min,sec = d.divmod 60  
> &nbsp;&nbsp;sec,msec = sec.divmod 1  
> &nbsp;&nbsp;sprintf "%d:%02d:%03d", min, sec, msec \* 1000  
> end

Can i clarify in the 2nd method what was meant by "implements all the  
math operators". Every value would be positive as it is greater than  
zero isn't it?

> **···**
>
> On Nov 4, 8:11 pm, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> > On Thu, Nov 4, 2010 at 8:50 AM, flebber \<flebber.c...@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 November 2010 12:29 UTC](https://rubytalk.org/t/ruby-time-sports-time/60723/4 "2010-11-04T12:29:31Z")

</div>

> > \> Hi I am reading the time classes for Ruby. However I can't figure out  
> > \> what time method would be best for sports times. I am referring to  
> > \> this linkhttp://www.ruby.ch/ProgrammingRuby/htmlC/ref\_c\_time.html
> > 
> > \> I want the times stored to be in a mm:ss:hh format or ss:hh (where hh  
> > \> = hundreths of seconds).
> > 
> > First we should distinguish between storage and representation. You  
> > may want to print (i.e. use a representation) times in the format you  
> > give but that does not necessarily mean that you also want or need to  
> > store them that way.
> > 
> > \> The times stored will be of times occuring over distance, and some  
> > \> will be imported from internet sources and some will come from direct  
> > \> user input.
> > 
> > \> I am currently planning to use sequel and sqlite to store and query my  
> > \> data.
> > 
> > Second, it seems you want to store \*durations\* not \*points in time\*,  
> > which is what class Time is for. For storing durations and doing the  
> > kind of math with them that you indicate it's best to base it on some  
> > numeric type, see below.
> > 
> > \> Any suggestions on the best and most reliable format? I forgot to  
> > \> mention that sums and calculations including averages will be  
> > \> performed on the times.
> > 
> > I see two reasonable approaches:
> > 
> > 1. Use what Time arithmetic provides (Floats)
> > 
> > irb(main):011:0\> t1=Time.now;sleep 1.2;t2=Time.now;t2 - t1  
> > =\> 1.205
> > 
> > Then you would have to create a method duration\_to\_string(d) which  
> > creates the string representation that you want, e.g.
> > 
> > def duration\_to\_string(d)  
> > min,sec = d.divmod 60  
> > sec,msec = sec.divmod 1  
> > sprintf "%d:%02d:%03d", min, sec, msec \* 1000  
> > end
> > 
> > 2. Create a custom class Duration that contains a Float for the  
> > duration, implements all the math operators and which implemens #to\_s  
> > with the code of duration\_to\_string above.
> > 
> > Downside of this approach is that you then normally would want to  
> > change Time#+, Time#- etc. to return an instance of Duration and not  
> > Float to have it all nicely integrated. With some effort you can get  
> > that done and probably also keep most code compatible. That would be  
> > a nice experiment.
> > 
> > You can find more info on approach 2 here:[http://blog.rubybestpractices.com/posts/rklemme/019-Complete\_Numeric\_\.\.\](http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_%5C.%5C.%5C).

> I have two questions regarding th numerical approaches.
> 
> > 1. Use what Time arithmetic provides (Floats)  
> > irb(main):011:0\> t1=Time.now;sleep 1.2;t2=Time.now;t2 - t1  
> > =\> 1.205
> 
> This method minuses t1 - t2 to calculate duration.

Yes, that was to demonstrate what Ruby's internal classes use when  
they deal with durations.

> However the duration will already be defined or imported.

That's what I assumed.

> So to me this means that  
> I need to go down the second option of creating a class based on the  
> reverse of this.

What do you mean? I can see no fact that implies you must go down  
route 2. Floats can be stored in the database and can be used for  
min, max, average and other calculations. You do not \_need\_ a new  
class.

> > def duration\_to\_string(d)  
> > min,sec = d.divmod 60  
> > sec,msec = sec.divmod 1  
> > sprintf "%d:%02d:%03d", min, sec, msec \* 1000  
> > end
> 
> Can i clarify in the 2nd method what was meant by "implements all the  
> math operators". Every value would be positive as it is greater than  
> zero isn't it?

Did you look at the blog entry I referred you to?

Kind regards

robert

> **···**
>
> On Thu, Nov 4, 2010 at 11:40 AM, flebber \<flebber.crue@gmail.com\> wrote:
> 
> > On Nov 4, 8:11 pm, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> > 
> > > On Thu, Nov 4, 2010 at 8:50 AM, flebber \<flebber.c...@gmail.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
