| author | Timo Sirainen <tss@iki.fi> |
| Tue Jul 21 15:18:49 2009 -0400 (2009-07-21) | |
| branch | branch_1_0 |
| changeset 5571 | 9073f25d1b94 |
| parent 1741 | 9df02b1533b3 |
| permissions | -rw-r--r-- |
| tss@1741 | 1 |
/* Copyright (c) 2002-2003 Timo Sirainen */ |
| tss@492 | 2 |
|
| tss@492 | 3 |
#include "lib.h" |
| tss@492 | 4 |
#include "utc-offset.h" |
| tss@492 | 5 |
|
| tss@492 | 6 |
#include <sys/time.h> |
| tss@492 | 7 |
|
| tss@492 | 8 |
int utc_offset(struct tm *tm, time_t t __attr_unused__) |
| tss@492 | 9 |
{
|
| tss@492 | 10 |
#ifdef HAVE_TM_GMTOFF |
| tss@492 | 11 |
return (int) (tm->tm_gmtoff/60); |
| tss@492 | 12 |
#else |
| tss@492 | 13 |
struct tm ltm, gtm; |
| tss@492 | 14 |
int offset; |
| tss@492 | 15 |
|
| tss@492 | 16 |
/* gmtime() overwrites tm, so we need to copy it elsewhere */ |
| tss@492 | 17 |
ltm = *tm; |
| tss@492 | 18 |
tm = gmtime(&t); |
| tss@492 | 19 |
gtm = *tm; |
| tss@492 | 20 |
|
| tss@492 | 21 |
/* max offset of 24 hours */ |
| tss@5571 | 22 |
if ((ltm.tm_yday < gtm.tm_yday && ltm.tm_year == gtm.tm_year) || |
| tss@5571 | 23 |
ltm.tm_year < gtm.tm_year) |
| tss@492 | 24 |
offset = -24 * 60; |
| tss@5571 | 25 |
else if ((ltm.tm_yday > gtm.tm_yday && ltm.tm_year == gtm.tm_year) || |
| tss@5571 | 26 |
ltm.tm_year > gtm.tm_year) |
| tss@492 | 27 |
offset = 24 * 60; |
| tss@492 | 28 |
else |
| tss@492 | 29 |
offset = 0; |
| tss@492 | 30 |
|
| tss@492 | 31 |
offset += (ltm.tm_hour - gtm.tm_hour) * 60; |
| tss@492 | 32 |
offset += (ltm.tm_min - gtm.tm_min); |
| tss@492 | 33 |
|
| tss@492 | 34 |
/* restore overwritten tm */ |
| tss@492 | 35 |
*tm = ltm; |
| tss@492 | 36 |
return offset; |
| tss@492 | 37 |
#endif |
| tss@492 | 38 |
} |