[phobos] datetime review (new attempt at URL)

Jonathan M Davis jmdavisProg at gmx.com
Sun Oct 17 20:50:21 PDT 2010


On Thursday 14 October 2010 19:04:12 Steve Schveighoffer wrote:
> > > TimeOfDay: Can we  use HNSecDuration with an invariant that it's < 24
> > > hours?
> > > 
> > >  I  can't see why you'd want to reimplement all this.  FWIW, Tango uses
> > >  Span (the duration type) as it's timeofday component.
> > > 
> > > Date is  one thing -- the durations are based on a point in time.  But
> > > time
> > > 
> > >  of day is always the same no matter the day.
> > 
> > Hmm. I didn't think of that.  It doesn't quite work though. Aside from
> > the fact
> > 
> > that that would take more  memory (for better or for worse), stuff like
> > rollHours() wouldn't work if  you just used an HNSecDuration, and if
> > you're wrapping an HNSecDuration, you  have to do a lot more
> > calculations for that
> >
> >sort
> >
> > of code. I think that the  result is a net-loss, personally.
> 
> More memory for more resolution and less types to deal with (no FracSec)
> 
> But let's calculate it out, there are 10 * 1000 * 1000 * 60 * 60 * 24
> hnsecs in a day.  That comes out to require 5 bytes of storage (max value
> 864 billion). In the grand scheme of things 3 wasted bytes aren't that
> bad.
> 
> as for rollHours, I skipped over that as well.  Let me read it...
> 
> Ah ok, here's a complete implementation (with hnsecsPer):
> 
> long roll!(TUnit un)(long orig, long nOfUnit)
> {
>    auto x = orig / hnsecsPer!(un - 1) * hnsecsPer!(un - 1);
>    return (orig + nOfUnit * hnsecsPer!(un)) % hnsecsPer!(un - 1) + x;
> }
> 
> and accompany this with the ensuing aliases e.g.:
> 
> alias roll!(TUnit.days) rollDays;

That doesn't work. It might be made to work, but it ignores a key issue with 
regards to the time of day, and that is the fact that the time of day in A.D. is 
a different number of hnsecs than it is in B.C. That's the case because in A.D., 
your counting up from midnight, while in B.C., your'e counting down from. So, 1 
hnsec passed midnight January 1st, 1 A.D. UTC is 0001-01-01 00:00:00.0000001 
whereas 1 hnsec before midnight is 0000-12-31 23:59:59.999999. They have the 
same number of hnsecs except for their sign, and the time of day that they are 
is completely different. That fact also makes uses an HNSecDuration instead of 
TimeOfDay rather problematic. Rolling the time in TimeOfDay or DateTime is far 
easier than it is in SysTime precisely because of this problem. Getting the 
hour, minute, or second of the day in TimeOfDay and DateTime is also easier for 
the exact same reason.

So, I have reduced the amount of code that I have for stuff like roll!(), but I'm 
definitely leaving in TimeOfDay, and I haven't completely revamped how rolling 
works in SysTime either. It has been further genericized though.

In any case, I will be posting my updated code on the main list shortly.

- Jonathan M Davis


More information about the phobos mailing list