"James Youngman" <[EMAIL PROTECTED]> writes: > On 3/27/07, Paul Eggert <[EMAIL PROTECTED]> wrote: >> - /* Birth time not supported. */ >> - pts->tv_sec = 0; >> - pts->tv_nsec = 0; >> - return 0; /* result is not valid */ >> + /* Birth time is not supported. Set tv_sec to avoid undefined behavior. >> */ >> + t.tv_sec = -1; >> + t.tv_nsec = -1; > > This is an implicit change from what *BSD does in this situation; it > normally tries to return (time_t)0 in tv_sec. > > To be honest, I don't like the convention of returning ((time_t)0) > myself, since it's a valid timestamp. Using ((time_t)-1) seems more > sensible to me, by analogy with the result of time(2). > > Was the change deliberate?
Yes. The key here is that tv_nsec < 0 indicates an error, so the value of tv_sec is irrelevant. We have to set tv_sec to something to avoid undefined behavior. -1 is cheap since it's already in a register, and I suppose using -1 might help track down bugs in buggy callers. However, (time_t) -1 is also a valid time_t value, so callers shouldn't use tv_sec==-1 to detect the error case. They should use tv_nsec<0.
