From 1b774d7718b398ee0b4241b0c61305c87bc00efe Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 7 Sep 2017 22:54:56 +0930 Subject: [PATCH] Include timezone in timeToDateString() --- poppler/DateInfo.cc | 59 ++++++++++++++++++++++------------------------------- poppler/DateInfo.h | 1 + 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/poppler/DateInfo.cc b/poppler/DateInfo.cc index fef3f00a..2580dab9 100644 --- a/poppler/DateInfo.cc +++ b/poppler/DateInfo.cc @@ -77,41 +77,30 @@ GBool parseDateString(const char *dateString, int *year, int *month, int *day, i } // Convert time to PDF date string -GooString *timeToDateString(time_t *timet) { - GooString *dateString; - char s[5]; - struct tm *gt; - size_t len; - time_t timep = timet ? *timet : time(NULL); - struct tm t; - - gt = gmtime_r (&timep, &t); - - dateString = new GooString ("D:"); - - /* Year YYYY */ - len = strftime (s, sizeof(s), "%Y", gt); - dateString->append (s, len); - - /* Month MM */ - len = strftime (s, sizeof(s), "%m", gt); - dateString->append (s, len); - - /* Day DD */ - len = strftime (s, sizeof(s), "%d", gt); - dateString->append (s, len); - - /* Hour HH */ - len = strftime (s, sizeof(s), "%H", gt); - dateString->append (s, len); - - /* Minute mm */ - len = strftime (s, sizeof(s), "%M", gt); - dateString->append (s, len); - - /* Second SS */ - len = strftime (s, sizeof(s), "%S", gt); - dateString->append (s, len); +GooString *timeToDateString(time_t *timeA) +{ + time_t timet = timeA ? *timeA : time(NULL); + + struct tm localtime_tm; + localtime_r (&timet, &localtime_tm); + + char buf[50]; + strftime (buf, sizeof(buf), "D:%Y%m%d%H%M%S", &localtime_tm); + GooString *dateString = new GooString(buf); + + // strftime "%z" does not work on windows (it prints zone name, not offset) + // calculate time zone offset by comparing local and gmtime time_t value for same + // time. + time_t timeg = timegm(&localtime_tm); + time_t offset = difftime(timeg, timet); // find time zone offset in seconds + if (offset > 0) { + dateString->appendf ("+{0:02d}'{1:02d}", offset/3600, (offset%3600)/60); + } else if (offset < 0) { + offset = -offset; + dateString->appendf ("-{0:02d}'{1:02d}", offset/3600, (offset%3600)/60); + } else { + dateString->append("Z"); + } return dateString; } diff --git a/poppler/DateInfo.h b/poppler/DateInfo.h index 468dd93b..1956e904 100644 --- a/poppler/DateInfo.h +++ b/poppler/DateInfo.h @@ -32,6 +32,7 @@ GBool parseDateString(const char *string, int *year, int *month, int *day, int * /* Converts the time_t into a PDF Date format string. * If timet is NULL, current time is used. + * Returns new GooString. Free with delete. */ GooString *timeToDateString(time_t *timet); -- 2.11.0