www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/08/30/10:25:31

Subject: Re: Why TZ=EST5EDT fails
To: roe2 AT midway DOT uchicago DOT edu (Cave Newt)
Date: Tue, 30 Aug 1994 15:25:33 +0200 (MET DST)
From: Henrik Storner <storner AT olicom DOT dk>
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

About using EST5EDT with DJGPP, you write:

> the de facto standard is the 
> Unix behavior, and that treats EST5EDT exactly the way it looks:  an off-
> set of 5 hours from GMT during standard time, and an offset of 4 hours 
> during the other half of the year (i.e., a one-hour offset for DST).  
> This may very well be US-centric, but the fact remains that *this is 
> the existing Unix behavior*.

I have mixed emotions about this. On the one hand, I agree that it would
be nice to have the de-facto standards working like they normally do. On
the other hand, I hesitate to change the ctime library.

Let me suggest an alternative. Instead of trying to hack tzset() so that
it works with TZ=XXXNYYY settings, modify TZ "on the fly" to be set 
according to what Posix expects. So if TZ=EST5EDT, add some reasonable
guess as to when DST is in effect.

I've put together the following little routine, which is layered on
top of the existing tzset() routine. It first tries tzset(), and if
the result is GMT0-like, it checks the TZ setting to see if it is of the
standardzone-offset-dstzone type. If it is, then is tags a DST specifica-
tion onto the existing TZ setting, and calls tzset() again.

Use as you like - no warranty given.


#include <time.h>
#include <string.h>

void legacy_tzset(void)
{
    /*
     * Enable the use of FOOXXFDT TZ settings, even though they are
     * not POSIX compliant.
     */

	time_t now;
	char tznam[80];
	char *p = tznam;
	int  stdlen, dstlen;

	/* Try TZ as-is */
	tzset();
	if (mktime(localtime(&now)) != mktime(gmtime(&now)))
		/* locatime() and gmtime() are different, so all is well */
		return;

	/* Get the TZ setting */
	strcpy(tznam, getenv("TZ"));
	
	/* Check if it is of the form XXXNNNYYY */
	stdlen = dstlen = 0;

	p = strpbrk(tznam, "+-0123456789");
	if (p == NULL)
		/* What an odd setting - there is no offset */
		return;
	else
		stdlen = (p - tznam);

	if ((*p == '-') || (*p == '+'))
		p++;

	for (; ((*p >= '0') && (*p <= '9')); p++) ;
	dstlen = strlen(p);

	if ((stdlen == 3) && (dstlen == 3)) {
		/* 3 character standard zone, and 3 character DST zone */
		/* Need to add in a Posix rule for switch to DST */
		strcat(tznam, ",M3.5.0/02:00,M9.5.0/02:00");
		setenv("TZ", tznam, 1);
	}

	tzset();
}

-- 
Henrik Storner        | "Man is the best computer we can put aboard a space-
(storner AT olicom DOT dk)   |  craft ... and the only one that can be mass produced 
System Engineering    |  with unskilled labor."
Olicom Denmark        |                                   Wernher von Braun

- Raw text -


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