Mail Archives: djgpp-workers/1997/09/03/08:41:03
Subject: 970831: mktime()
> mktime() "fix" for dates within a daylight savings time change (i.e.
> two possible time_t results): I think that this *is* an error, and
> mktime should return -1. However, the original problem still exists:
> converting dos m/d/y to a unix time_t. Would it be better to just
> call mktime twice at the right spot: if it fails the first time, add
> an hour, call mktime, then subtract an hour from the result.
Sounds reasonable.
What do you think about the new function below ?
Functions that know that they have valid DOS-dates should call
this instead of mktime() - in libc these are stat() [xstat.c]
and gettimeofday() [gettimeo.c].
Markus
time_t __djgpp_safe_mktime(struct tm *tptr)
{
int diff = 0;
struct tm saved_tm = *tptr;
time_t t;
/* search for a valid timestamp within the next 8 hours */
while (diff <= 8 * 3600)
{
t = mktime(tptr);
if (t != ((time_t)-1) && t >= (time_t) diff)
return t - diff;
/* restore time */
*tptr = saved_tm;
/* try 15 minutes later */
diff += 15 * 60;
tptr->tm_sec += diff;
}
/* give up */
return ((time_t)-1);
}
- Raw text -