Re: mktime() failure vs 31-DEC-1969 23:59:59 GMT
The standards for mktime() define a return value of (time_t)-1 as indicating that the time specified cannot be represented. However, a return value of -1 is also a valid time_t corresponding to 31-DEC-1969 23:59:59 GMT. How do we distinguish between the two?
Call localtime() on the seemingly-in-error mktime() result, and compare the relevant portions of the structs tm. A match implies a non-error.
Since there's normalization going on in mktime, the tm that comes back from localtime may not match the one that went in to mktime even if -1 is being returned as a legitimate time_t rather than an error indication. Another possibility is to save away the original tm structure; if mktime returns -1, bump the tm_sec value by 1, call mktime again, and see if it returns zero. --ado
Another possibility is to save away the original tm structure; if mktime returns -1, bump the tm_sec value by 1, call mktime again, and see if it returns zero.
This still doesn't tell me whether the -1 return from mktime() meant that the time was not representable. It does tell me whether it is representable one second later. If a particular implementation of mktime() doesn't support times before the Epoch (and that seems okay as far as the standards go), then a return of 0 resulting from bumping the tm_sec up by 1 would lead me to the wrong conclusion concerning my original call. An errno would really be helpful here. It's a shame the standards don't define one. - Tom
participants (2)
-
ado -
tomp@zk3.dec.com