1st of 2 forwarded messages from Bradley White: time2posix/posix2time

Arthur David Olson ado
Tue Jun 9 18:43:19 UTC 1992


The attached comes from Bradley White, bww+ at cs.cmu.edu.
Your comments will be appreciated!

				--ado

I have found [time2posix() and posix2time()] invaluable when
dealing with things like external time formats, or programs that want to
wake up every minute (as opposed to every 60 seconds), on systems that
allow for leap seconds....

	/*
	** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
	** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which
	** is not the case if we are accounting for leap seconds.
	** So, we provide the following conversion routines for use
	** when exchanging timestamps with POSIX conforming systems.
	*/

	static long
	leapcorr(timep)
	time_t *	timep;
	{
		register struct state *		sp;
		register struct lsinfo *	lp;
		register int			i;

		if (!lcl_is_set)
			(void) tzset();
		sp = lclptr;
		i = sp->leapcnt;
		while (--i >= 0) {
			lp = &sp->lsis[i];
			if (*timep >= lp->ls_trans)
				return lp->ls_corr;
		}
		return 0L;
	}

	time_t
	time2posix(t)
	time_t	t;
	{
		return t - leapcorr(&t);
	}

	time_t
	posix2time(t)
	time_t	t;
	{
		time_t	x, y;

		/*
		** For a positive leap second hit, the result
		** is not unique.  For a negative leap second
		** hit, the corresponding time doesn't exist,
		** so we return an adjacent second.
		*/
		x = t + leapcorr(&t);
		y = x - leapcorr(&x);
		if (y < t) {
			do {
				x++;
				y = x - leapcorr(&x);
			} while (y < t);
			if (t != y)
				return x-1;
		} else if (y > t) {
			do {
				--x;
				y = x - leapcorr(&x);
			} while (y > t);
			if (t != y)
				return x+1;
		}
		return x;
	}



More information about the tz mailing list