cpp/poppler-document.cpp      |  136 +++++++++++++++++++++++++++++++++++++++++-
 cpp/poppler-document.h        |   20 ++++--
 cpp/poppler-embedded-file.cpp |   26 +++++++-
 cpp/poppler-embedded-file.h   |    8 +-
 cpp/poppler-global.cpp        |   11 +++
 cpp/poppler-global.h          |    6 +
 cpp/tests/poppler-dump.cpp    |    8 +-
 7 files changed, 195 insertions(+), 20 deletions(-)

New commits:
commit 2656d986d01da5aea4f51c75e4deee569ca88064
Author: Albert Astals Cid <aa...@kde.org>
Date:   Sat Apr 2 22:21:35 2022 +0200

    cpp: Use time_t for time
    
    The existing time_type is unsigned int which suffers from the Y2K38
    problem

diff --git a/cpp/poppler-document.cpp b/cpp/poppler-document.cpp
index 2ae13199..70cae1b6 100644
--- a/cpp/poppler-document.cpp
+++ b/cpp/poppler-document.cpp
@@ -327,7 +327,7 @@ bool document::set_info_key(const std::string &key, const 
ustring &val)
 }
 
 /**
- Gets the time_t value of the specified \p key of the document
+ Gets the time_type value of the specified \p key of the document
  information.
 
  \returns the time_t value for the \p key
@@ -347,6 +347,27 @@ time_type document::info_date(const std::string &key) const
     return static_cast<time_type>(dateStringToTime(goo_date.get()));
 }
 
+/**
+ Gets the time_t value of the specified \p key of the document
+ information.
+
+ \returns the time_t value for the \p key
+ \see info_keys, info_date
+ */
+time_t document::info_date_t(const std::string &key) const
+{
+    if (d->is_locked) {
+        return time_t(-1);
+    }
+
+    std::unique_ptr<GooString> 
goo_date(d->doc->getDocInfoStringEntry(key.c_str()));
+    if (!goo_date.get()) {
+        return time_t(-1);
+    }
+
+    return dateStringToTime(goo_date.get());
+}
+
 /**
  Sets the time_type value of the specified \p key of the %document information
  to \p val.
@@ -373,6 +394,31 @@ bool document::set_info_date(const std::string &key, 
time_type val)
     return true;
 }
 
+/**
+ Sets the time_t value of the specified \p key of the %document information
+ to \p val.
+ If \p val == time_t(-1), the entry specified by \p key is removed.
+
+ \returns true on success, false on failure
+ */
+bool document::set_info_date_t(const std::string &key, time_t val)
+{
+    if (d->is_locked) {
+        return false;
+    }
+
+    GooString *goo_date;
+
+    if (val == time_t(-1)) {
+        goo_date = nullptr;
+    } else {
+        goo_date = timeToDateString(&val);
+    }
+
+    d->doc->setDocInfoStringEntry(key.c_str(), goo_date);
+    return true;
+}
+
 /**
  Gets the %document's title.
 
@@ -657,6 +703,26 @@ time_type document::get_creation_date() const
     return static_cast<time_type>(dateStringToTime(goo_creation_date.get()));
 }
 
+/**
+ Gets the document's creation date as a time_t value.
+
+ \returns the document's creation date as a time_t value
+ \see set_creation_date, info_date
+ */
+time_t document::get_creation_date_t() const
+{
+    if (d->is_locked) {
+        return time_t(-1);
+    }
+
+    std::unique_ptr<GooString> 
goo_creation_date(d->doc->getDocInfoCreatDate());
+    if (!goo_creation_date.get()) {
+        return time_t(-1);
+    }
+
+    return dateStringToTime(goo_creation_date.get());
+}
+
 /**
  Sets the %document's creation date to \p creation_date.
  If \p creation_date == time_type(-1), the %document's creation date is 
removed.
@@ -682,6 +748,30 @@ bool document::set_creation_date(time_type creation_date)
     return true;
 }
 
+/**
+ Sets the %document's creation date to \p creation_date.
+ If \p creation_date == time_t(-1), the %document's creation date is removed.
+
+ \returns true on success, false on failure
+ */
+bool document::set_creation_date_t(time_t creation_date)
+{
+    if (d->is_locked) {
+        return false;
+    }
+
+    GooString *goo_creation_date;
+
+    if (creation_date == time_t(-1)) {
+        goo_creation_date = nullptr;
+    } else {
+        goo_creation_date = timeToDateString(&creation_date);
+    }
+
+    d->doc->setDocInfoCreatDate(goo_creation_date);
+    return true;
+}
+
 /**
  Gets the document's modification date as a time_type value.
 
@@ -702,6 +792,26 @@ time_type document::get_modification_date() const
     return 
static_cast<time_type>(dateStringToTime(goo_modification_date.get()));
 }
 
+/**
+ Gets the document's modification date as a time_t value.
+
+ \returns the document's modification date as a time_t value
+ \see set_modification_date, info_date
+ */
+time_t document::get_modification_date_t() const
+{
+    if (d->is_locked) {
+        return time_t(-1);
+    }
+
+    std::unique_ptr<GooString> 
goo_modification_date(d->doc->getDocInfoModDate());
+    if (!goo_modification_date.get()) {
+        return time_t(-1);
+    }
+
+    return dateStringToTime(goo_modification_date.get());
+}
+
 /**
  Sets the %document's modification date to \p mod_date.
  If \p mod_date == time_type(-1), the %document's modification date is removed.
@@ -727,6 +837,30 @@ bool document::set_modification_date(time_type mod_date)
     return true;
 }
 
+/**
+ Sets the %document's modification date to \p mod_date.
+ If \p mod_date == time_t(-1), the %document's modification date is removed.
+
+ \returns true on success, false on failure
+ */
+bool document::set_modification_date_t(time_t mod_date)
+{
+    if (d->is_locked) {
+        return false;
+    }
+
+    GooString *goo_mod_date;
+
+    if (mod_date == time_t(-1)) {
+        goo_mod_date = nullptr;
+    } else {
+        goo_mod_date = timeToDateString(&mod_date);
+    }
+
+    d->doc->setDocInfoModDate(goo_mod_date);
+    return true;
+}
+
 /**
  Removes the %document's Info dictionary.
 
diff --git a/cpp/poppler-document.h b/cpp/poppler-document.h
index ccf0a0f4..97fd9a87 100644
--- a/cpp/poppler-document.h
+++ b/cpp/poppler-document.h
@@ -2,7 +2,7 @@
  * Copyright (C) 2009-2010, Pino Toscano <p...@kde.org>
  * Copyright (C) 2016 Jakub Alba <jakuba...@gmail.com>
  * Copyright (C) 2019, Masamichi Hosoda <truer...@trueroad.jp>
- * Copyright (C) 2019, 2021, Albert Astals Cid <aa...@kde.org>
+ * Copyright (C) 2019, 2021, 2022, Albert Astals Cid <aa...@kde.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
@@ -72,8 +72,10 @@ public:
     ustring info_key(const std::string &key) const;
     bool set_info_key(const std::string &key, const ustring &val);
 
-    time_type info_date(const std::string &key) const;
-    bool set_info_date(const std::string &key, time_type val);
+    [[deprecated]] time_type info_date(const std::string &key) const;
+    [[deprecated]] bool set_info_date(const std::string &key, time_type val);
+    time_t info_date_t(const std::string &key) const;
+    bool set_info_date_t(const std::string &key, time_t val);
 
     ustring get_title() const;
     bool set_title(const ustring &title);
@@ -87,10 +89,14 @@ public:
     bool set_creator(const ustring &creator);
     ustring get_producer() const;
     bool set_producer(const ustring &producer);
-    time_type get_creation_date() const;
-    bool set_creation_date(time_type creation_date);
-    time_type get_modification_date() const;
-    bool set_modification_date(time_type mod_date);
+    [[deprecated]] time_type get_creation_date() const;
+    [[deprecated]] bool set_creation_date(time_type creation_date);
+    time_t get_creation_date_t() const;
+    bool set_creation_date_t(time_t creation_date);
+    [[deprecated]] time_type get_modification_date() const;
+    [[deprecated]] bool set_modification_date(time_type mod_date);
+    time_t get_modification_date_t() const;
+    bool set_modification_date_t(time_t mod_date);
 
     bool remove_info();
 
diff --git a/cpp/poppler-embedded-file.cpp b/cpp/poppler-embedded-file.cpp
index 7255bbef..d0b240a0 100644
--- a/cpp/poppler-embedded-file.cpp
+++ b/cpp/poppler-embedded-file.cpp
@@ -96,7 +96,7 @@ int embedded_file::size() const
 }
 
 /**
- \returns the time_t representing the modification date of the embedded file,
+ \returns the time_type representing the modification date of the embedded 
file,
           if available
  */
 time_type embedded_file::modification_date() const
@@ -107,7 +107,7 @@ time_type embedded_file::modification_date() const
 }
 
 /**
- \returns the time_t representing the creation date of the embedded file,
+ \returns the time_type representing the creation date of the embedded file,
           if available
  */
 time_type embedded_file::creation_date() const
@@ -117,6 +117,28 @@ time_type embedded_file::creation_date() const
     return goo ? static_cast<time_type>(dateStringToTime(goo)) : time_type(-1);
 }
 
+/**
+ \returns the time_t representing the modification date of the embedded file,
+          if available
+ */
+time_t embedded_file::modification_date_t() const
+{
+    const EmbFile *ef = d->file_spec->getEmbeddedFile();
+    const GooString *goo = ef ? ef->modDate() : nullptr;
+    return goo ? dateStringToTime(goo) : time_t(-1);
+}
+
+/**
+ \returns the time_t representing the creation date of the embedded file,
+          if available
+ */
+time_t embedded_file::creation_date_t() const
+{
+    const EmbFile *ef = d->file_spec->getEmbeddedFile();
+    const GooString *goo = ef ? ef->createDate() : nullptr;
+    return goo ? dateStringToTime(goo) : time_t(-1);
+}
+
 /**
  \returns the checksum of the embedded file
  */
diff --git a/cpp/poppler-embedded-file.h b/cpp/poppler-embedded-file.h
index d108afb1..f39bd745 100644
--- a/cpp/poppler-embedded-file.h
+++ b/cpp/poppler-embedded-file.h
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2009-2010, Pino Toscano <p...@kde.org>
- * Copyright (C) 2021, Albert Astals Cid <aa...@kde.org>
+ * Copyright (C) 2021, 2022, Albert Astals Cid <aa...@kde.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
@@ -37,8 +37,10 @@ public:
     std::string name() const;
     ustring description() const;
     int size() const;
-    time_type modification_date() const;
-    time_type creation_date() const;
+    [[deprecated]] time_type modification_date() const;
+    [[deprecated]] time_type creation_date() const;
+    time_t modification_date_t() const;
+    time_t creation_date_t() const;
     byte_array checksum() const;
     std::string mime_type() const;
     byte_array data() const;
diff --git a/cpp/poppler-global.cpp b/cpp/poppler-global.cpp
index 75938321..08765569 100644
--- a/cpp/poppler-global.cpp
+++ b/cpp/poppler-global.cpp
@@ -317,7 +317,7 @@ ustring ustring::from_latin1(const std::string &str)
 }
 
 /**
- Converts a string representing a PDF date to a value compatible with time_t.
+ Converts a string representing a PDF date to a value compatible with 
time_type.
  */
 time_type poppler::convert_date(const std::string &date)
 {
@@ -325,6 +325,15 @@ time_type poppler::convert_date(const std::string &date)
     return static_cast<time_type>(dateStringToTime(&gooDateStr));
 }
 
+/**
+ Converts a string representing a PDF date to a value compatible with time_t.
+ */
+time_t poppler::convert_date_t(const std::string &date)
+{
+    GooString gooDateStr(date.c_str());
+    return dateStringToTime(&gooDateStr);
+}
+
 std::ostream &poppler::operator<<(std::ostream &stream, const byte_array 
&array)
 {
     stream << "[";
diff --git a/cpp/poppler-global.h b/cpp/poppler-global.h
index 49fc7104..2a63e88c 100644
--- a/cpp/poppler-global.h
+++ b/cpp/poppler-global.h
@@ -3,7 +3,7 @@
  * Copyright (C) 2010, Patrick Spendrin <ps...@gmx.de>
  * Copyright (C) 2014, Hans-Peter Deifel <hpdei...@gmx.de>
  * Copyright (C) 2018, Adam Reichold <adam.reich...@t-online.de>
- * Copyright (C) 2021, Albert Astals Cid <aa...@kde.org>
+ * Copyright (C) 2021, 2022, Albert Astals Cid <aa...@kde.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
@@ -117,7 +117,9 @@ private:
 #    pragma warning(pop)
 #endif
 
-POPPLER_CPP_EXPORT time_type convert_date(const std::string &date);
+[[deprecated]] POPPLER_CPP_EXPORT time_type convert_date(const std::string 
&date);
+
+POPPLER_CPP_EXPORT time_t convert_date_t(const std::string &date);
 
 POPPLER_CPP_EXPORT std::ostream &operator<<(std::ostream &stream, const 
byte_array &array);
 
diff --git a/cpp/tests/poppler-dump.cpp b/cpp/tests/poppler-dump.cpp
index cc343ff2..c10b8f41 100644
--- a/cpp/tests/poppler-dump.cpp
+++ b/cpp/tests/poppler-dump.cpp
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2009-2010, Pino Toscano <p...@kde.org>
- * Copyright (C) 2017-2019, Albert Astals Cid <aa...@kde.org>
+ * Copyright (C) 2017-2019, 2022, Albert Astals Cid <aa...@kde.org>
  * Copyright (C) 2017, Jason Alan Palmer <jalanpal...@gmail.com>
  * Copyright (C) 2018, 2020, Suzuki Toshiya <mpsuz...@hiroshima-u.ac.jp>
  * Copyright (C) 2019, Masamichi Hosoda <truer...@trueroad.jp>
@@ -194,9 +194,9 @@ static void print_info(poppler::document *doc)
         std::cout << std::setw(out_width) << *key_it << ": " << 
doc->info_key(*key_it) << std::endl;
     }
     std::cout << std::setw(out_width) << "Date (creation)"
-              << ": " << out_date(doc->info_date("CreationDate")) << std::endl;
+              << ": " << out_date(doc->info_date_t("CreationDate")) << 
std::endl;
     std::cout << std::setw(out_width) << "Date (modification)"
-              << ": " << out_date(doc->info_date("ModDate")) << std::endl;
+              << ": " << out_date(doc->info_date_t("ModDate")) << std::endl;
     std::cout << std::setw(out_width) << "Number of pages"
               << ": " << doc->pages() << std::endl;
     std::cout << std::setw(out_width) << "Linearized"
@@ -280,7 +280,7 @@ static void print_embedded_files(poppler::document *doc)
         std::left(std::cout);
         for (; it != it_end; ++it) {
             poppler::embedded_file *f = *it;
-            std::cout << " " << std::setw(out_width + 10) << f->name() << " " 
<< std::setw(10) << out_size(f->size()) << " " << std::setw(20) << 
out_date(f->creation_date()) << " " << std::setw(20) << 
out_date(f->modification_date())
+            std::cout << " " << std::setw(out_width + 10) << f->name() << " " 
<< std::setw(10) << out_size(f->size()) << " " << std::setw(20) << 
out_date(f->creation_date_t()) << " " << std::setw(20) << 
out_date(f->modification_date_t())
                       << std::endl
                       << "     ";
             if (f->description().empty()) {

Reply via email to