gmt2local.c revision 39297
139297Sfenner/*
239297Sfenner * Copyright (c) 1997
339297Sfenner *	The Regents of the University of California.  All rights reserved.
439297Sfenner *
539297Sfenner * Redistribution and use in source and binary forms, with or without
639297Sfenner * modification, are permitted provided that: (1) source code distributions
739297Sfenner * retain the above copyright notice and this paragraph in its entirety, (2)
839297Sfenner * distributions including binary code include the above copyright notice and
939297Sfenner * this paragraph in its entirety in the documentation or other materials
1039297Sfenner * provided with the distribution, and (3) all advertising materials mentioning
1139297Sfenner * features or use of this software display the following acknowledgement:
1239297Sfenner * ``This product includes software developed by the University of California,
1339297Sfenner * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1439297Sfenner * the University nor the names of its contributors may be used to endorse
1539297Sfenner * or promote products derived from this software without specific prior
1639297Sfenner * written permission.
1739297Sfenner * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1839297Sfenner * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1939297Sfenner * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2039297Sfenner */
2139297Sfenner
2239297Sfenner#ifndef lint
2339297Sfennerstatic const char rcsid[] =
2439297Sfenner    "@(#) $Header: gmt2local.c,v 1.2 97/01/23 22:31:25 leres Exp $ (LBL)";
2539297Sfenner#endif
2639297Sfenner
2739297Sfenner#include <sys/types.h>
2839297Sfenner#include <sys/time.h>
2939297Sfenner
3039297Sfenner#include <stdio.h>
3139297Sfenner#ifdef TIME_WITH_SYS_TIME
3239297Sfenner#include <time.h>
3339297Sfenner#endif
3439297Sfenner
3539297Sfenner#include "gnuc.h"
3639297Sfenner#ifdef HAVE_OS_PROTO_H
3739297Sfenner#include "os-proto.h"
3839297Sfenner#endif
3939297Sfenner
4039297Sfenner#include "gmt2local.h"
4139297Sfenner
4239297Sfenner/*
4339297Sfenner * Returns the difference between gmt and local time in seconds.
4439297Sfenner * Use gmtime() and localtime() to keep things simple.
4539297Sfenner */
4639297Sfennerint32_t
4739297Sfennergmt2local(time_t t)
4839297Sfenner{
4939297Sfenner	register int dt, dir;
5039297Sfenner	register struct tm *gmt, *loc;
5139297Sfenner	struct tm sgmt;
5239297Sfenner
5339297Sfenner	if (t == 0)
5439297Sfenner		t = time(NULL);
5539297Sfenner	gmt = &sgmt;
5639297Sfenner	*gmt = *gmtime(&t);
5739297Sfenner	loc = localtime(&t);
5839297Sfenner	dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
5939297Sfenner	    (loc->tm_min - gmt->tm_min) * 60;
6039297Sfenner
6139297Sfenner	/*
6239297Sfenner	 * If the year or julian day is different, we span 00:00 GMT
6339297Sfenner	 * and must add or subtract a day. Check the year first to
6439297Sfenner	 * avoid problems when the julian day wraps.
6539297Sfenner	 */
6639297Sfenner	dir = loc->tm_year - gmt->tm_year;
6739297Sfenner	if (dir == 0)
6839297Sfenner		dir = loc->tm_yday - gmt->tm_yday;
6939297Sfenner	dt += dir * 24 * 60 * 60;
7039297Sfenner
7139297Sfenner	return (dt);
7239297Sfenner}
73