00001 /* 00002 * Copyright (c) 1997 00003 * The Regents of the University of California. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that: (1) source code distributions 00007 * retain the above copyright notice and this paragraph in its entirety, (2) 00008 * distributions including binary code include the above copyright notice and 00009 * this paragraph in its entirety in the documentation or other materials 00010 * provided with the distribution, and (3) all advertising materials mentioning 00011 * features or use of this software display the following acknowledgement: 00012 * ``This product includes software developed by the University of California, 00013 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 00014 * the University nor the names of its contributors may be used to endorse 00015 * or promote products derived from this software without specific prior 00016 * written permission. 00017 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 00018 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 00019 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00020 */ 00021 00022 #include <stdlib.h> 00023 #include <time.h> 00024 00025 /* 00026 * Returns the difference between gmt and local time in seconds. 00027 * Use gmtime() and localtime() to keep things simple. 00028 */ 00029 int32_t gmt2local(time_t t) 00030 { 00031 register int dt, dir; 00032 register struct tm *gmt, *loc; 00033 struct tm sgmt; 00034 00035 if (t == 0) 00036 t = time(NULL); 00037 gmt = &sgmt; 00038 *gmt = *gmtime(&t); 00039 loc = localtime(&t); 00040 dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 + 00041 (loc->tm_min - gmt->tm_min) * 60; 00042 00043 /* 00044 * If the year or julian day is different, we span 00:00 GMT 00045 * and must add or subtract a day. Check the year first to 00046 * avoid problems when the julian day wraps. 00047 */ 00048 dir = loc->tm_year - gmt->tm_year; 00049 if (dir == 0) 00050 dir = loc->tm_yday - gmt->tm_yday; 00051 dt += dir * 24 * 60 * 60; 00052 00053 return (dt); 00054 }