From 4270c51a98b6cc77a8337c6120195be4f620c775 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Wed, 4 Sep 2002 19:13:14 +0000 Subject: New. Like mktime(3), but assumes the input time is UTC. * e-time-utils.c (e_mktime_utc): New. Like mktime(3), but assumes the input time is UTC. (e_localtime_with_offset): New. Like localtime_r(3), but also returns an offset from UTC. svn path=/trunk/; revision=17971 --- e-util/ChangeLog | 7 ++++++ e-util/e-time-utils.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++----- e-util/e-time-utils.h | 5 +++++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/e-util/ChangeLog b/e-util/ChangeLog index 47b9bf138e..c58ab2ef73 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -1,3 +1,10 @@ +2002-09-04 Dan Winship + + * e-time-utils.c (e_mktime_utc): New. Like mktime(3), but assumes + the input time is UTC. + (e_localtime_with_offset): New. Like localtime_r(3), but also + returns an offset from UTC. + 2002-08-29 Dan Winship * e-list.c (e_list_destroy): Don't call g_list_foreach with the diff --git a/e-util/e-time-utils.c b/e-util/e-time-utils.c index f91695f977..ecc3fc9602 100644 --- a/e-util/e-time-utils.c +++ b/e-util/e-time-utils.c @@ -10,13 +10,17 @@ #include -/* We need this for strptime. */ -#define _XOPEN_SOURCE 500 -#define __USE_XOPEN +#ifdef __linux__ +/* We need this to get a prototype for strptime. */ +#define _GNU_SOURCE +#endif /* __linux__ */ + #include #include -#undef _XOPEN_SOURCE -#undef __USE_XOPEN + +#ifdef __linux__ +#undef _GNU_SOURCE +#endif /* __linux__ */ #include #include @@ -426,3 +430,50 @@ e_time_format_time (struct tm *date_tm, if (strftime (buffer, buffer_size, format, date_tm) == 0) buffer[0] = '\0'; } + + +/* Like mktime(3), but assumes UTC instead of local timezone. */ +time_t +e_mktime_utc (struct tm *tm) +{ + time_t tt; + + tm->tm_isdst = -1; + tt = mktime (tm); + +#if defined (HAVE_TM_GMTOFF) + tt += tm->tm_gmtoff; +#elif defined (HAVE_TIMEZONE) + if (tm->tm_isdst > 0) { + #if defined (HAVE_ALTZONE) + tt -= altzone; + #else /* !defined (HAVE_ALTZONE) */ + tt -= (timezone - 3600); + #endif + } else + tt -= timezone; +#endif + + return tt; +} + +/* Like localtime_r(3), but also returns an offset in minutes after UTC. + (Calling gmtime with tt + offset would generate the same tm) */ +void +e_localtime_with_offset (time_t tt, struct tm *tm, int *offset) +{ + localtime_r (&tt, tm); + +#if defined (HAVE_TM_GMTOFF) + *offset = tm->tm_gmtoff; +#elif defined (HAVE_TIMEZONE) + if (tm->tm_isdst > 0) { + #if defined (HAVE_ALTZONE) + *offset = -altzone; + #else /* !defined (HAVE_ALTZONE) */ + *offset = -(timezone - 3600); + #endif + } else + *offset = -timezone; +#endif +} diff --git a/e-util/e-time-utils.h b/e-util/e-time-utils.h index f6d85df5ac..0b081dadd4 100644 --- a/e-util/e-time-utils.h +++ b/e-util/e-time-utils.h @@ -48,6 +48,11 @@ void e_time_format_time (struct tm *date_tm, int buffer_size); +/* Like mktime(3), but assumes UTC instead of local timezone. */ +time_t e_mktime_utc (struct tm *timeptr); +/* Like localtime_r(3), but also returns an offset in minutes after UTC. + (Calling gmtime with tt + offset would generate the same tm) */ +void e_localtime_with_offset (time_t tt, struct tm *tm, int *offset); #endif /* E_TIME_UTILS */ -- cgit