From d45cfa5ab5bbe8f1483c7bacb4655f11d55f036f Mon Sep 17 00:00:00 2001 From: dgotwisner Date: Fri, 18 Apr 2014 10:55:44 +0200 Subject: [PATCH] Bug 1707 related changes - convert timestamps to use ISO date/time format. (upstream r8170) --- Logger.cpp | 15 +++++++++------ Timeval.cpp | 16 ++++++++++++++++ Timeval.h | 6 ++++++ TimevalTest.cpp | 7 +++++++ URLEncodeTest.cpp | 2 +- Utils.cpp | 3 ++- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Logger.cpp b/Logger.cpp index 03a1059..6dabfff 100644 --- a/Logger.cpp +++ b/Logger.cpp @@ -32,6 +32,7 @@ #include #include "Configuration.h" +#include "Timeval.h" #include "Logger.h" #include "Threads.h" // pat added @@ -266,9 +267,10 @@ void gLogInitWithFile(const char* name, const char* level, int facility, char * if (gLogToFile==0 && LogFilePath != 0 && *LogFilePath != 0 && strlen(LogFilePath) > 0) { gLogToFile = fopen(LogFilePath,"w"); // New log file each time we start. if (gLogToFile) { - time_t now; - time(&now); - fprintf(gLogToFile,"Starting at %s",ctime(&now)); + time_t now = time(NULL); + std::string result; + Timeval::isoTime(now, result); + fprintf(gLogToFile,"Starting at %s",result.c_str()); fflush(gLogToFile); std::cout << name <<" logging to file: " << LogFilePath << "\n"; } @@ -299,9 +301,10 @@ void gLogInit(const char* name, const char* level, int facility) if (fn && *fn && strlen(fn)>3) { // strlen because a garbage char is getting in sometimes. gLogToFile = fopen(fn,"w"); // New log file each time we start. if (gLogToFile) { - time_t now; - time(&now); - fprintf(gLogToFile,"Starting at %s",ctime(&now)); + time_t now = time(NULL); + std::string result; + Timeval::isoTime(now, result); + fprintf(gLogToFile,"Starting at %s",result.c_str()); fflush(gLogToFile); std::cout << name <<" logging to file: " << fn << "\n"; } diff --git a/Timeval.cpp b/Timeval.cpp index 642c05a..0c679e6 100644 --- a/Timeval.cpp +++ b/Timeval.cpp @@ -25,6 +25,7 @@ +#include #include "Timeval.h" using namespace std; @@ -93,6 +94,21 @@ ostream& operator<<(ostream& os, const struct timespec& ts) return os; } +void Timeval::isoTime(time_t t, std::string &result, bool isLocal) +{ + char buf[BUFSIZ]; + struct tm tBuf; + if (isLocal) + localtime_r(&t, &tBuf); + else + gmtime_r(&t, &tBuf); + snprintf(buf, sizeof(buf)-1, "%04d-%02d-%02dT%02d:%02d:%02d%s", + tBuf.tm_year + 1900, tBuf.tm_mon + 1, tBuf.tm_mday, + tBuf.tm_hour, tBuf.tm_min, tBuf.tm_sec, + isLocal == false ? "Z" : ""); + result = buf; +} + // vim: ts=4 sw=4 diff --git a/Timeval.h b/Timeval.h index c497864..950e1ab 100644 --- a/Timeval.h +++ b/Timeval.h @@ -94,6 +94,12 @@ class Timeval { /** Add a given number of minutes to the time. */ void addMinutes(unsigned minutes) { mTimeval.tv_sec += minutes*60; } + /** Convert a time_t into a formatted string, using the ISO + * YYYY-MM-DDTHH:MM:SS[Z] format. If isLocal is true, use localtime, + * otherwise, use gmtime. + */ + static void isoTime(time_t t, std::string &result, bool isLocal = false); + }; std::ostream& operator<<(std::ostream& os, const Timeval&); diff --git a/TimevalTest.cpp b/TimevalTest.cpp index b4746f2..576e244 100644 --- a/TimevalTest.cpp +++ b/TimevalTest.cpp @@ -42,4 +42,11 @@ int main(int argc, char *argv[]) usleep(500000); } cout << "now: " << Timeval() << " then: " << then << " remaining: " << then.remaining() << endl; + + time_t t = time(NULL); + std::string sLocal(""); + std::string sGMT(""); + Timeval::isoTime(t, sLocal, true); + Timeval::isoTime(t, sGMT); + cout << "Localtime: " << sLocal << ", GMT: " << sGMT << std::endl; } diff --git a/URLEncodeTest.cpp b/URLEncodeTest.cpp index dbc4630..fa20744 100644 --- a/URLEncodeTest.cpp +++ b/URLEncodeTest.cpp @@ -10,7 +10,7 @@ using namespace std; int main(int argc, char *argv[]) { - string test = string("Testing: !@#$%^&*() " __DATE__ " " __TIME__); + string test = string("Testing: !@#$%^&*() " TIMESTAMP_ISO); cout << test << endl; cout << URLEncode(test) << endl; } diff --git a/Utils.cpp b/Utils.cpp index 78d3500..0a85d19 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -275,7 +275,8 @@ const string timestr(unsigned fieldwidth, bool addDate) // Use to pick the numbe unsigned tenths = tv.tv_usec / 100000; // Rounding down is ok. string result; if (addDate) - result = format(" %04d/%02d/%02d %02d:%02d:%02d.%1d", + // ISO time but with a fractional seconds number + result = format(" %04d-%02d-%02dT%02d:%02d:%02d.%1d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tenths); else