From 3cb2c2f02fb20de5480c507b96c30da18a52cb81 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Fri, 19 Feb 2016 20:24:50 +1030 Subject: [PATCH] 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 | 7 ++++++- poppler/DateInfo.cc | 27 +++++++++++++++++++++++++++ poppler/DateInfo.h | 4 ++++ 6 files changed, 42 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..2fe6274 100644 --- a/glib/poppler-date.cc +++ b/glib/poppler-date.cc @@ -58,11 +58,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/poppler/DateInfo.cc b/poppler/DateInfo.cc index 563204a..cbe538d 100644 --- a/poppler/DateInfo.cc +++ b/poppler/DateInfo.cc @@ -116,3 +116,30 @@ GooString *timeToDateString(time_t *timet) { return dateString; } +#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); +#ifdef HAVE_GMTIME_R + gmtime_r(&utc, &tm_utc); +#else + tm_utc = *gmtime(&utc); +#endif + 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/poppler/DateInfo.h b/poppler/DateInfo.h index 116350f..956699e 100644 --- a/poppler/DateInfo.h +++ b/poppler/DateInfo.h @@ -32,4 +32,8 @@ GBool parseDateString(const char *string, int *year, int *month, int *day, int * */ GooString *timeToDateString(time_t *timet); +#ifndef HAVE_TIMEGM +time_t timegm(struct tm *tm); +#endif + #endif -- 2.1.4