On Thu, Nov 4, 2010 at 11:40 AM, flebber <flebber.crue / gmail.com> wrote:
> On Nov 4, 8:11=A0pm, Robert Klemme <shortcut... / googlemail.com> wrote:
>> On Thu, Nov 4, 2010 at 8:50 AM, flebber <flebber.c... / gmail.com> 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 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
>> > =3D hundreths of seconds).
>>
>> First we should distinguish between storage and representation. =A0You
>> 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. =A0For 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=3DTime.now;sleep 1.2;t2=3DTime.now;t2 - t1
>> =3D> 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)
>> =A0 min,sec =3D d.divmod 60
>> =A0 sec,msec =3D sec.divmod 1
>> =A0 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. =A0With some effort you can get
>> that done and probably also keep most code compatible. =A0That would be
>> 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=3DTime.now;sleep 1.2;t2=3DTime.now;t2 - t1
>> =3D> 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)
>> =A0 min,sec =3D d.divmod 60
>> =A0 sec,msec =3D sec.divmod 1
>> =A0 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/