{GPL, other}: Relicense to MIT license.
[collectd.git] / src / utils_time.c
1 /**
2  * collectd - src/utils_time.c
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 #include "collectd.h"
28 #include "utils_time.h"
29 #include "plugin.h"
30 #include "common.h"
31
32 #if HAVE_CLOCK_GETTIME
33 cdtime_t cdtime (void) /* {{{ */
34 {
35   int status;
36   struct timespec ts = { 0, 0 };
37
38   status = clock_gettime (CLOCK_REALTIME, &ts);
39   if (status != 0)
40   {
41     char errbuf[1024];
42     ERROR ("cdtime: clock_gettime failed: %s",
43         sstrerror (errno, errbuf, sizeof (errbuf)));
44     return (0);
45   }
46
47   return (TIMESPEC_TO_CDTIME_T (&ts));
48 } /* }}} cdtime_t cdtime */
49 #else
50 /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
51 cdtime_t cdtime (void) /* {{{ */
52 {
53   int status;
54   struct timeval tv = { 0, 0 };
55
56   status = gettimeofday (&tv, /* struct timezone = */ NULL);
57   if (status != 0)
58   {
59     char errbuf[1024];
60     ERROR ("cdtime: gettimeofday failed: %s",
61         sstrerror (errno, errbuf, sizeof (errbuf)));
62     return (0);
63   }
64
65   return (TIMEVAL_TO_CDTIME_T (&tv));
66 } /* }}} cdtime_t cdtime */
67 #endif
68
69 size_t cdtime_to_iso8601 (char *s, size_t max, cdtime_t t) /* {{{ */
70 {
71   struct timespec t_spec;
72   struct tm t_tm;
73
74   size_t len;
75
76   CDTIME_T_TO_TIMESPEC (t, &t_spec);
77   NORMALIZE_TIMESPEC (t_spec);
78
79   if (localtime_r ((time_t *)&t_spec.tv_sec, &t_tm) == NULL) {
80     char errbuf[1024];
81     ERROR ("cdtime_to_iso8601: localtime_r failed: %s",
82         sstrerror (errno, errbuf, sizeof (errbuf)));
83     return (0);
84   }
85
86   len = strftime (s, max, "%Y-%m-%dT%H:%M:%S", &t_tm);
87   if (len == 0)
88     return 0;
89
90   if (max - len > 2) {
91     int n = snprintf (s + len, max - len, ".%09i", (int)t_spec.tv_nsec);
92     len += (n < max - len) ? n : max - len;
93   }
94
95   if (max - len > 3) {
96     int n = strftime (s + len, max - len, "%z", &t_tm);
97     len += (n < max - len) ? n : max - len;
98   }
99
100   s[max - 1] = '\0';
101   return len;
102 } /* }}} size_t cdtime_to_iso8601 */
103
104 /* vim: set sw=2 sts=2 et fdm=marker : */