{GPL, other}: Relicense to MIT license.
[collectd.git] / src / utils_time.h
1 /**
2  * collectd - src/utils_time.h
3  * Copyright (C) 2010       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <ff at octo.it>
25  **/
26
27 #ifndef UTILS_TIME_H
28 #define UTILS_TIME_H 1
29
30 #include "collectd.h"
31
32 /*
33  * "cdtime_t" is a 64bit unsigned integer. The time is stored at a 2^-30 second
34  * resolution, i.e. the most significant 34 bit are used to store the time in
35  * seconds, the least significant bits store the sub-second part in something
36  * very close to nanoseconds. *The* big advantage of storing time in this
37  * manner is that comparing times and calculating differences is as simple as
38  * it is with "time_t", i.e. a simple integer comparison / subtraction works.
39  */
40 /* 
41  * cdtime_t is defined in "collectd.h" */
42 /* typedef uint64_t cdtime_t; */
43
44 /* 2^30 = 1073741824 */
45 #define TIME_T_TO_CDTIME_T(t) (((cdtime_t) (t)) * 1073741824)
46 #define CDTIME_T_TO_TIME_T(t) ((time_t) ((t) / 1073741824))
47
48 #define CDTIME_T_TO_DOUBLE(t) (((double) (t)) / 1073741824.0)
49 #define DOUBLE_TO_CDTIME_T(d) ((cdtime_t) ((d) * 1073741824.0))
50
51 #define MS_TO_CDTIME_T(ms) ((cdtime_t)    (((double) (ms)) * 1073741.824))
52 #define CDTIME_T_TO_MS(t)  ((long)        (((double) (t))  / 1073741.824))
53 #define US_TO_CDTIME_T(us) ((cdtime_t)    (((double) (us)) * 1073.741824))
54 #define CDTIME_T_TO_US(t)  ((suseconds_t) (((double) (t))  / 1073.741824))
55 #define NS_TO_CDTIME_T(ns) ((cdtime_t)    (((double) (ns)) * 1.073741824))
56 #define CDTIME_T_TO_NS(t)  ((long)        (((double) (t))  / 1.073741824))
57
58 #define CDTIME_T_TO_TIMEVAL(cdt,tvp) do {                                    \
59         (tvp)->tv_sec = CDTIME_T_TO_TIME_T (cdt);                            \
60         (tvp)->tv_usec = CDTIME_T_TO_US ((cdt) % 1073741824);                \
61 } while (0)
62 #define TIMEVAL_TO_CDTIME_T(tv) (TIME_T_TO_CDTIME_T ((tv)->tv_sec)           \
63     + US_TO_CDTIME_T ((tv)->tv_usec))
64
65 #define CDTIME_T_TO_TIMESPEC(cdt,tsp) do {                                   \
66   (tsp)->tv_sec = CDTIME_T_TO_TIME_T (cdt);                                  \
67   (tsp)->tv_nsec = CDTIME_T_TO_NS ((cdt) % 1073741824);                      \
68 } while (0)
69 #define TIMESPEC_TO_CDTIME_T(ts) (TIME_T_TO_CDTIME_T ((ts)->tv_sec)           \
70     + NS_TO_CDTIME_T ((ts)->tv_nsec))
71
72 cdtime_t cdtime (void);
73
74 /* format a cdtime_t value in ISO 8601 format:
75  * returns the number of characters written to the string (not including the
76  * terminating null byte or 0 on error; the function ensures that the string
77  * is null terminated */
78 size_t cdtime_to_iso8601 (char *s, size_t max, cdtime_t t);
79
80 #endif /* UTILS_TIME_H */
81 /* vim: set sw=2 sts=2 et : */