Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / common / time.cpp
index 7149912468d2c9d7a6acd41d41e13fe9eed92e90..ca29b2d61891efad38919300ee6771cc70f7008d 100644 (file)
@@ -5,22 +5,23 @@
  *
  */
 
-#include <common/time.hpp>
+#include <common/compat/errno.hpp>
 #include <common/error.hpp>
+#include <common/exception.hpp>
 #include <common/macros.hpp>
-#include <common/error.hpp>
-#include <common/compat/errno.hpp>
-#include <stddef.h>
-#include <stdint.h>
+#include <common/time.hpp>
+
+#include <algorithm>
 #include <limits.h>
-#include <pthread.h>
 #include <locale.h>
+#include <pthread.h>
+#include <stddef.h>
+#include <stdint.h>
 #include <string.h>
-#include <algorithm>
 
 static bool utf8_output_supported;
 
-bool locale_supports_utf8(void)
+bool locale_supports_utf8()
 {
        return utf8_output_supported;
 }
@@ -31,13 +32,13 @@ int timespec_to_ms(struct timespec ts, unsigned long *ms)
 
        if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) {
                errno = EOVERFLOW;
-               return -1;      /* multiplication overflow */
+               return -1; /* multiplication overflow */
        }
        res = ts.tv_sec * MSEC_PER_SEC;
        remain_ms = ULONG_MAX - res;
        if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) {
                errno = EOVERFLOW;
-               return -1;      /* addition overflow */
+               return -1; /* addition overflow */
        }
        res += ts.tv_nsec / NSEC_PER_MSEC;
        *ms = res;
@@ -46,10 +47,8 @@ int timespec_to_ms(struct timespec ts, unsigned long *ms)
 
 struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2)
 {
-       uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC +
-                       (uint64_t) t1.tv_nsec;
-       uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC +
-                       (uint64_t) t2.tv_nsec;
+       uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + (uint64_t) t1.tv_nsec;
+       uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + (uint64_t) t2.tv_nsec;
        uint64_t diff = std::max(ts1, ts2) - std::min(ts1, ts2);
        struct timespec res;
 
@@ -58,10 +57,9 @@ struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2)
        return res;
 }
 
-static
-void __attribute__((constructor)) init_locale_utf8_support(void)
+static void __attribute__((constructor)) init_locale_utf8_support()
 {
-       const char *program_locale = setlocale(LC_ALL, NULL);
+       const char *program_locale = setlocale(LC_ALL, nullptr);
        const char *lang = getenv("LANG");
 
        if (program_locale && strstr(program_locale, "utf8")) {
@@ -80,7 +78,8 @@ int time_to_iso8601_str(time_t time, char *str, size_t len)
 
        if (len < ISO8601_STR_LEN) {
                ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed",
-                               len, ISO8601_STR_LEN);
+                   len,
+                   ISO8601_STR_LEN);
                ret = -1;
                goto end;
        }
@@ -111,7 +110,8 @@ int time_to_datetime_str(time_t time, char *str, size_t len)
 
        if (len < DATETIME_STR_LEN) {
                ERR("Buffer too short to format to datetime: %zu bytes provided when at least %zu are needed",
-                               len, DATETIME_STR_LEN);
+                   len,
+                   DATETIME_STR_LEN);
                ret = -1;
                goto end;
        }
@@ -132,3 +132,18 @@ int time_to_datetime_str(time_t time, char *str, size_t len)
 end:
        return ret;
 }
+
+std::string lttng::utils::time_to_iso8601_str(std::time_t time)
+{
+       std::string iso8601_str(ISO8601_STR_LEN, '\0');
+       const auto ret = ::time_to_iso8601_str(time, &iso8601_str[0], iso8601_str.capacity());
+
+       if (ret) {
+               LTTNG_THROW_ERROR("Failed to format time to iso8601 format");
+       }
+
+       /* Don't include '\0' in the C++ string. */
+       iso8601_str.resize(iso8601_str.size() - 1);
+
+       return iso8601_str;
+}
This page took 0.024687 seconds and 4 git commands to generate.