diff options
author | Mihai Moldovan <ionic@ionic.de> | 2018-01-09 01:08:17 +0100 |
---|---|---|
committer | Mihai Moldovan <ionic@ionic.de> | 2018-01-09 01:08:17 +0100 |
commit | 1086c4aa5b4ad65bb51dac1dbab606f14ebaf98b (patch) | |
tree | 1365ea718d79998976bfab487b596ef48ce34b0c /nxcomp/src/Timestamp.cpp | |
parent | 70e1e6a011126aa027456ad4b3163f67d72c034f (diff) | |
parent | 2eb2f2e6ca13d84113e30041ade2dbfa9f4e1432 (diff) | |
download | nx-libs-1086c4aa5b4ad65bb51dac1dbab606f14ebaf98b.tar.gz nx-libs-1086c4aa5b4ad65bb51dac1dbab606f14ebaf98b.tar.bz2 nx-libs-1086c4aa5b4ad65bb51dac1dbab606f14ebaf98b.zip |
Merge branch 'Ionic-bugfix/ctime' into 3.6.x
Attributes GH PR #623: https://github.com/ArcticaProject/nx-libs/pull/623
Fixes: ArcticaProject/nx-libs#616
Diffstat (limited to 'nxcomp/src/Timestamp.cpp')
-rw-r--r-- | nxcomp/src/Timestamp.cpp | 55 |
1 files changed, 38 insertions, 17 deletions
diff --git a/nxcomp/src/Timestamp.cpp b/nxcomp/src/Timestamp.cpp index e7e0c494a..4a9dd3347 100644 --- a/nxcomp/src/Timestamp.cpp +++ b/nxcomp/src/Timestamp.cpp @@ -44,34 +44,55 @@ T_timestamp timestamp; -// -// The following functions all use the ctime -// static buffer from the C library. -// - -char *strTimestamp(const T_timestamp &ts) +std::string strTimestamp(const T_timestamp &ts) { - char *ctime_now = ctime((time_t *) &ts.tv_sec); + std::string ret; + + char ctime_now[26] = { }; + bool err = true; - ctime_now[24] = '\0'; +#if HAVE_CTIME_S + errno_t retval = ::ctime_s(ctime_now, sizeof(ctime_now), static_cast<const time_t*>(&ts.tv_sec)); - return ctime_now; + if (retval != 0) +#else + char *retval = ::ctime_r(static_cast<const time_t*>(&ts.tv_sec), ctime_now); + + if (!(retval)) +#endif + { + std::cerr << "WARNING: converting time to string failed." << std::endl; + } + else + { + /* Replace newline at position 25 with a NULL byte. */ + ctime_now[24] = '\0'; + + ret = ctime_now; + } + + return ret; } // -// This is especially dirty. +// This is especially dirty. // -char *strMsTimestamp(const T_timestamp &ts) +std::string strMsTimestamp(const T_timestamp &ts) { - char *ctime_now = ctime((time_t *) &ts.tv_sec); + std::string ret; + + std::string ctime_now = strTimestamp(ts); - char ctime_new[25]; + if (!(ctime_now.empty())) + { + char ctime_new[26] = { }; - sprintf(ctime_new, "%.8s:%3.3f", ctime_now + 11, - (float) ts.tv_usec / 1000); + snprintf(ctime_new, sizeof(ctime_new), "%.8s:%3.3f", + ctime_now.c_str() + 11, static_cast<float>(ts.tv_usec) / 1000); - strncpy(ctime_now, ctime_new, 24); + ret = ctime_new; + } - return ctime_now; + return ret; } |