[poppler] poppler/DateInfo.cc poppler/DateInfo.h

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Jan 10 16:54:23 UTC 2019


Rebased ref, commits from common ancestor:
commit fbb6509c05bad7e6bfeba8b4bb2cdc4a0e2d297f
Author: Adrian Johnson <ajohnson at redneon.com>
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..4cbc0959 100644
--- a/poppler/DateInfo.cc
+++ b/poppler/DateInfo.cc
@@ -6,7 +6,7 @@
 // Copyright (C) 2009 Carlos Garcia Campos <carlosgc at gnome.org>
 // Copyright (C) 2015 André Guerreiro <aguerreiro1985 at gmail.com>
 // Copyright (C) 2015 André Esser <bepandre at hotmail.com>
-// Copyright (C) 2016 Adrian Johnson <ajohnson at redneon.com>
+// Copyright (C) 2016, 2018 Adrian Johnson <ajohnson at redneon.com>
 //
 // 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(nullptr);
+
+  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);
 


More information about the poppler mailing list