delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1997/09/03/08:41:03

Message-Id: <199709031239.IAA00721@delorie.com>
From: Oberhumer Markus <k3040e4 AT c210 DOT edvz DOT uni-linz DOT ac DOT at>
Subject: 970831: mktime()
To: dj AT delorie DOT com (DJ Delorie), djgpp-workers AT delorie DOT com (djgpp-workers)
Date: Wed, 3 Sep 1997 14:33:21 +0200 (METDST)
Return-Read-To: markus DOT oberhumer AT jk DOT uni-linz DOT ac DOT at
Mime-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019