|
Boost : |
From: Dave Gomboc (dave_at_[hidden])
Date: 2001-12-19 13:34:40
> Sorry, I'm not sure whether I understand. Would you use TWO
> datetime_instant objects (or one instant and one span?) to represent
> December 17, 2001? What advantage does this have? Please give an
> example.
Neither. I'd use one datetime_range.
Clearly, when working just with days, one can use datetime_instant and
forget about it. But when working with a mixture of single days, ranges
of days, months, years, you name it, I've found it much more convenient
to simply treat everything as ranges of datetimes. In that case, the
day of December 17 would be represented publicly as:
datetime_range.begin() => December 17, 2001, 12:00am
datetime_range.end() => December 18, 2001, 12:00am
datetime_range.duration() => 1 day
(What you store and what you compute on-the-fly and/or cache is up to
you. If you're importing data from Excel, you might find using
double start => 37242.0;
double duration => 1.0;
to be convenient.)
The advantage is that your code then doesn't have to deal with different
kinds of datetime items everywhere (this thing is holding year values,
that thing is holding date ranges, that third thing is in date values,
yada yada). You pick a representation that handles the smallest item
you need (e.g. perhaps you need minutes), then you can use the same
representation for all kinds of datetimes. Sorting events
chronologically, or finding all events that contain a certain
datetime_instant is straightforward, because you've dodged a bunch of
conversions (or at least, did them up front).
Your mileage may vary, but it's saved me significant hassle in the past.
Dave Gomboc
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk