[poppler] CMakeLists.txt glib/poppler-document.cc poppler/DateInfo.cc poppler/DateInfo.h poppler/Makefile.am qt4/src qt/poppler-document.cc utils/pdfinfo.cc utils/pdftohtml.cc

Albert Astals Cid aacid at kemper.freedesktop.org
Fri Aug 29 14:06:56 PDT 2008


 CMakeLists.txt                      |    2 +
 glib/poppler-document.cc            |   29 +---------------
 poppler/DateInfo.cc                 |   62 ++++++++++++++++++++++++++++++++++++
 poppler/DateInfo.h                  |   27 +++++++++++++++
 poppler/Makefile.am                 |    2 +
 qt/poppler-document.cc              |   22 +-----------
 qt4/src/poppler-annotation-helper.h |   16 +--------
 qt4/src/poppler-document.cc         |   52 ++----------------------------
 utils/pdfinfo.cc                    |   16 +--------
 utils/pdftohtml.cc                  |    9 +----
 10 files changed, 111 insertions(+), 126 deletions(-)

New commits:
commit 8f1deb3f8000bdeb845a6c786a654bc7eb684f0a
Author: Albert Astals Cid <aacid at kde.org>
Date:   Fri Aug 29 23:06:19 2008 +0200

    Are we a lib or aren't we? Unify String to Date parsing so we all behave the same way

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 026d4c1..63ec8bd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -139,6 +139,7 @@ set(poppler_SRCS
   poppler/Catalog.cc
   poppler/CharCodeToUnicode.cc
   poppler/CMap.cc
+  poppler/DateInfo.cc
   poppler/Decrypt.cc
   poppler/Dict.cc
   poppler/Error.cc
@@ -267,6 +268,7 @@ if(ENABLE_XPDF_HEADERS)
     poppler/Catalog.h
     poppler/CharCodeToUnicode.h
     poppler/CMap.h
+    poppler/DateInfo.h
     poppler/Decrypt.h
     poppler/Dict.h
     poppler/Error.h
diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index 25b3478..1d7e04f 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -28,6 +28,7 @@
 #include <Stream.h>
 #include <FontInfo.h>
 #include <PDFDocEncoding.h>
+#include <DateInfo.h>
 
 #include "poppler.h"
 #include "poppler-private.h"
@@ -1508,7 +1509,6 @@ _poppler_convert_pdf_date_to_gtime (GooString *date,
 				    GTime     *gdate) 
 {
   int year, mon, day, hour, min, sec;
-  int scanned_items;
   struct tm time;
   gchar *date_string, *ds;
   GTime result;
@@ -1523,34 +1523,11 @@ _poppler_convert_pdf_date_to_gtime (GooString *date,
   ds = date_string;
   
   /* See PDF Reference 1.3, Section 3.8.2 for PDF Date representation */
-
-  if (date_string [0] == 'D' && date_string [1] == ':')
-    date_string += 2;
-	
-  /* FIXME only year is mandatory; parse optional timezone offset */
-  scanned_items = sscanf (date_string, "%4d%2d%2d%2d%2d%2d",
-			  &year, &mon, &day, &hour, &min, &sec);
-  
-  if (scanned_items != 6) {
+  if (!parseDateString(ds, &year, &mon, &day, &hour, &min, &sec)) {
     g_free (ds);
     return FALSE;
   }
-  
-  /* Workaround for y2k bug in Distiller 3, hoping that it won't
-   * be used after y2.2k */
-  if (year < 1930 && strlen (date_string) > 14) {
-    int century, years_since_1900;
-    scanned_items = sscanf (date_string, "%2d%3d%2d%2d%2d%2d%2d",
-			    &century, &years_since_1900, &mon, &day, &hour, &min, &sec);
-						
-    if (scanned_items != 7) {
-      g_free (ds);
-      return FALSE;
-    }
-    
-    year = century * 100 + years_since_1900;
-  }
-
+	
   time.tm_year = year - 1900;
   time.tm_mon = mon - 1;
   time.tm_mday = day;
diff --git a/poppler/DateInfo.cc b/poppler/DateInfo.cc
new file mode 100644
index 0000000..b9e1c0a
--- /dev/null
+++ b/poppler/DateInfo.cc
@@ -0,0 +1,62 @@
+//========================================================================
+//
+// DateInfo.cc
+//
+// Copyright (C) 2008 Albert Astals Cid <aacid at kde.org>
+//
+// 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
+//
+//========================================================================
+
+//========================================================================
+//
+// Based on code from pdfinfo.cc
+//
+// Copyright 1998-2003 Glyph & Cog, LLC
+//
+//========================================================================
+
+#include "DateInfo.h"
+
+#include <stdio.h>
+#include <string.h>
+
+/* See PDF Reference 1.3, Section 3.8.2 for PDF Date representation */
+/* FIXME only year is mandatory */
+/* FIXME parse optional timezone offset <- It seems Adobe Reader does not do it? */
+GBool parseDateString(const char *dateString, int *year, int *month, int *day, int *hour, int *minute, int *second)
+{
+    if ( dateString == NULL ) return gFalse;
+    if ( strlen(dateString) < 2 ) return gFalse;
+
+    if ( dateString[0] == 'D' && dateString[1] == ':' )
+        dateString += 2;
+    if ( sscanf( dateString,
+                 "%4d%2d%2d%2d%2d%2d",
+                 year, month, day, hour, minute, second ) == 6 )
+    {
+        /* Workaround for y2k bug in Distiller 3 stolen from gpdf, hoping that it won't
+        * be used after y2.2k */
+        if ( *year < 1930 && strlen (dateString) > 14)
+        {
+           int century, years_since_1900;
+           if ( sscanf( dateString,
+                        "%2d%3d%2d%2d%2d%2d%2d",
+                        &century, &years_since_1900, month, day, hour, minute, second) == 7 )
+           {
+               *year = century * 100 + years_since_1900;
+           }
+           else
+           {
+               return gFalse;
+           }
+       }
+
+       if (*year <= 0) return gFalse;
+
+       return gTrue;
+   }
+
+   return gFalse;
+}
diff --git a/poppler/DateInfo.h b/poppler/DateInfo.h
new file mode 100644
index 0000000..6b11923
--- /dev/null
+++ b/poppler/DateInfo.h
@@ -0,0 +1,27 @@
+//========================================================================
+//
+// DateInfo.h
+//
+// Copyright (C) 2008 Albert Astals Cid <aacid at kde.org>
+//
+// 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
+//
+//========================================================================
+
+//========================================================================
+//
+// Based on code from pdfinfo.cc
+//
+// Copyright 1998-2003 Glyph & Cog, LLC
+//
+//========================================================================
+
+#ifndef DATE_INFO_H
+#define DATE_INFO_H
+
+#include "goo/gtypes.h"
+
+GBool parseDateString(const char *string, int *year, int *month, int *day, int *hour, int *minute, int *second);
+
+#endif
diff --git a/poppler/Makefile.am b/poppler/Makefile.am
index a904e92..f19b31e 100644
--- a/poppler/Makefile.am
+++ b/poppler/Makefile.am
@@ -151,6 +151,7 @@ poppler_include_HEADERS =	\
 	Catalog.h		\
 	CharCodeToUnicode.h	\
 	CMap.h			\
+	DateInfo.h		\
 	Decrypt.h		\
 	Dict.h			\
 	Error.h			\
@@ -219,6 +220,7 @@ libpoppler_la_SOURCES =		\
 	Catalog.cc 		\
 	CharCodeToUnicode.cc	\
 	CMap.cc			\
+	DateInfo.cc		\
 	Decrypt.cc		\
 	Dict.cc 		\
 	Error.cc 		\
diff --git a/qt/poppler-document.cc b/qt/poppler-document.cc
index cf47e19..d87ebd4 100644
--- a/qt/poppler-document.cc
+++ b/qt/poppler-document.cc
@@ -29,6 +29,7 @@
 #include <ErrorCodes.h>
 #include <SplashOutputDev.h>
 #include <splash/SplashBitmap.h>
+#include <DateInfo.h>
 #include "poppler-private.h"
 
 namespace Poppler {
@@ -227,27 +228,8 @@ QDateTime Document::getDate( const QString & type ) const
   if ( infoDict->lookup( (char*)type.latin1(), &obj )->isString() )
   {
     QString s = UnicodeParsedString(obj.getString());
-    if ( s[0] == 'D' && s[1] == ':' )
-      s = s.mid(2);
-
-    /* FIXME process time zone on systems that support it */  
-    if ( !s.isEmpty() && sscanf( s.latin1(), "%4d%2d%2d%2d%2d%2d", &year, &mon, &day, &hour, &min, &sec ) == 6 )
+    if ( parseDateString( s.latin1(), &year, &mon, &day, &hour, &min, &sec ) )
     {
-      /* Workaround for y2k bug in Distiller 3 stolen from gpdf, hoping that it won't
-       *   * be used after y2.2k */
-      if ( year < 1930 && strlen (s) > 14) {
-	int century, years_since_1900;
-	if ( sscanf( s, "%2d%3d%2d%2d%2d%2d%2d",
-	    &century, &years_since_1900,
-	    &mon, &day, &hour, &min, &sec) == 7 )
-	  year = century * 100 + years_since_1900;
-	else {
-	  obj.free();
-	  info.free();
-	  return QDateTime();
-	}
-      }
-
       QDate d( year, mon, day );  //CHECK: it was mon-1, Jan->0 (??)
       QTime t( hour, min, sec );
       if ( d.isValid() && t.isValid() ) {
diff --git a/qt4/src/poppler-annotation-helper.h b/qt4/src/poppler-annotation-helper.h
index eccb2b5..f5d17eb 100644
--- a/qt4/src/poppler-annotation-helper.h
+++ b/qt4/src/poppler-annotation-helper.h
@@ -1,5 +1,5 @@
 /* poppler-annotation-helper.h: qt interface to poppler
- * Copyright (C) 2006, Albert Astals Cid <aacid at kde.org>
+ * Copyright (C) 2006, 2008, Albert Astals Cid <aacid at kde.org>
  * Copyright (C) 2008, Pino Toscano <pino at kde.org>
  * Adapting code from
  *   Copyright (C) 2004 by Enrico Ros <eros.kde at email.it>
@@ -162,19 +162,7 @@ void XPDFReader::lookupDate( Dict * dict, char * type, QDateTime & dest )
         return;
     if ( dateObj.isString() )
     {
-        char * s = dateObj.getString()->getCString();
-        if ( s[0] == 'D' && s[1] == ':' )
-            s += 2;
-        int year, mon, day, hour, min, sec;
-        if ( sscanf( s, "%4d%2d%2d%2d%2d%2d", &year, &mon, &day, &hour, &min, &sec ) == 6 )
-        {
-            QDate d( year, mon, day );
-            QTime t( hour, min, sec );
-            if ( d.isValid() && t.isValid() )
-                dest = QDateTime(d, t);
-        }
-        else
-            qDebug() << "Wrong Date format '" << s << "' for '" << type << "'." << endl;
+        dest = convertDate( dateObj.getString()->getCString() );
     }
     else
         qDebug() << type << " is not Date" << endl;
diff --git a/qt4/src/poppler-document.cc b/qt4/src/poppler-document.cc
index 13012f2..623d590 100644
--- a/qt4/src/poppler-document.cc
+++ b/qt4/src/poppler-document.cc
@@ -28,6 +28,7 @@
 #include <PDFDoc.h>
 #include <Stream.h>
 #include <Catalog.h>
+#include <DateInfo.h>
 
 #include <QtCore/QDebug>
 #include <QtCore/QFile>
@@ -520,57 +521,14 @@ namespace Poppler {
 
     QDateTime convertDate( char *dateString )
     {
-        int year;
-        int mon = 1;
-        int day = 1;
-        int hour = 0;
-        int min = 0;
-        int sec = 0;
-        char tz = 0x00;
-        int tzHours = 0;
-        int tzMins = 0;
-
-        if ( dateString == NULL ) return QDateTime();
-        if ( strlen(dateString) < 2 ) return QDateTime();
-
-        if ( dateString[0] == 'D' && dateString[1] == ':' )
-            dateString += 2;
-        if ( dateString != NULL && sscanf( dateString,
-		     "%4d%2d%2d%2d%2d%2d%c%2d%*c%2d",
-		     &year, &mon, &day, &hour, &min, &sec,
-		     &tz, &tzHours, &tzMins ) > 0 ) {
-            /* Workaround for y2k bug in Distiller 3 stolen from gpdf, hoping that it won't
-             * be used after y2.2k */
-            if ( year < 1930 && strlen (dateString) > 14) {
-                int century, years_since_1900;
-                if ( sscanf( dateString, "%2d%3d%2d%2d%2d%2d%2d",
-                             &century, &years_since_1900,
-                             &mon, &day, &hour, &min, &sec) == 7 )
-                    year = century * 100 + years_since_1900;
-                else {
-                    return QDateTime();
-                }
-            }
+        int year, mon, day, hour, min, sec;
 
+        if ( parseDateString( dateString, &year, &mon, &day, &hour, &min, &sec ) )
+        {
             QDate d( year, mon, day );
             QTime t( hour, min, sec );
             if ( d.isValid() && t.isValid() ) {
-                QDateTime dt( d, t, Qt::UTC );
-                if ( tz ) {
-                    // then we have some form of timezone
-                    if ( 'Z' == tz  ) {
-                        // We are already at UTC
-                    } else if ( '+' == tz ) {
-                        // local time is ahead of UTC
-                        dt = dt.addSecs(-1*((tzHours*60)+tzMins)*60);
-                    } else if ( '-' == tz ) {
-                        // local time is behind UTC
-                        dt = dt.addSecs(((tzHours*60)+tzMins)*60);
-                    } else {
-                        qWarning("unexpected tz val");
-                    }
-                }
-                return dt;
+                return QDateTime( d, t );
             }
         }
         return QDateTime();
diff --git a/utils/pdfinfo.cc b/utils/pdfinfo.cc
index a7f81d3..df54862 100644
--- a/utils/pdfinfo.cc
+++ b/utils/pdfinfo.cc
@@ -43,6 +43,7 @@
 #include "UnicodeMap.h"
 #include "PDFDocEncoding.h"
 #include "Error.h"
+#include "DateInfo.h"
 
 static void printInfoString(Dict *infoDict, char *key, char *text,
 			    UnicodeMap *uMap);
@@ -366,25 +367,14 @@ static void printInfoString(Dict *infoDict, char *key, char *text,
 static void printInfoDate(Dict *infoDict, char *key, char *text) {
   Object obj;
   char *s;
-  int year, mon, day, hour, min, sec, n;
+  int year, mon, day, hour, min, sec;
   struct tm tmStruct;
   char buf[256];
 
   if (infoDict->lookup(key, &obj)->isString()) {
     fputs(text, stdout);
     s = obj.getString()->getCString();
-    if (s[0] == 'D' && s[1] == ':') {
-      s += 2;
-    }
-    if ((n = sscanf(s, "%4d%2d%2d%2d%2d%2d",
-		    &year, &mon, &day, &hour, &min, &sec)) >= 1) {
-      switch (n) {
-      case 1: mon = 1;
-      case 2: day = 1;
-      case 3: hour = 0;
-      case 4: min = 0;
-      case 5: sec = 0;
-      }
+    if ( parseDateString( s, &year, &mon, &day, &hour, &min, &sec ) ) {
       tmStruct.tm_year = year - 1900;
       tmStruct.tm_mon = mon - 1;
       tmStruct.tm_mday = day;
diff --git a/utils/pdftohtml.cc b/utils/pdftohtml.cc
index ede4c81..8628441 100644
--- a/utils/pdftohtml.cc
+++ b/utils/pdftohtml.cc
@@ -42,6 +42,7 @@
 #include "PSOutputDev.h"
 #include "GlobalParams.h"
 #include "Error.h"
+#include "DateInfo.h"
 #include "goo/gfile.h"
 
 #ifndef GHOSTSCRIPT
@@ -410,11 +411,7 @@ static GooString* getInfoDate(Dict *infoDict, char *key) {
 
   if (infoDict->lookup(key, &obj)->isString()) {
     s = obj.getString()->getCString();
-    if (s[0] == 'D' && s[1] == ':') {
-      s += 2;
-    }
-    if (sscanf(s, "%4d%2d%2d%2d%2d%2d",
-               &year, &mon, &day, &hour, &min, &sec) == 6) {
+    if ( parseDateString( s, &year, &mon, &day, &hour, &min, &sec ) ) {
       tmStruct.tm_year = year - 1900;
       tmStruct.tm_mon = mon - 1;
       tmStruct.tm_mday = day;
@@ -426,7 +423,7 @@ static GooString* getInfoDate(Dict *infoDict, char *key) {
       tmStruct.tm_isdst = -1;
       mktime(&tmStruct); // compute the tm_wday and tm_yday fields
       if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S+00:00", &tmStruct)) {
-	result = new GooString(buf);
+        result = new GooString(buf);
       } else {
         result = new GooString(s);
       }


More information about the poppler mailing list