www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/06/21/04:29:34

Date: Tue, 21 Jun 1994 09:51:31 +0200
From: terra AT diku DOT dk
To: meetze AT charlie DOT ece DOT scarolina DOT edu
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: gmtime, ctime and localtime functions

djgpp has problems with its time functions, especially in the presense
of a TZ environment variable.  There are three situations that will
give different behaviour

1. No TZ variable at all.
2. TZ variable set from Dos (so GO32 can see it).
3. TZ varibale set within the C program (so GO32 cannot see it).

The only situation where it works correctly (I *think*) is situation
1 when you are in timezone EST.  I assume you are one hour away from
that, possibly because of daylight savings.

When you check out the source code for the library -- and these
functions are very, very obscure source-code-wise -- you will find
that the origin of the problems is gettimeofday which returns a value
that GO32 has tampered with.

The described problems are also the cause behind the "File has changed
on disk" messages from Demacs.

I solved this for Emacs 19.23 with the replacement below.  Note the
use of gettime/getdate that go directly to Dos.  Unfortunately, it
must be initialized at startup and every time TZ is changed by the
C program -- not very clean, but it seems to work.

Morten Welinder
terra AT diku DOT dk



-------------------------- from emacs 19.24 ----------------------------

/* When time zones are set from Ms-Dos too may C-libraries are playing
   tricks with time values.  We solve this by defining our own version
   of `gettimeofday' bypassing GO32.  Our version needs to be initialized
   once and after each call to `tzset' with TZ changed.  */

static int daylight, gmtoffset;

int
gettimeofday (struct timeval *tp, struct timezone *tzp)
{
  if (tp)
    {
      struct time t;
      struct date d;
      struct tm tmrec;

      gettime (&t);
      getdate (&d);
      tmrec.tm_year = d.da_year - 1900;
      tmrec.tm_mon = d.da_mon - 1;
      tmrec.tm_mday = d.da_day;
      tmrec.tm_hour = t.ti_hour;
      tmrec.tm_min = t.ti_min;
      tmrec.tm_sec = t.ti_sec;
      tmrec.tm_gmtoff = gmtoffset;
      tmrec.tm_isdst = daylight;
      tp->tv_sec = mktime (&tmrec);
      tp->tv_usec = t.ti_hund * (1000000 / 100);
    }
  if (tzp)
    {
      tzp->tz_minuteswest = gmtoffset;
      tzp->tz_dsttime = daylight;
    }
  return 0;
}

void
init_gettimeofday ()
{
  time_t ltm, gtm;
  struct tm *lstm;

  daylight = 0;
  gmtoffset = 0;
  ltm = gtm = time (NULL);
  ltm = mktime (lstm = localtime (&ltm));
  gtm = mktime (gmtime (&gtm));
  daylight = lstm->tm_isdst;
  gmtoffset = (int)(gtm - ltm) / 60;
}

-------------------------- from emacs 19.24 ----------------------------

(Code fragment showing you how to set TZ)


  /* Time zone determined from country code.  To make this possible, the
     country code may not span more than one time zone.  In other words,
     in the USA, you lose.  */
  switch (dos_country_code)
    {
    case 31: /* Belgium */
    case 32: /* The Netherlands */
    case 33: /* France */
    case 34: /* Spain */
    case 36: /* Hungary */
    case 38: /* Yugoslavia (or what's left of it?) */
    case 39: /* Italy */
    case 41: /* Switzerland */
    case 42: /* Tjekia */
    case 45: /* Denmark */
    case 46: /* Sweden */
    case 47: /* Norway */
    case 48: /* Poland */
    case 49: /* Germany */
      /* Daylight saving from last Sunday in March to last Sunday in
	 September, both at 2AM.  */
      setenv ("TZ", "MET-01METDST-02,M3.5.0/02:00,M9.5.0/02:00", 0);
      break;
    case 44: /* United Kingdom */
    case 351: /* Portugal */
    case 354: /* Iceland */
      setenv ("TZ", "GMT+00", 0);
      break;
    case 81: /* Japan */
    case 82: /* Korea */
      setenv ("TZ", "???-09", 0);
      break;
    case 90: /* Turkey */
    case 358: /* Finland */
    case 972: /* Israel */
      setenv ("TZ", "EET-02", 0);
      break;
    }
  tzset ();
  init_gettimeofday ();
-------------------------- from emacs 19.24 ----------------------------

- Raw text -


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