From 414028a05f7a1291f13825902b52d6c515bed3a4 Mon Sep 17 00:00:00 2001 From: Nathan Wolfe Date: Wed, 10 Apr 2024 11:57:11 -0700 Subject: [PATCH 1/3] Remove unwanted newline character at end of timestamp --- orchagent/portsorch.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/orchagent/portsorch.cpp b/orchagent/portsorch.cpp index 50c23d4aec..cceaa19879 100755 --- a/orchagent/portsorch.cpp +++ b/orchagent/portsorch.cpp @@ -3120,12 +3120,18 @@ void PortsOrch::updateDbPortFlapCount(Port& port, sai_port_oper_status_t pstatus std::time_t now_c = std::chrono::system_clock::to_time_t(now); if (pstatus == SAI_PORT_OPER_STATUS_DOWN) { - FieldValueTuple tuple("last_down_time", std::ctime(&now_c)); + // std::ctime returns a string with an unwanted newline character so we trim it + char * timeStr = std::ctime(&now_c); + timeStr[strlen(timeStr)-1] = '\0'; + FieldValueTuple tuple("last_down_time", timeStr); tuples.push_back(tuple); } else if (pstatus == SAI_PORT_OPER_STATUS_UP) { - FieldValueTuple tuple("last_up_time", std::ctime(&now_c)); + // std::ctime returns a string with an unwanted newline character so we trim it + char * timeStr = std::ctime(&now_c); + timeStr[strlen(timeStr)-1] = '\0'; + FieldValueTuple tuple("last_up_time", timeStr); tuples.push_back(tuple); } m_portTable->set(port.m_alias, tuples); From 64c61e9c7cd5c1df4f8be548e7162fff5eac8918 Mon Sep 17 00:00:00 2001 From: Nathan Wolfe Date: Thu, 11 Apr 2024 09:32:45 -0700 Subject: [PATCH 2/3] Cleaner approach to generate timestamp string with no newline character --- orchagent/portsorch.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/orchagent/portsorch.cpp b/orchagent/portsorch.cpp index cceaa19879..772783b7cb 100755 --- a/orchagent/portsorch.cpp +++ b/orchagent/portsorch.cpp @@ -3120,18 +3120,18 @@ void PortsOrch::updateDbPortFlapCount(Port& port, sai_port_oper_status_t pstatus std::time_t now_c = std::chrono::system_clock::to_time_t(now); if (pstatus == SAI_PORT_OPER_STATUS_DOWN) { - // std::ctime returns a string with an unwanted newline character so we trim it - char * timeStr = std::ctime(&now_c); - timeStr[strlen(timeStr)-1] = '\0'; - FieldValueTuple tuple("last_down_time", timeStr); + char buffer[32]; + // Format: Www Mmm dd hh:mm:ss yyyy + std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", std::localtime(&now_c)); + FieldValueTuple tuple("last_down_time", buffer); tuples.push_back(tuple); } else if (pstatus == SAI_PORT_OPER_STATUS_UP) { - // std::ctime returns a string with an unwanted newline character so we trim it - char * timeStr = std::ctime(&now_c); - timeStr[strlen(timeStr)-1] = '\0'; - FieldValueTuple tuple("last_up_time", timeStr); + char buffer[32]; + // Format: Www Mmm dd hh:mm:ss yyyy + std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", std::localtime(&now_c)); + FieldValueTuple tuple("last_up_time", buffer); tuples.push_back(tuple); } m_portTable->set(port.m_alias, tuples); From f2d66068dc076a8eb66f61eeb831c052a0be07c7 Mon Sep 17 00:00:00 2001 From: Nathan Wolfe Date: Mon, 15 Apr 2024 13:58:15 -0700 Subject: [PATCH 3/3] Use UTC rather than local time in timestamp --- orchagent/portsorch.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/orchagent/portsorch.cpp b/orchagent/portsorch.cpp index 772783b7cb..4f2e709182 100755 --- a/orchagent/portsorch.cpp +++ b/orchagent/portsorch.cpp @@ -3122,7 +3122,7 @@ void PortsOrch::updateDbPortFlapCount(Port& port, sai_port_oper_status_t pstatus { char buffer[32]; // Format: Www Mmm dd hh:mm:ss yyyy - std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", std::localtime(&now_c)); + std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", std::gmtime(&now_c)); FieldValueTuple tuple("last_down_time", buffer); tuples.push_back(tuple); } @@ -3130,7 +3130,7 @@ void PortsOrch::updateDbPortFlapCount(Port& port, sai_port_oper_status_t pstatus { char buffer[32]; // Format: Www Mmm dd hh:mm:ss yyyy - std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", std::localtime(&now_c)); + std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", std::gmtime(&now_c)); FieldValueTuple tuple("last_up_time", buffer); tuples.push_back(tuple); }