mktime() failure vs 31-DEC-1969 23:59:59 GMT
This has probably been discussed before, but perhaps someone can bring me up to speed. 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? Here's a sample program: #include <time.h> #include <strings.h> #include <string.h> #include <stdio.h> main() { struct tm stm; time_t timet; memset(&stm, 0, sizeof(stm)); putenv("TZ=GMT0"); stm.tm_sec = 59; stm.tm_min = 59; stm.tm_hour = 23; stm.tm_mday = 31; stm.tm_mon = 11; stm.tm_year = 69; stm.tm_isdst = -1; timet = mktime(&stm); printf("timet = %ld\n",timet); } thanks, - Tom
The standard way in ANSI C to detect errors when the return value is ambiguous is using `errno': { errno = 0; timet = mktime (&stm); if (timet == (time_t) -1 && errno != 0) perror ("mktime"); }
Hi Roland, Thanks very much for the response.
The standard way in ANSI C to detect errors when the return value is ambiguous is using `errno':
Unfortunately, the standards say that "no errors are defined" for mktime(). (ie - testing for errno is not dependable) The standards I'm looking at are XPG4 (issue 1) and XPG4 (issue 2) aka UNIX 95 or Spec1170. Each of these standards are supersets of ANSI C, so I suspect ANSI C does not define errors for mktime() either. - Tom
How do we distinguish between the two?
We don't, as far as I know. ANSI C, as noted, doesn't specify that "mktime()" sets "errno", so it appears there's no way of distinguishing between "a valid 'struct tm' value that happens to translate to a 'time_t' value of -1" and "an error". (It also doesn't specify whether "tm_wday" and "tm_yday" are set if the "struct tm" can't be converted, so it doesn't seem to allow you to stuff some impossible value there and check whether it got set to some non-impossible value and, if it did, assume it succeeded.)
participants (4)
-
guy@netapp.com -
J.T. Conklin -
Roland McGrath -
tomp@zk3.dec.com