Forum Navigation
Change Time to a struct/eliminate Interval
Posted: 09/05/07 21:41:33One of the strange parts of tango I've found is the core/Type.d, which defines Interval as a floating point value, representing seconds, and Time, which is an enumeration based on long, which represents 100ns increments since the year 0.
First, I don't like Interval being a floating point value. Floating point arithmetic is subject to error, and if you are precisely timing things, you wouldn't want floating point error creeping in. There are cases where you can generate infinite loop errors if you are doing floating point comparisons. I propose changing Interval to a long, defined as the number of ticks in an interval.
Second, Time being a simple enumeration provides too much flexibility. For example, you can convert a Time value t to seconds by doing:
t /= Time.TicksPerSecond;However, now t does not represent a Time type as specified by the Type.d description, rather another type that is based on seconds. If a careless coder saw that t is defined as a Time type, and didn't see the conversion to seconds, he wouldn't know that t is no longer a Time type. Another example, if I want t to be now based on ticks since 1970, I do:
t -= Time.TicksTo1970;But again, t no longer represents a Time, since Time is defined as the 100ns ticks since the year 0, not 1970.
These semantics could be mostly solved by changing Time to a structure that represents a point in time, and creating a new structure which represents a length of time, maybe called TimeSpan?. The internal representation could be somewhat abstract, requiring the user to think more about the meaning of the types. For example, the operator to subtract two times would return a TimeSpan? instead of a Time. A TimeSpan? could define properties that convert it to various time bases, such as long toSeconds() or long toMilliseconds().
I think this should be done sooner rather than later, as there are already instances of the above mentioned erroneous semantics in the tango tree, and undoubtably in other users' code. I wouldn't mind updating the tango tree, but I wouldn't want to do it if this isn't something anyone wants :)
In addition, I'd get rid of Interval, and use the TimeSpan? struct in it's place.
Using a struct for both Time and TimeSpan? would make the storage size of the value types no bigger than the current implementation.
What do people think?