Commit 65c592f7 authored by Eric Cano's avatar Eric Cano
Browse files

Create util function to print out local time.

parent ab6bba1d
......@@ -659,4 +659,13 @@ TEST_F(cta_UtilsTest, ellipses) {
ASSERT_EQ("[...]0ABCDEF", preEllipsis("1234567890ABCDEF", 12));
}
TEST_F(cta_UtilsTest, DISABLED_currentTime) {
/* This test is disabled as it prints our similar, yet slightly different dates,
* so it would be complex to automate.
* Just run with: "cta-unitTests --gtest_filter=*currentTime --gtest_also_run_disabled_tests" */
using namespace cta::utils;
::system("date \"+%h %e %H:%M:%S.%N\" ");
std::cout << getCurrentLocalTime() << std::endl;
}
} // namespace unitTests
......@@ -780,5 +780,22 @@ void copyString(char *const dst, const size_t dstSize, const std::string &src) {
*(dst + dstSize -1) = '\0'; // Ensure destination string is null terminated
}
//-----------------------------------------------------------------------------
// getCurrentLocalTime
//-----------------------------------------------------------------------------
std::string getCurrentLocalTime() {
::timeval tv;
gettimeofday(&tv, nullptr);
time_t now = (time_t)tv.tv_sec;
struct tm * localNow;
::time(&now);
localNow = ::localtime(&now);
char buff[80];
char buff2[10];
::strftime(buff,sizeof(buff), "%b %e %H:%M:%S.", localNow);
::snprintf(buff2, sizeof(buff2), "%06ld", tv.tv_usec);
return std::string(buff) + std::string(buff2);
}
} // namespace utils
} // namespace cta
......@@ -364,6 +364,12 @@ namespace utils {
const std::string &src) {
copyString(dst, dstSize, src);
}
/**
* Returns the current time, in the local time zone, formated for logging.
* @return the foramtted time.
*/
std::string getCurrentLocalTime();
} // namespace utils
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment