Add ParseServerTime TZ fix and tests

This commit is contained in:
John Marrett
2022-12-29 07:53:39 -05:00
parent 4ffbc39e3e
commit 2a6a1d70ff
2 changed files with 36 additions and 0 deletions
+15
View File
@@ -587,7 +587,22 @@ timeval CUtils::ParseServerTime(const CString& sTime) {
struct timeval tv;
memset(&tv, 0, sizeof(tv));
if (cp) {
char* oldTZ = getenv("TZ");
if (oldTZ) oldTZ = strdup(oldTZ);
setenv("TZ", "UTC", 1);
tzset();
tv.tv_sec = mktime(&stm);
// restore old value
if (oldTZ) {
setenv("TZ", oldTZ, 1);
free(oldTZ);
} else {
unsetenv("TZ");
}
tzset();
CString s_usec(cp);
if (s_usec.TrimPrefix(".") && s_usec.TrimSuffix("Z")) {
tv.tv_usec = s_usec.ToULong() * 1000;
+21
View File
@@ -109,6 +109,27 @@ TEST(UtilsTest, ServerTime) {
CString str3 = CUtils::FormatServerTime(tv3);
EXPECT_EQ(str3, "1970-01-01T00:00:00.000Z");
if (oldTZ) {
setenv("TZ", oldTZ, 1);
free(oldTZ);
} else {
unsetenv("TZ");
}
tzset();
}
TEST(UtilsTest, ParseServerTime) {
char* oldTZ = getenv("TZ");
if (oldTZ) oldTZ = strdup(oldTZ);
setenv("TZ", "America/Montreal", 1);
tzset();
timeval tv4 = CUtils::ParseServerTime("2011-10-19T16:40:51.620Z");
CString str4 = CUtils::FormatServerTime(tv4);
EXPECT_EQ(str4, "2011-10-19T16:40:51.620Z");
if (oldTZ) {
setenv("TZ", oldTZ, 1);
free(oldTZ);