Ruby Time - Sports Time

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

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

On Thu, Nov 4, 2010 at 8:50 AM, flebber [email protected] wrote:

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

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

  1. 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

Kind regards

robert

On Nov 4, 8:11pm, Robert K. [email protected] wrote:

give but that does not necessarily mean that you also want or need to
which is what class Time is for. For storing durations and doing the

end

You can find more info on approach 2
here:http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_

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)
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?

On Thu, Nov 4, 2010 at 11:40 AM, flebber [email protected] wrote:

may want to print (i.e. use a representation) times in the format you
Second, it seems you want to store durations not points in time,

  1. Use what Time arithmetic provides (Floats)
    sprintf “%d:%02d:%03d”, min, sec, msec * 1000
    a nice experiment.

You can find more info on approach 2
here:http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_

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