CUtils::ParseServerTime(): fix handling of invalid timestamps

This commit is contained in:
J-P Nurmi
2015-09-18 01:16:01 +02:00
parent f1973fe81b
commit 683379df7d
2 changed files with 11 additions and 4 deletions
+7 -4
View File
@@ -463,10 +463,13 @@ timeval CUtils::ParseServerTime(const CString& sTime) {
memset(&stm, 0, sizeof(stm));
const char* cp = strptime(sTime.c_str(), "%Y-%m-%dT%H:%M:%S", &stm);
struct timeval tv;
tv.tv_sec = mktime(&stm);
CString s_usec(cp);
if (s_usec.TrimPrefix(".") && s_usec.TrimSuffix("Z")) {
tv.tv_usec = s_usec.ToULong() * 1000;
memset(&tv, 0, sizeof(stm));
if (cp) {
tv.tv_sec = mktime(&stm);
CString s_usec(cp);
if (s_usec.TrimPrefix(".") && s_usec.TrimSuffix("Z")) {
tv.tv_usec = s_usec.ToULong() * 1000;
}
}
return tv;
}
+4
View File
@@ -96,6 +96,10 @@ TEST(UtilsTest, ServerTime) {
EXPECT_EQ(now.tv_sec, tv2.tv_sec);
EXPECT_EQ(now.tv_usec, tv2.tv_usec);
timeval tv3 = CUtils::ParseServerTime("invalid");
CString str3 = CUtils::FormatServerTime(tv3);
EXPECT_EQ("1970-01-01T00:00:00.000Z", str3);
if (oldTZ) {
setenv("TZ", oldTZ, 1);
free(oldTZ);