poppler/DateInfo.cc | 61 ++++++++++++++++++++-------------------------------- poppler/DateInfo.h | 1 2 files changed, 25 insertions(+), 37 deletions(-)
New commits: commit 5981258d058cf93b6e18b5f33d7cd0e6380556e3 Author: Adrian Johnson <[email protected]> Date: Sun Nov 25 22:12:12 2018 +0100 Include timezone in timeToDateString() diff --git a/poppler/DateInfo.cc b/poppler/DateInfo.cc index 67be91cf..f9e62806 100644 --- a/poppler/DateInfo.cc +++ b/poppler/DateInfo.cc @@ -6,7 +6,7 @@ // Copyright (C) 2009 Carlos Garcia Campos <[email protected]> // Copyright (C) 2015 André Guerreiro <[email protected]> // Copyright (C) 2015 André Esser <[email protected]> -// Copyright (C) 2016 Adrian Johnson <[email protected]> +// Copyright (C) 2016, 2018 Adrian Johnson <[email protected]> // // To see a description of the changes please see the Changelog file that // came with your tarball or type make ChangeLog if you are building from git @@ -76,42 +76,29 @@ bool parseDateString(const char *dateString, int *year, int *month, int *day, in return false; } -// 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(nullptr); - 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) +{ + const 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. + const time_t timeg = timegm(&localtime_tm); + const 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) { + 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 4fb03c3e..d726b310 100644 --- a/poppler/DateInfo.h +++ b/poppler/DateInfo.h @@ -31,6 +31,7 @@ bool parseDateString(const char *string, int *year, int *month, int *day, int *h /* 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); _______________________________________________ poppler mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/poppler
