From dc9c7333e52c5066cc14be3363d77e3b7c38e5d8 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 23 Feb 2016 21:01:49 +1030 Subject: [PATCH 2/2] glib: return date in UTC instead of local time Bug 94173 --- ConfigureChecks.cmake | 1 + config.h.cmake | 3 +++ configure.ac | 1 + glib/poppler-date.cc | 8 +++++++- goo/glibc.cc | 24 ++++++++++++++++++++++++ goo/glibc.h | 4 ++++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index d3f5732..345f29c 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -30,6 +30,7 @@ check_function_exists(ftell64 HAVE_FTELL64) check_function_exists(pread64 HAVE_PREAD64) check_function_exists(lseek64 HAVE_LSEEK64) check_function_exists(gmtime_r HAVE_GMTIME_R) +check_function_exists(timegm HAVE_TIMEGM) check_function_exists(gettimeofday HAVE_GETTIMEOFDAY) check_function_exists(localtime_r HAVE_LOCALTIME_R) check_function_exists(popen HAVE_POPEN) diff --git a/config.h.cmake b/config.h.cmake index 440a13d..e0f9473 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -58,6 +58,9 @@ /* Defines if gmtime_r is available on your system */ #cmakedefine HAVE_GMTIME_R 1 +/* Defines if timegm is available on your system */ +#cmakedefine HAVE_TIMEGM 1 + /* Define if you have the iconv() function and it works. */ #cmakedefine HAVE_ICONV 1 diff --git a/configure.ac b/configure.ac index 45575be..93b7856 100644 --- a/configure.ac +++ b/configure.ac @@ -178,6 +178,7 @@ AC_LANG_CPLUSPLUS AC_CHECK_DECL(gettimeofday, [AC_CHECK_FUNC(gettimeofday, AC_DEFINE(HAVE_GETTIMEOFDAY, 1, [Defines if gettimeofday is available on your system]))],[],[#include ]) AC_CHECK_FUNC(localtime_r, AC_DEFINE(HAVE_LOCALTIME_R, 1, [Defines if localtime_r is available on your system])) AC_CHECK_FUNC(gmtime_r, AC_DEFINE(HAVE_GMTIME_R, 1, [Defines if gmtime_r is available on your system])) +AC_CHECK_FUNC(timegm, AC_DEFINE(HAVE_TIMEGM, 1, [Defines if timegm is available on your system])) AC_CHECK_FUNC(rand_r, AC_DEFINE(HAVE_RAND_R, 1, [Defines if rand_r is available on your system])) dnl ##### Check for extra libraries needed by X. (LynxOS needs this.) diff --git a/glib/poppler-date.cc b/glib/poppler-date.cc index e3141c1..a67429a 100644 --- a/glib/poppler-date.cc +++ b/glib/poppler-date.cc @@ -17,6 +17,7 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ +#include #include #include "poppler-date.h" @@ -58,11 +59,16 @@ poppler_date_parse (const gchar *date, time.tm_isdst = -1; /* 0 = DST off, 1 = DST on, -1 = don't know */ /* compute tm_wday and tm_yday and check date */ - retval = mktime (&time); + retval = timegm (&time); if (retval == (time_t) - 1) return FALSE; *timet = retval; + gint64 offset = (tz_hour*60 + tz_minute)*60; + if (tz == '-') + offset *= -1; + *timet -= offset; + return TRUE; } diff --git a/goo/glibc.cc b/goo/glibc.cc index b1589db..47093b8 100644 --- a/goo/glibc.cc +++ b/goo/glibc.cc @@ -30,3 +30,27 @@ struct tm *localtime_r(const time_t *timep, struct tm *result) return lt; } #endif + +#ifndef HAVE_TIMEGM +// Get offset of local time from UTC in seconds. DST is ignored. +static time_t getLocalTimeZoneOffset() +{ + time_t utc, local; + struct tm tm_utc; + time (&utc); + gmtime_r(&utc, &tm_utc); + local = mktime(&tm_utc); + return difftime(utc, local); +} + +time_t timegm(struct tm *tm) +{ + tm->tm_isdst = 0; + time_t t = mktime(tm); + if (t == -1) + return t; + + t += getLocalTimeZoneOffset(); + return t; +} +#endif diff --git a/goo/glibc.h b/goo/glibc.h index 527c8b3..53d7bfd 100644 --- a/goo/glibc.h +++ b/goo/glibc.h @@ -25,6 +25,10 @@ struct tm *gmtime_r(const time_t *timep, struct tm *result); struct tm *localtime_r(const time_t *timep, struct tm *result); #endif +#ifndef HAVE_TIMEGM +time_t timegm(struct tm *tm); +#endif + }; #endif // GLIBC_H -- 2.1.4