🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #3785

Re: timezone macro for autoconf?

#3785
From: Dan Kegel
Date: Sun, 23 Apr 2000 00:00
118 lines
4545 bytes
Russ Allbery wrote:
> >>> Does anyone have a good autoconf macro for figuring out
> >>> how to get at the timezone offset (the extern int) portably?
> >> No, but I need to write one for INN as well.
> > The most portable way to get the time zone offset is to invoke localtime
> > and gmtime on the same time_t value, and then compute the offset
> > yourself from the two broken-down times.  This works on any system that
> > conforms to either K&R C or to Standard C (either 1989 or 1999).
> Right.  This is what INN currently falls back to if struct tm doesn't have
> the tm_gmtoff member.

PHP3 has a function gmmktime() which is the inverse of gmtime(),
i.e. it takes a struct tm * in GMT and returns a time_t.  
Boy, do I wish Unix had that function.

Here's the code PHP3 uses, by the way.  It relies on mktime()
setting the tm_gmtoff field in the structure the argument points to!
Note that that behavior doesn't seem to be specified in SUS, e.g.
http://www.opengroup.org/onlinepubs/007908799/xsh/mktime.html

I take it from the remarks above that INN does it this way
if tm_gmtoff is present?

- Dan

void _php3_mktime(INTERNAL_FUNCTION_PARAMETERS, int gm)
{
	pval *arguments[7];
	struct tm *ta;
	time_t t;
	int i, seconds, gmadjust=0, arg_count = ARG_COUNT(ht);

	if (arg_count > 7 || getParametersArray(ht, arg_count, arguments) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	/* convert supplied arguments to longs */
	for (i = 0; i < arg_count; i++) {
		convert_to_long(arguments[i]);
	}

	/* get the current time in seconds from epoch */
	t=time(NULL);
#if HAVE_TZSET
	tzset();
#endif
		ta = localtime(&t);

	/* Let the system decide later whether the time is
	   in daylight savings time or not. Unless the user
	   overrides this, of course. */
	ta->tm_isdst = -1;

	/* now we apply the specified parameters to the timestamp */
	switch(arg_count) {
	case 7:
		ta->tm_isdst = arguments[6]->value.lval;
		/* fall-through */
	case 6:
		ta->tm_year = arguments[5]->value.lval;
		if (ta->tm_year < 70)
			ta->tm_year += 100;
		else if (ta->tm_year > 1000)
			ta->tm_year -= 1900;
		/* fall-through */
	case 5:
		ta->tm_mday = arguments[4]->value.lval;
		/* fall-through */
	case 4:
		ta->tm_mon = arguments[3]->value.lval - 1;
		/* fall-through */
	case 3:
		ta->tm_sec = arguments[2]->value.lval;
		/* fall-through */
	case 2:
		ta->tm_min = arguments[1]->value.lval;
		/* fall-through */
	case 1:
		ta->tm_hour = arguments[0]->value.lval;
	case 0:
		break;
	}

	seconds =  mktime(ta);

	/* mktime(), which *does* modify the structure its argument points to,
	   very nicely just filled ta->tm_gmtoff with the right value for us to
	   use. So add it to seconds, if we want GMT. */
	if (gm) {
#if HAVE_TM_GMTOFF
		gmadjust = ta->tm_gmtoff;
#else
		gmadjust = timezone;
#endif
		seconds += gmadjust;
	}

	return_value->value.lval = seconds;
	return_value->type = IS_LONG;
}

/* {{{ proto int mktime(int hour, int min, int sec, int mon, int mday, int year)
   Get UNIX timestamp for a date */
void php3_mktime(INTERNAL_FUNCTION_PARAMETERS)
{
	_php3_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */

/* {{{ proto int gmmktime(int hour, int min, int sec, int mon, int mday, int year)
   Get UNIX timestamp for a GMT date */
void php3_gmmktime(INTERNAL_FUNCTION_PARAMETERS)
{
	_php3_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */

-- 
Entia non sunt multiplicanda praeter necessitatem.
Technical Details
Message-ID: <3903E142.FBD4E5E8@alumni.caltech.edu>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <38FBB4DB.DE12E1C4@alumni.caltech.edu> <ylog78chos.fsf@windlord.stanford.edu> <8di5uf$8p0$1@ruby.twinsun.com> <ylaeiryr7f.fsf@windlord.stanford.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10c696,8ce1c34c9f5a1eaa
X-Google-Attributes: gid10c696,public
X-Google-Thread: 109f73,8ce1c34c9f5a1eaa
X-Google-Attributes: gid109f73,public
X-Google-Thread: fc89c,8ce1c34c9f5a1eaa
X-Google-Attributes: gidfc89c,public
From: Dan Kegel <dank@alumni.caltech.edu>
Subject: Re: timezone macro for autoconf?
Date: 2000/04/23
Message-ID: <3903E142.FBD4E5E8@alumni.caltech.edu>
X-Deja-AN: 614875515
Content-Transfer-Encoding: 7bit
References: <38FBB4DB.DE12E1C4@alumni.caltech.edu> <ylog78chos.fsf@windlord.stanford.edu> <8di5uf$8p0$1@ruby.twinsun.com> <ylaeiryr7f.fsf@windlord.stanford.edu>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: abuse@swbell.net
X-Trace: news.swbell.net 956555463 63.201.176.178 (Mon, 24 Apr 2000 00:51:03 CDT)
Organization: SBC Internet Services
MIME-Version: 1.0
NNTP-Posting-Date: Mon, 24 Apr 2000 00:51:03 CDT
Newsgroups: comp.unix.programmer,comp.os.linux.development.apps,gnu.utils.help