Time fromMilliseconds ... greaterThan

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Time fromMilliseconds ... greaterThan

Howard Oh
I've encountered a absurd test results below.


time1 := Time fromMilliseconds: 62524375.
time2 := Time fromMilliseconds: 90665140.

time1 < time2   "true"
time hours < time2 hours "false"


Normally, we would think instVar of Time 'milliseconds' has a bounding
range within
0 ~ 24*60*60*1000.

Sometimes adding times or scaling a time can set 'milliseconds' off the
bounding range, which might be a hard-to-find bug.

What if we take modular of "24*60*60*1000" before we set 'milliseconds'
.

Time>>setMilliseconds: anInteger

        milliseconds := anInteger rem: 24*60*60*1000


Does anyone foresee this modification might have some side effects?

To be careful, I've just #rem: the milliseconds before
#fromMilliseconds: being called.
But I not sure I can remember to rem: them in the future.

Best Regards,


Reply | Threaded
Open this post in threaded view
|

Re: Time fromMilliseconds ... greaterThan

Ian Bartholomew-21
Howard,

> Sometimes adding times or scaling a time can set 'milliseconds' off the
> bounding range, which might be a hard-to-find bug.
>
> What if we take modular of "24*60*60*1000" before we set 'milliseconds'
> .
>
> Time>>setMilliseconds: anInteger
>
> milliseconds := anInteger rem: 24*60*60*1000

I'd be more inclined to trap it as an error

Time>>fromMilliseconds: anInteger
        self assert: [anInteger < ##(24*60*60*1000)].
        "blah"

The Time class is intended to be used as a value representing "time of
day".  Any attempt to set it to represent a value greater than 24 hours
should be seen, in the context of the Time class, as an error.  If you
have to adjust a value to make it fit then, IMHO, that should be done
outside of the Time class.

You might want to have a look at the implementation of the ANSI Duration
class in my goodies.  It represents any duration of time, not limited to
24 hours, and might be easier to use if you are manipulating longer times.

--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.