From 8fbc8579f2481dc3eeb962e99522047291e16fbe Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 11 Feb 2021 06:51:21 -0500 Subject: Allow zone information to be omitted from timestamp strings --- include/qpdf/QUtil.hh | 5 +++-- libqpdf/QUtil.cc | 4 ++-- libtests/qutil.cc | 4 ++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh index 09a6c181..5b1f8aae 100644 --- a/include/qpdf/QUtil.hh +++ b/include/qpdf/QUtil.hh @@ -203,8 +203,9 @@ namespace QUtil // Convert a QPDFTime structure to a PDF timestamp string, which // is "D:yyyymmddhhmmss" where is either "Z" for UTC or - // "-hh'mm'" or "+hh'mm'" for timezone offset. Examples: - // "D:20210207161528-05'00'", "D:20210207211528Z". See + // "-hh'mm'" or "+hh'mm'" for timezone offset. may also be + // omitted. Examples: "D:20210207161528-05'00'", + // "D:20210207211528Z", "D:20210207211528". See // get_current_qpdf_time and the QPDFTime structure above. QPDF_DLL std::string qpdf_time_to_pdf_time(QPDFTime const&); diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 39d21ea0..4b86c0d4 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -898,7 +898,7 @@ QUtil::pdf_time_to_qpdf_time(std::string const& str, QPDFTime* qtm) { static std::regex pdf_date("^D:([0-9]{4})([0-9]{2})([0-9]{2})" "([0-9]{2})([0-9]{2})([0-9]{2})" - "(?:(Z)|([\\+\\-])([0-9]{2})'([0-9]{2})')$"); + "(?:(Z?)|([\\+\\-])([0-9]{2})'([0-9]{2})')$"); std::smatch m; if (! std::regex_match(str, m, pdf_date)) { @@ -909,7 +909,7 @@ QUtil::pdf_time_to_qpdf_time(std::string const& str, QPDFTime* qtm) return QUtil::string_to_int(s.c_str()); }; - if (m[7] == "") + if (m[8] != "") { tz_delta = ((to_i(m[9]) * 60) + to_i(m[10])); diff --git a/libtests/qutil.cc b/libtests/qutil.cc index b67b7580..965c4352 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -594,6 +594,10 @@ void timestamp_test() check(QUtil::QPDFTime(2021, 2, 10, 1, 19, 25, -330)); check(QUtil::QPDFTime(2021, 2, 9, 19, 19, 25, 0)); assert(! QUtil::pdf_time_to_qpdf_time("potato")); + assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743Z")); + assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743-05'00'")); + assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743+05'30'")); + assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743")); // Round trip on the current time without actually printing it. // Manual testing was done to ensure that we are actually getting // back the current time in various timezones. -- cgit v1.2.3-54-g00ecf