Browse Source

Bug Fix: Updated Timestamp() to use C++ stringstream and to handle possible null pointer from gmtime()

git-svn-id: https://svn.microneil.com/svn/SNFMulti/trunk@48 dc71a809-1921-45c4-985c-09c81d0142d9
wx
madscientist 11 years ago
parent
commit
f17433ad6c
1 changed files with 20 additions and 12 deletions
  1. 20
    12
      snfLOGmgr.cpp

+ 20
- 12
snfLOGmgr.cpp View File

@@ -8,7 +8,9 @@
#include "snfLOGmgr.hpp"
#include "../CodeDweller/threading.hpp"
#include "../CodeDweller/timing.hpp"
#include <unistd.h>
#include <unistd.h>
#include <sstream>
#include <iomanip>

using namespace std;

@@ -1734,18 +1736,24 @@ time_t snfLOGmgr::Timestamp() {
}

string snfLOGmgr::Timestamp(time_t t) { // Convert time_t to a timestamp s.
char TimestampBfr[20]; // Create a small buffer.
stringstream TimestampBfr;
const string EmptyTimestamp = "00000000000000";
string theTimeStamp = EmptyTimestamp;
tm* gmt; // Get a ptr to a tm structure.
gmt = gmtime(&t); // Fill it with UTC.
sprintf(TimestampBfr,"%04d%02d%02d%02d%02d%02d", // Format yyyymmddhhmmss
gmt->tm_year+1900,
gmt->tm_mon+1,
gmt->tm_mday,
gmt->tm_hour,
gmt->tm_min,
gmt->tm_sec
);
return string(TimestampBfr); // Return a string.
gmt = gmtime(&t); // Fill it with UTC.
bool isValidGMT = (0 != gmt);
if(isValidGMT) {
TimestampBfr
<< setw(4) << (gmt->tm_year+1900)
<< setw(2) << setfill('0')
<< (gmt->tm_mon+1)
<< (gmt->tm_mday)
<< (gmt->tm_hour)
<< (gmt->tm_min)
<< (gmt->tm_sec);
theTimeStamp = TimestampBfr.str();
}
return theTimeStamp; // Return a string.
}



Loading…
Cancel
Save