X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fdaemon%2Futils_time.h;h=9e98040694eb8b57822653e6fc1df5296646a314;hb=40aabf444e124f40603021821e98bcc40c4aaa0e;hp=6476780ad4f4e9cde02eeaf3044e39d7548de4cc;hpb=45e631cf6134de4a5dd3bcf01794c9fdd539c412;p=collectd.git diff --git a/src/daemon/utils_time.h b/src/daemon/utils_time.h index 6476780a..9e980406 100644 --- a/src/daemon/utils_time.h +++ b/src/daemon/utils_time.h @@ -1,6 +1,6 @@ /** - * collectd - src/utils_time.h - * Copyright (C) 2010 Florian octo Forster + * collectd - src/daemon/utils_time.h + * Copyright (C) 2010-2015 Florian octo Forster * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -21,7 +21,7 @@ * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian octo Forster + * Florian octo Forster **/ #ifndef UTILS_TIME_H @@ -29,6 +29,12 @@ #include "collectd.h" +#ifdef TESTING_H +/* cdtime_mock is the time returned by cdtime() when build with + * -DMOCK_TIME */ +extern cdtime_t cdtime_mock; +#endif + /* * "cdtime_t" is a 64bit unsigned integer. The time is stored at a 2^-30 second * resolution, i.e. the most significant 34 bit are used to store the time in @@ -51,13 +57,13 @@ #define NS_TO_CDTIME_T(ns) (((((cdtime_t) (ns)) / 1000000000) << 30) | \ ((((((cdtime_t) (ns)) % 1000000000) << 30) + 500000000) / 1000000000)) -#define CDTIME_T_TO_TIME_T(t) ((((time_t) (t)) + (1 << 29)) >> 30) -#define CDTIME_T_TO_MS(t) (((((uint64_t) (t)) >> 30) * 1000L) + \ - (((((uint64_t) (t)) & 0x3fffffff) * 1000L + (1 << 29)) >> 30)) -#define CDTIME_T_TO_US(t) (((((uint64_t) (t)) >> 30) * 1000000L) + \ - (((((uint64_t) (t)) & 0x3fffffff) * 1000000L + (1 << 29)) >> 30)) -#define CDTIME_T_TO_NS(t) (((((uint64_t) (t)) >> 30) * 1000000000L) + \ - (((((uint64_t) (t)) & 0x3fffffff) * 1000000000L + (1 << 29)) >> 30)) +#define CDTIME_T_TO_TIME_T(t) ((time_t) (((t) + (1 << 29)) >> 30)) +#define CDTIME_T_TO_MS(t) ((uint64_t) ((((t) >> 30) * 1000) + \ + ((((t) & 0x3fffffff) * 1000 + (1 << 29)) >> 30))) +#define CDTIME_T_TO_US(t) ((uint64_t) ((((t) >> 30) * 1000000) + \ + ((((t) & 0x3fffffff) * 1000000 + (1 << 29)) >> 30))) +#define CDTIME_T_TO_NS(t) ((uint64_t) ((((t) >> 30) * 1000000000) + \ + ((((t) & 0x3fffffff) * 1000000000 + (1 << 29)) >> 30))) #define CDTIME_T_TO_DOUBLE(t) (((double) (t)) / 1073741824.0) #define DOUBLE_TO_CDTIME_T(d) ((cdtime_t) ((d) * 1073741824.0)) @@ -72,15 +78,19 @@ (tsp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ (tsp)->tv_nsec = (long) CDTIME_T_TO_NS ((cdt) & 0x3fffffff); \ } while (0) -#define TIMESPEC_TO_CDTIME_T(ts) NS_TO_CDTIME_T(1000000000 * (ts)->tv_sec + (ts)->tv_nsec) +#define TIMESPEC_TO_CDTIME_T(ts) NS_TO_CDTIME_T(1000000000ULL * (ts)->tv_sec + (ts)->tv_nsec) cdtime_t cdtime (void); -/* format a cdtime_t value in ISO 8601 format: - * returns the number of characters written to the string (not including the - * terminating null byte or 0 on error; the function ensures that the string - * is null terminated */ -size_t cdtime_to_iso8601 (char *s, size_t max, cdtime_t t); +#define RFC3339_SIZE 26 +#define RFC3339NANO_SIZE 36 + +/* rfc3339 formats a cdtime_t time in RFC 3339 format with second precision. */ +int rfc3339 (char *buffer, size_t buffer_size, cdtime_t t); + +/* rfc3339nano formats a cdtime_t time in RFC 3339 format with nanosecond + * precision. */ +int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t); #endif /* UTILS_TIME_H */ /* vim: set sw=2 sts=2 et : */