Commit 8fbc8579f2481dc3eeb962e99522047291e16fbe

Authored by Jay Berkenbilt
1 parent df067c9a

Allow zone information to be omitted from timestamp strings

include/qpdf/QUtil.hh
... ... @@ -203,8 +203,9 @@ namespace QUtil
203 203  
204 204 // Convert a QPDFTime structure to a PDF timestamp string, which
205 205 // is "D:yyyymmddhhmmss<z>" where <z> is either "Z" for UTC or
206   - // "-hh'mm'" or "+hh'mm'" for timezone offset. Examples:
207   - // "D:20210207161528-05'00'", "D:20210207211528Z". See
  206 + // "-hh'mm'" or "+hh'mm'" for timezone offset. <z> may also be
  207 + // omitted. Examples: "D:20210207161528-05'00'",
  208 + // "D:20210207211528Z", "D:20210207211528". See
208 209 // get_current_qpdf_time and the QPDFTime structure above.
209 210 QPDF_DLL
210 211 std::string qpdf_time_to_pdf_time(QPDFTime const&);
... ...
libqpdf/QUtil.cc
... ... @@ -898,7 +898,7 @@ QUtil::pdf_time_to_qpdf_time(std::string const&amp; str, QPDFTime* qtm)
898 898 {
899 899 static std::regex pdf_date("^D:([0-9]{4})([0-9]{2})([0-9]{2})"
900 900 "([0-9]{2})([0-9]{2})([0-9]{2})"
901   - "(?:(Z)|([\+\-])([0-9]{2})'([0-9]{2})')$");
  901 + "(?:(Z?)|([\+\-])([0-9]{2})'([0-9]{2})')$");
902 902 std::smatch m;
903 903 if (! std::regex_match(str, m, pdf_date))
904 904 {
... ... @@ -909,7 +909,7 @@ QUtil::pdf_time_to_qpdf_time(std::string const&amp; str, QPDFTime* qtm)
909 909 return QUtil::string_to_int(s.c_str());
910 910 };
911 911  
912   - if (m[7] == "")
  912 + if (m[8] != "")
913 913 {
914 914 tz_delta = ((to_i(m[9]) * 60) +
915 915 to_i(m[10]));
... ...
libtests/qutil.cc
... ... @@ -594,6 +594,10 @@ void timestamp_test()
594 594 check(QUtil::QPDFTime(2021, 2, 10, 1, 19, 25, -330));
595 595 check(QUtil::QPDFTime(2021, 2, 9, 19, 19, 25, 0));
596 596 assert(! QUtil::pdf_time_to_qpdf_time("potato"));
  597 + assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743Z"));
  598 + assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743-05'00'"));
  599 + assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743+05'30'"));
  600 + assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743"));
597 601 // Round trip on the current time without actually printing it.
598 602 // Manual testing was done to ensure that we are actually getting
599 603 // back the current time in various timezones.
... ...