Re: time_t problem: representing an error value
On 6 May, 12:03, Sam <s...@email-scan.com> wrote:
Dom Bannon writes:
I need to represent dates going back to, potentially, ~1900
(on Windows and Linux). The obvious way to do this is to use
a time_t, but this seems to have a flaw in that various
library routines (mktime and time, at least) use (time_t)-1
as an error return.
(time_t)-1 is a valid representation of the last second in
1969, so it's only a matter of time before I need this as
a valid date.
I suppose I'm just going to have to ignore this, and work
around problems as they arise. Thoughts? A better way to do
this? If you've got a database with dates in it, do you use
time_t, or something else?
(time_t)-1 is used as an error indication by library functions
whose domain is defined solely as Jan 1, 1970, and after.
There is no ambiguity in those cases.
That's true for Unix, but the C standard is much, much looser.
The domain is implementation defined (and could reasonably start
much later than Jan. 1, 1970), and the C standard doesn't even
guarantee that time_t is an integral type (although it must be
If you need to work with date values before Jan 1, 1970,
you'll have to defined your own class, and provide methods for
mapping your date value to/from a time_t for the date ranges
that time_t can represent.
More generally, if you're doing anything with dates more than
a few years from the present, you're unlikely to be able to use
time_t. (Note that this is true for the future as well: where
I work, we sometimes have to calculate 50 years into the future,
or more, and the 32 bit time_t common on most Unixes doesn't
work for this either.)