Discussion:
A question regarding the signed-ness of time_t [C]
(too old to reply)
Rob Somers
2006-06-13 22:41:57 UTC
Permalink
First of all, I must apologise for using Google to post this message.
I am having a bit of trouble with the news service from my ISP, and so
this is what I have ended up resorting to..

In my manual page for the time function - man (2) time, I read:

"On success, the value of time in seconds since the Epoch is
returned. On error, ((time_t)-1) is returned, and errno is set
appropriately."

Now I understand that the *NIX man pages are not the C standard, but if
I believe, this is the standard behaviour for time anyhow. The thing
that makes me wonder is the return value [(time_t) -1] if there is an
error. I have read in a couple of different places, by reputable
authors, that time_t could be signed, or unsigned. If time_t is
unsigned though, would that not break a lot of existing *NIX
implementations? How could the time function return (time_t) -1 if it
is unsigned?
Keith Thompson
2006-06-14 00:50:36 UTC
Permalink
Post by Rob Somers
First of all, I must apologise for using Google to post this message.
I am having a bit of trouble with the news service from my ISP, and so
this is what I have ended up resorting to..
"On success, the value of time in seconds since the Epoch is
returned. On error, ((time_t)-1) is returned, and errno is set
appropriately."
Now I understand that the *NIX man pages are not the C standard, but if
I believe, this is the standard behaviour for time anyhow. The thing
that makes me wonder is the return value [(time_t) -1] if there is an
error. I have read in a couple of different places, by reputable
authors, that time_t could be signed, or unsigned. If time_t is
unsigned though, would that not break a lot of existing *NIX
implementations? How could the time function return (time_t) -1 if it
is unsigned?
The C standard merely says that time_t is an arithmetic type capable
of representing times. It doesn't specify the epoch, the resolution,
or even that the representation is linear or monotonic; it could be a
floating-point type.

On Unix systems, and on many others, time_t is a signed integer type
representing seconds since 1970-01-01 00:00:00 GMT. The value
(time_t)-1 could be an error indicator *or* a valid representation for
1969-12-31 23:59:59 GMT. But it's defined as an error indicator only
for the result of the time() and mktime() functions. The time()
function will never return a valid result of -1 unless you have a time
machine or a clock that's running at least 36 years slow.

Making time_t unsigned could break some things under Unix, but if the
epoch is the same, the binary representation of a given time between
1970 (0) and 2038 (2**31-1) will probably be unchanged. Changing
time_t from 32-bit signed to 32-bit unsigned would change the range of
representable times from 1901..2038 to 1970..2106. In my opinion,
this would be a bad idea; I suspect 32-bit time_t will be obsolete
long before 2038, and making time_t unsigned would create pressure to
make a 64-bit time_t unsigned as well, permanently losing the ability
to represent times before 1970. (The one-second gap at 1969-12-31
23:59:59 GMT is troubling, but in practice shouldn't cause many
problems.)

There have been proposals to create a revised time package for a
future C standard. Work on such a proposal wasn't ready in time for
C99, and it's now more or less on hold. There's currently a
discussion of this in comp.std.c.
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jack Klein
2006-06-14 06:23:51 UTC
Permalink
Post by Keith Thompson
Post by Rob Somers
First of all, I must apologise for using Google to post this message.
I am having a bit of trouble with the news service from my ISP, and so
this is what I have ended up resorting to..
"On success, the value of time in seconds since the Epoch is
returned. On error, ((time_t)-1) is returned, and errno is set
appropriately."
Now I understand that the *NIX man pages are not the C standard, but if
I believe, this is the standard behaviour for time anyhow. The thing
that makes me wonder is the return value [(time_t) -1] if there is an
error. I have read in a couple of different places, by reputable
authors, that time_t could be signed, or unsigned. If time_t is
unsigned though, would that not break a lot of existing *NIX
implementations? How could the time function return (time_t) -1 if it
is unsigned?
The C standard merely says that time_t is an arithmetic type capable
of representing times. It doesn't specify the epoch, the resolution,
or even that the representation is linear or monotonic; it could be a
floating-point type.
On Unix systems, and on many others, time_t is a signed integer type
representing seconds since 1970-01-01 00:00:00 GMT.
[snip]

On some Unix systems, time_t is a signed integer type. The POSIX
standard specifies neither that is a signed integer type, nor that it
is an integer type at all.

Like the C standard, POSIX specifies that time_t is an arithmetic
type, and an implementation that used a long long or a long double
could conform to both standards. Although that would break a large
amount of sloppy C code on both POSIX and non-POSIX platforms, where
programmers assume that time_t is a 32-bit integer type, signed or
unsigned.

The only difference between the C and POSIX standards is that the
latter specifies the resolution (seconds) and the specific 0 reference
time, while the former does not.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
Francis Glassborow
2006-06-14 09:59:28 UTC
Permalink
Post by Rob Somers
Now I understand that the *NIX man pages are not the C standard, but if
I believe, this is the standard behaviour for time anyhow. The thing
that makes me wonder is the return value [(time_t) -1] if there is an
error. I have read in a couple of different places, by reputable
authors, that time_t could be signed, or unsigned. If time_t is
unsigned though, would that not break a lot of existing *NIX
implementations? How could the time function return (time_t) -1 if it
is unsigned?
Let me answer the question asked. (time_t) -1 is either -1 (on systems
using a signed type for time_t) or the maximum value of the range of
unsigned values for systems using an unsigned type.

Badly written code that uses a naive check that the return value of the
relevant functions is >= 0 will not be portable. However those that
correctly check retval != (time_t) - 1 will work regardless of the type
used for time_t
--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Loading...