Modify Timestamp to RFC3339 Format
Modify host log timestamp to RFC3339 time format to provide more time information.
Origin sample message:
[ 00:00:31 ] >>> Log collection started at 1970-01-01 00:00:31
After modified, the message will be liked:
[ 1970-01-01T00:00:31+00:00 ] >>> Log collection started at 1970-01-01 00:00:31
Signed-off-by: SpencerKu <Spencer.Ku@quantatw.com>
Change-Id: Ief4c074523830d1ce1f90658085ce661c0220573
diff --git a/src/zlib_file.cpp b/src/zlib_file.cpp
index 042dafb..73695b0 100644
--- a/src/zlib_file.cpp
+++ b/src/zlib_file.cpp
@@ -41,9 +41,17 @@
{
int rc;
- // Write time stamp
- rc = gzprintf(fd, "[ %02i:%02i:%02i ] ", timeStamp.tm_hour,
- timeStamp.tm_min, timeStamp.tm_sec);
+ // Write time stamp.
+ // "tm_gmtoff" is the number of seconds east of UTC, so we need to calculate
+ // timezone offset. For example, for U.S. Eastern Standard Time, the value
+ // is -18000 = -5*60*60."
+
+ rc = gzprintf(fd, "[ %i-%02i-%02iT%02i:%02i:%02i%+03ld:%02ld ] ",
+ timeStamp.tm_year + 1900, timeStamp.tm_mon + 1,
+ timeStamp.tm_mday, timeStamp.tm_hour, timeStamp.tm_min,
+ timeStamp.tm_sec, timeStamp.tm_gmtoff / (60 * 60),
+ abs(timeStamp.tm_gmtoff % (60 * 60)) / 60);
+
if (rc <= 0)
{
throw ZlibException(ZlibException::write, rc, fd, fileName);
diff --git a/test/zlib_file_test.cpp b/test/zlib_file_test.cpp
index ef49a6f..65f2206 100644
--- a/test/zlib_file_test.cpp
+++ b/test/zlib_file_test.cpp
@@ -25,9 +25,13 @@
file.close();
char expect[64];
- const int len = snprintf(expect, sizeof(expect), "[ %02i:%02i:%02i ] %s\n",
- localTime.tm_hour, localTime.tm_min,
- localTime.tm_sec, msg.c_str());
+ const int len =
+ snprintf(expect, sizeof(expect),
+ "[ %i-%02i-%02iT%02i:%02i:%02i%+03ld:%02ld ] %s\n",
+ localTime.tm_year + 1900, localTime.tm_mon + 1,
+ localTime.tm_mday, localTime.tm_hour, localTime.tm_min,
+ localTime.tm_sec, localTime.tm_gmtoff / (60 * 60),
+ abs(localTime.tm_gmtoff % (60 * 60)) / 60, msg.c_str());
gzFile fd = gzopen(path.c_str(), "r");
ASSERT_TRUE(fd);