gmt2local.c revision 56893
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[] =
2456893Sfenner    "@(#) $Header: /tcpdump/master/tcpdump/gmt2local.c,v 1.3 1999/11/21 09:36:47 fenner Exp $ (LBL)";
2539297Sfenner#endif
2639297Sfenner
2756893Sfenner#ifdef HAVE_CONFIG_H
2856893Sfenner#include "config.h"
2956893Sfenner#endif
3056893Sfenner
3139297Sfenner#include <sys/types.h>
3239297Sfenner#include <sys/time.h>
3339297Sfenner
3439297Sfenner#include <stdio.h>
3539297Sfenner#ifdef TIME_WITH_SYS_TIME
3639297Sfenner#include <time.h>
3739297Sfenner#endif
3839297Sfenner
3939297Sfenner#include "gnuc.h"
4039297Sfenner#ifdef HAVE_OS_PROTO_H
4139297Sfenner#include "os-proto.h"
4239297Sfenner#endif
4339297Sfenner
4439297Sfenner#include "gmt2local.h"
4539297Sfenner
4639297Sfenner/*
4739297Sfenner * Returns the difference between gmt and local time in seconds.
4839297Sfenner * Use gmtime() and localtime() to keep things simple.
4939297Sfenner */
5039297Sfennerint32_t
5139297Sfennergmt2local(time_t t)
5239297Sfenner{
5339297Sfenner	register int dt, dir;
5439297Sfenner	register struct tm *gmt, *loc;
5539297Sfenner	struct tm sgmt;
5639297Sfenner
5739297Sfenner	if (t == 0)
5839297Sfenner		t = time(NULL);
5939297Sfenner	gmt = &sgmt;
6039297Sfenner	*gmt = *gmtime(&t);
6139297Sfenner	loc = localtime(&t);
6239297Sfenner	dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
6339297Sfenner	    (loc->tm_min - gmt->tm_min) * 60;
6439297Sfenner
6539297Sfenner	/*
6639297Sfenner	 * If the year or julian day is different, we span 00:00 GMT
6739297Sfenner	 * and must add or subtract a day. Check the year first to
6839297Sfenner	 * avoid problems when the julian day wraps.
6939297Sfenner	 */
7039297Sfenner	dir = loc->tm_year - gmt->tm_year;
7139297Sfenner	if (dir == 0)
7239297Sfenner		dir = loc->tm_yday - gmt->tm_yday;
7339297Sfenner	dt += dir * 24 * 60 * 60;
7439297Sfenner
7539297Sfenner	return (dt);
7639297Sfenner}
77