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