Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
dCache
cta
Commits
65c592f7
Commit
65c592f7
authored
Apr 04, 2018
by
Eric Cano
Browse files
Create util function to print out local time.
parent
ab6bba1d
Changes
3
Hide whitespace changes
Inline
Side-by-side
common/utils/UtilsTest.cpp
View file @
65c592f7
...
...
@@ -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
common/utils/utils.cpp
View file @
65c592f7
...
...
@@ -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
common/utils/utils.hpp
View file @
65c592f7
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment