Commit fbb6509c authored by Adrian Johnson's avatar Adrian Johnson Committed by Albert Astals Cid

Include timezone in timeToDateString()

parent 8da4d536
......@@ -6,7 +6,7 @@
// Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
// Copyright (C) 2015 André Guerreiro <aguerreiro1985@gmail.com>
// Copyright (C) 2015 André Esser <bepandre@hotmail.com>
// Copyright (C) 2016 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2016, 2018 Adrian Johnson <ajohnson@redneon.com>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
......@@ -76,42 +76,29 @@ bool parseDateString(const char *dateString, int *year, int *month, int *day, in
return false;
}
// Convert time to PDF date string
GooString *timeToDateString(time_t *timet) {
GooString *dateString;
char s[5];
struct tm *gt;
size_t len;
time_t timep = timet ? *timet : time(nullptr);
struct tm t;
gt = gmtime_r (&timep, &t);
dateString = new GooString ("D:");
/* Year YYYY */
len = strftime (s, sizeof(s), "%Y", gt);
dateString->append (s, len);
/* Month MM */
len = strftime (s, sizeof(s), "%m", gt);
dateString->append (s, len);
/* Day DD */
len = strftime (s, sizeof(s), "%d", gt);
dateString->append (s, len);
/* Hour HH */
len = strftime (s, sizeof(s), "%H", gt);
dateString->append (s, len);
/* Minute mm */
len = strftime (s, sizeof(s), "%M", gt);
dateString->append (s, len);
/* Second SS */
len = strftime (s, sizeof(s), "%S", gt);
dateString->append (s, len);
GooString *timeToDateString(time_t *timeA)
{
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);
// 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
// time.
const time_t timeg = timegm(&localtime_tm);
const time_t offset = difftime(timeg, timet); // find time zone offset in seconds
if (offset > 0) {
dateString->appendf("+{0:02d}'{1:02d}", offset/3600, (offset%3600)/60);
} else if (offset < 0) {
dateString->appendf("-{0:02d}'{1:02d}", -offset/3600, (-offset%3600)/60);
} else {
dateString->append("Z");
}
return dateString;
}
......
......@@ -31,6 +31,7 @@ bool parseDateString(const char *string, int *year, int *month, int *day, int *h
/* Converts the time_t into a PDF Date format string.
* If timet is NULL, current time is used.
* Returns new GooString. Free with delete.
*/
GooString *timeToDateString(time_t *timet);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment