Post by Geoff ClarePost by Keith ThompsonPost by Mr. Man-wai ChangPost by Keith ThompsonI'm a little disappointed that POSIX doesn't require time_t to be
signed. 64 bits is enough to represent a range of about 584 billion
years. An unsigned time_t makes it impossible to represent times before
1970.
Could we roll our own signed time_t? :)
I see the smiley, but I don't get the joke.
If you're creating your own implementation, you can do anything you
like. If not, and you're using an implementation that makes time_t an
unsigned type, there's not much you can do to treat it as signed. For
example, localtime() would presumably treat a time_t value of -2 as a
very large unsigned value.
I don't know of any implementations that make time_t an unsigned type.
QNX is one.
See www.qnx.net/developers/docs/6.4.0/neutrino/sys_arch/kernel.html
Valid dates on a QNX Neutrino system range from January 1970 to at
least 2038. The time_t data type is an unsigned 32-bit number, which
extends this range for many applications through 2106.
Also, having signed time_t doesn't necessarily mean that dates before
Jan 1970 are supported. I seem to recall that MS Windows has signed
time_t but does not support negative values.
Sadly, you're right.
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/mktime-mktime32-mktime64?view=msvc-170
_mktime32 returns the specified calendar time encoded as a value of
type time_t. If timeptr references a date before midnight, January
1, 1970, or if the calendar time can't be represented, _mktime32
returns -1 cast to type time_t.
I've worked on Windows code that had to deal with patients' birth dates
to determine their current age. I don't recall the exact details, but I
had to apply a 400-year offset to the birth date and current time to get
correct results for patients born before 1970. (The Gregorian calendar
repeats after 400 years.) We had 64-bit signed time_t, so it wasn't due
to this specific problem; with 32-bit time_t the 400-year offset
wouldn't have been possible.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+***@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */