diff options
author | Carlos Garcia Campos <carlosgc@gnome.org> | 2009-04-21 19:29:32 +0200 |
---|---|---|
committer | Carlos Garcia Campos <carlosgc@gnome.org> | 2009-04-21 19:29:32 +0200 |
commit | 39d09fa237d06fa93b02eb916d2c0242c4e8fe85 (patch) | |
tree | 927058e3e688e230913cc4dabbb18a8ef5a0d9eb /glib | |
parent | 9c2714a3e1c02f445661618e24bcd27f1392b2b7 (diff) |
[glib] Add poppler_date_parse to parse PDF format date strings
We need to make this public because the field M in the Annot dictionary
might be a Date string (in PDF date format) or a text string. According
to the PDF spec: "The preferred format is a date string as described in
Section 3.8.3, “Dates,” but viewer applications should be prepared to
accept and display a string in any format". The only way to know whether
it's a PDF date string or not, is by trying to parse it. For this reason
poppler_annot_get_modified() returns a gchar * instead of a time_t. So,
viewers should try to parse the string in order to convert it to a
time_t, and if it fails to parse, use the date string as provided by the
document.
Diffstat (limited to 'glib')
-rw-r--r-- | glib/Makefile.am | 2 | ||||
-rw-r--r-- | glib/poppler-attachment.cc | 4 | ||||
-rw-r--r-- | glib/poppler-date.cc | 66 | ||||
-rw-r--r-- | glib/poppler-date.h | 30 | ||||
-rw-r--r-- | glib/poppler-document.cc | 45 | ||||
-rw-r--r-- | glib/poppler-private.h | 2 | ||||
-rw-r--r-- | glib/poppler.h | 1 |
7 files changed, 110 insertions, 40 deletions
diff --git a/glib/Makefile.am b/glib/Makefile.am index fe75c671..1548ce14 100644 --- a/glib/Makefile.am +++ b/glib/Makefile.am @@ -45,6 +45,7 @@ EXTRA_DIST = \ poppler_glib_public_headers = \ poppler-action.h \ + poppler-date.h \ poppler-document.h \ poppler-page.h \ poppler-attachment.h \ @@ -64,6 +65,7 @@ lib_LTLIBRARIES = libpoppler-glib.la libpoppler_glib_la_SOURCES = \ $(BUILT_SOURCES) \ poppler-action.cc \ + poppler-date.cc \ poppler-document.cc \ poppler-page.cc \ poppler-attachment.cc \ diff --git a/glib/poppler-attachment.cc b/glib/poppler-attachment.cc index 1c3eef00..2e2d12da 100644 --- a/glib/poppler-attachment.cc +++ b/glib/poppler-attachment.cc @@ -94,8 +94,8 @@ _poppler_attachment_new (PopplerDocument *document, attachment->size = emb_file->size (); - _poppler_convert_pdf_date_to_gtime (emb_file->createDate (), &attachment->ctime); - _poppler_convert_pdf_date_to_gtime (emb_file->modDate (), &attachment->mtime); + _poppler_convert_pdf_date_to_gtime (emb_file->createDate (), (time_t *)&attachment->ctime); + _poppler_convert_pdf_date_to_gtime (emb_file->modDate (), (time_t *)&attachment->mtime); attachment->checksum = g_string_new_len (emb_file->checksum ()->getCString (), emb_file->checksum ()->getLength ()); diff --git a/glib/poppler-date.cc b/glib/poppler-date.cc new file mode 100644 index 00000000..f29a8192 --- /dev/null +++ b/glib/poppler-date.cc @@ -0,0 +1,66 @@ +/* poppler-date.cc: glib interface to poppler + * + * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <DateInfo.h> + +#include "poppler-date.h" + +/** + * poppler_date_parse: + * @date: string to parse + * @timet: an uninitialized #time_t + * + * Parses a PDF format date string and converts it to a #time_t. Returns #FALSE + * if the parsing file or the input string is not a valid PDF format date string + * + * Return value: #TRUE, if @timet was set + **/ +gboolean +poppler_date_parse (const gchar *date, + time_t *timet) +{ + gint year, mon, day, hour, min, sec, tz_hour, tz_minute; + gchar tz; + struct tm time; + time_t retval; + + /* See PDF Reference 1.3, Section 3.8.2 for PDF Date representation */ + // TODO do something with the timezone information + if (!parseDateString (date, &year, &mon, &day, &hour, &min, &sec, &tz, &tz_hour, &tz_minute)) + return FALSE; + + time.tm_year = year - 1900; + time.tm_mon = mon - 1; + time.tm_mday = day; + time.tm_hour = hour; + time.tm_min = min; + time.tm_sec = sec; + time.tm_wday = -1; + time.tm_yday = -1; + 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); + if (retval == (time_t) - 1) + return FALSE; + + *timet = retval; + + return TRUE; +} diff --git a/glib/poppler-date.h b/glib/poppler-date.h new file mode 100644 index 00000000..2bb52b27 --- /dev/null +++ b/glib/poppler-date.h @@ -0,0 +1,30 @@ +/* poppler-date.h: glib interface to poppler + * + * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __POPPLER_DATE_H__ +#define __POPPLER_DATE_H__ + +#include "poppler.h" + +G_BEGIN_DECLS +gboolean poppler_date_parse (const gchar *date, + time_t *timet); +G_END_DECLS + +#endif /* __POPPLER_DATE_H__ */ diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index 3387becb..17eec51c 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -30,7 +30,6 @@ #include <Stream.h> #include <FontInfo.h> #include <PDFDocEncoding.h> -#include <DateInfo.h> #include <OptionalContent.h> #include "poppler.h" @@ -567,7 +566,7 @@ static void info_dict_get_date (Dict *info_dict, const gchar *key, GValue *value) { Object obj; - GTime result; + time_t result; if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) { obj.free (); @@ -1895,13 +1894,10 @@ poppler_document_get_form_field (PopplerDocument *document, gboolean _poppler_convert_pdf_date_to_gtime (GooString *date, - GTime *gdate) + time_t *gdate) { - int year, mon, day, hour, min, sec, tz_hour, tz_minute; - char tz; - struct tm time; - gchar *date_string, *ds; - GTime result; + gchar *date_string; + gboolean retval; if (date->hasUnicodeMarker()) { date_string = g_convert (date->getCString () + 2, @@ -1910,34 +1906,9 @@ _poppler_convert_pdf_date_to_gtime (GooString *date, } else { date_string = g_strndup (date->getCString (), date->getLength ()); } - ds = date_string; - - /* See PDF Reference 1.3, Section 3.8.2 for PDF Date representation */ - // TODO do something with the timezone information - if (!parseDateString(ds, &year, &mon, &day, &hour, &min, &sec, &tz, &tz_hour, &tz_minute)) { - g_free (ds); - return FALSE; - } - - time.tm_year = year - 1900; - time.tm_mon = mon - 1; - time.tm_mday = day; - time.tm_hour = hour; - time.tm_min = min; - time.tm_sec = sec; - time.tm_wday = -1; - time.tm_yday = -1; - time.tm_isdst = -1; /* 0 = DST off, 1 = DST on, -1 = don't know */ - - /* compute tm_wday and tm_yday and check date */ - result = mktime (&time); - if (result == (time_t) - 1) { - g_free (ds); - return FALSE; - } - - g_free (ds); - *gdate = result; - return TRUE; + retval = poppler_date_parse (date_string, gdate); + g_free (date_string); + + return retval; } diff --git a/glib/poppler-private.h b/glib/poppler-private.h index 1cb1eac7..e8ace142 100644 --- a/glib/poppler-private.h +++ b/glib/poppler-private.h @@ -111,7 +111,7 @@ PopplerAnnot *_poppler_annot_free_text_new (Annot *annot); char *_poppler_goo_string_to_utf8(GooString *s); gboolean _poppler_convert_pdf_date_to_gtime (GooString *date, - GTime *gdate); + time_t *gdate); /* * A convenience macro for boxed type implementations, which defines a diff --git a/glib/poppler.h b/glib/poppler.h index d554093b..b1a77307 100644 --- a/glib/poppler.h +++ b/glib/poppler.h @@ -125,5 +125,6 @@ G_END_DECLS #include "poppler-enums.h" #include "poppler-attachment.h" #include "poppler-annot.h" +#include "poppler-date.h" #endif /* __POPPLER_GLIB_H__ */ |