Bug 1707 related changes - convert timestamps to use ISO date/time format.

(upstream r8170)
This commit is contained in:
dgotwisner 2014-04-18 10:55:44 +02:00 committed by Michael Iedema
parent df638854db
commit d45cfa5ab5
6 changed files with 41 additions and 8 deletions

View File

@ -32,6 +32,7 @@
#include <stdarg.h>
#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";
}

View File

@ -25,6 +25,7 @@
#include <cstdio>
#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

View File

@ -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&);

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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