diff options
author | Erich E. Hoover <erich.e.hoover@gmail.com> | 2022-08-15 10:04:17 -0600 |
---|---|---|
committer | Albert Astals Cid <aacid@kde.org> | 2024-02-18 17:45:13 +0000 |
commit | 53b98e8d3c7783b4340e6006e7641fa6b0ffe3ec (patch) | |
tree | c3fa56d7f3553f2b883322b5afb0b9bec5c42f06 | |
parent | 7b189dc13c1e8de7df23326ce5b2ff93b76dc712 (diff) |
DateInfo: Add timeToStringWithFormat for using a custom format string
-rw-r--r-- | poppler/DateInfo.cc | 33 | ||||
-rw-r--r-- | poppler/DateInfo.h | 6 |
2 files changed, 31 insertions, 8 deletions
diff --git a/poppler/DateInfo.cc b/poppler/DateInfo.cc index bcaff88b..afcd876b 100644 --- a/poppler/DateInfo.cc +++ b/poppler/DateInfo.cc @@ -86,16 +86,14 @@ bool parseDateString(const GooString *date, int *year, int *month, int *day, int return false; } -GooString *timeToDateString(const time_t *timeA) +std::string timeToStringWithFormat(const time_t *timeA, const char *format) { const time_t timet = timeA ? *timeA : time(nullptr); struct tm localtime_tm; localtime_r(&timet, &localtime_tm); - char buf[50]; - strftime(buf, sizeof(buf), "D:%Y%m%d%H%M%S", &localtime_tm); - GooString *dateString = new GooString(buf); + char timeOffset[12]; // strftime "%z" does not work on windows (it prints zone name, not offset) // calculate time zone offset by comparing local and gmtime time_t value for same @@ -103,14 +101,33 @@ GooString *timeToDateString(const time_t *timeA) const time_t timeg = timegm(&localtime_tm); const int offset = static_cast<int>(difftime(timeg, timet)); // find time zone offset in seconds if (offset > 0) { - dateString->appendf("+{0:02d}'{1:02d}'", offset / 3600, (offset % 3600) / 60); + snprintf(timeOffset, sizeof(timeOffset), "+%02d'%02d'", offset / 3600, (offset % 3600) / 60); } else if (offset < 0) { - dateString->appendf("-{0:02d}'{1:02d}'", -offset / 3600, (-offset % 3600) / 60); + snprintf(timeOffset, sizeof(timeOffset), "-%02d'%02d'", -offset / 3600, (-offset % 3600) / 60); } else { - dateString->append("Z"); + snprintf(timeOffset, sizeof(timeOffset), "Z"); + } + std::string fmt(format); + const char timeOffsetPattern[] = "%z"; + size_t timeOffsetPosition = fmt.find(timeOffsetPattern); + if (timeOffsetPosition != std::string::npos) { + fmt.replace(timeOffsetPosition, sizeof(timeOffsetPattern) - 1, timeOffset); + } + + if (fmt.length() == 0) { + return ""; } + size_t bufLen = 50; + std::string buf(bufLen, ' '); + while (strftime(&buf[0], buf.size(), fmt.c_str(), &localtime_tm) == 0) { + buf.resize(bufLen *= 2); + } + return buf; +} - return dateString; +GooString *timeToDateString(const time_t *timeA) +{ + return new GooString(timeToStringWithFormat(timeA, "D:%Y%m%d%H%M%S%z")); } // Convert PDF date string to time. Returns -1 if conversion fails. diff --git a/poppler/DateInfo.h b/poppler/DateInfo.h index be349e61..d1802c8c 100644 --- a/poppler/DateInfo.h +++ b/poppler/DateInfo.h @@ -36,6 +36,12 @@ bool POPPLER_PRIVATE_EXPORT parseDateString(const GooString *date, int *year, in */ GooString POPPLER_PRIVATE_EXPORT *timeToDateString(const time_t *timeA); +/* Converts the time_t into a string with the specified format. + * If timeA is NULL, current time is used. + * Returns std::string + */ +std::string POPPLER_PRIVATE_EXPORT timeToStringWithFormat(const time_t *timeA, const char *format); + /* Convert PDF date string to time. * Returns -1 if conversion fails. */ |