From 73ebb68b0749573a97d3354d111f7174717d29b7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20P=C3=ADsa=C5=99?= Date: Wed, 2 Dec 2009 14:05:37 +0100 Subject: [PATCH] Switch TZ at mktime() temporarily --- src/isds.c | 3 ++- src/utils.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/utils.h | 8 ++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/isds.c b/src/isds.c index 8a6048d..8de970b 100644 --- a/src/isds.c +++ b/src/isds.c @@ -974,8 +974,9 @@ static isds_error timestring2timeval(const xmlChar *string, } /* Convert to time_t */ - /* FIXME: Don't run-time TZ */ + switch_tz_to_utc(); (*time)->tv_sec = mktime(&broken); + switch_tz_to_native(); if ((*time)->tv_sec == (time_t) -1) { free(*time); *time = NULL; return IE_DATE; diff --git a/src/utils.c b/src/utils.c index 8418424..5c1155a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,11 +1,16 @@ +#define _XOPEN_SOURCE 500 /* For strdup(3) */ +#define _POSIX_C_SOURCE 200112L /* For setenv() */ #include #include #include #include #include +#include #include "utils.h" #include "cencode.h" +char *tz_orig; /* Copy of original TZ variable */ + /* Concatenate two strings into newly allocated buffer. * You must free() them, when you don't need it anymore. * Any of the arguments can be NULL meaning empty string. @@ -218,3 +223,39 @@ _hidden char *b64encode(const void *plain, const size_t length) { return buffer; } + +/* Switches time zone to UTC. + * XXX: This is not reentrant and not thread-safe */ +_hidden void switch_tz_to_utc(void) { + char *tz; + + tz = getenv("TZ"); + if (tz) { + tz_orig = strdup(tz); + if (!tz_orig) + PANIC("Can not back original time zone up"); + } else { + tz_orig = NULL; + } + + if (setenv("TZ", "", 1)) + PANIC("Can not change time zone to UTC temporarily"); + + tzset(); +} + + +/* Switches time zone to original value. + * XXX: This is not reentrant and not thread-safe */ +_hidden void switch_tz_to_native(void) { + if (tz_orig) { + if (setenv("TZ", tz_orig, 1)) + PANIC("Can not restore time zone by setting TZ variable"); + free(tz_orig); + tz_orig = NULL; + } else { + if(unsetenv("TZ")) + PANIC("Can not restore time zone by unsetting TZ variable"); + } + tzset(); +} diff --git a/src/utils.h b/src/utils.h index 8f55c1c..ea4eb73 100644 --- a/src/utils.h +++ b/src/utils.h @@ -64,4 +64,12 @@ char *utf82locale(const char *utf); * error. You must free it. */ char *b64encode(const void *plain, const size_t length); +/* Switches time zone to UTC. + * XXX: This is not reentrant and not thread-safe */ +_hidden void switch_tz_to_utc(void); + +/* Switches time zone to original value. + * XXX: This is not reentrant and not thread-safe */ +_hidden void switch_tz_to_native(void); + #endif -- 2.11.4.GIT