36ee8f2ae60ebf682633426ceb0bf61be9b2c47d
[collectd.git] / src / daemon / utils_time.c
1 /**
2  * collectd - src/utils_time.c
3  * Copyright (C) 2010-2015  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 <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "utils_time.h"
30 #include "plugin.h"
31 #include "common.h"
32
33 #ifndef DEFAULT_MOCK_TIME
34 # define DEFAULT_MOCK_TIME 1542455354518929408ULL
35 #endif
36
37 #ifdef MOCK_TIME
38 cdtime_t cdtime_mock = (cdtime_t) MOCK_TIME;
39
40 cdtime_t cdtime (void)
41 {
42   return cdtime_mock;
43 }
44 #else /* !MOCK_TIME */
45 # if HAVE_CLOCK_GETTIME
46 cdtime_t cdtime (void) /* {{{ */
47 {
48   int status;
49   struct timespec ts = { 0, 0 };
50
51   status = clock_gettime (CLOCK_REALTIME, &ts);
52   if (status != 0)
53   {
54     char errbuf[1024];
55     ERROR ("cdtime: clock_gettime failed: %s",
56         sstrerror (errno, errbuf, sizeof (errbuf)));
57     return 0;
58   }
59
60   return TIMESPEC_TO_CDTIME_T (&ts);
61 } /* }}} cdtime_t cdtime */
62 # else /* !HAVE_CLOCK_GETTIME */
63 /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
64 cdtime_t cdtime (void) /* {{{ */
65 {
66   int status;
67   struct timeval tv = { 0, 0 };
68
69   status = gettimeofday (&tv, /* struct timezone = */ NULL);
70   if (status != 0)
71   {
72     char errbuf[1024];
73     ERROR ("cdtime: gettimeofday failed: %s",
74         sstrerror (errno, errbuf, sizeof (errbuf)));
75     return 0;
76   }
77
78   return TIMEVAL_TO_CDTIME_T (&tv);
79 } /* }}} cdtime_t cdtime */
80 # endif
81 #endif
82
83 /**********************************************************************
84  Time retrieval functions
85 ***********************************************************************/
86
87 static int get_utc_time (cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
88 {
89   struct timespec t_spec;
90   int status;
91
92   CDTIME_T_TO_TIMESPEC (t, &t_spec);
93   NORMALIZE_TIMESPEC (t_spec);
94
95   if (gmtime_r (&t_spec.tv_sec, t_tm) == NULL) {
96     char errbuf[1024];
97     status = errno;
98     ERROR ("get_utc_time: gmtime_r failed: %s",
99         sstrerror (status, errbuf, sizeof (errbuf)));
100     return status;
101   }
102
103   *nsec = t_spec.tv_nsec;
104   return 0;
105 } /* }}} int get_utc_time */
106
107 static int get_local_time (cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
108 {
109   struct timespec t_spec;
110   int status;
111
112   CDTIME_T_TO_TIMESPEC (t, &t_spec);
113   NORMALIZE_TIMESPEC (t_spec);
114
115   if (localtime_r (&t_spec.tv_sec, t_tm) == NULL) {
116     char errbuf[1024];
117     status = errno;
118     ERROR ("get_local_time: localtime_r failed: %s",
119         sstrerror (status, errbuf, sizeof (errbuf)));
120     return status;
121   }
122
123   *nsec = t_spec.tv_nsec;
124   return 0;
125 } /* }}} int get_local_time */
126
127 /**********************************************************************
128  Formatting functions
129 ***********************************************************************/
130
131 static const char utc_zone[] = "+00:00";
132 static const char zulu_zone[] = "Z";
133
134 /* format_zone reads time zone information from "extern long timezone", exported
135  * by <time.h>, and formats it according to RFC 3339. This differs from
136  * strftime()'s "%z" format by including a colon between hour and minute. */
137 static int format_zone (char *buffer, size_t buffer_size) /* {{{ */
138 {
139   struct tm t_tm = { 0 };  /* The value doesn't matter. */
140   char tmp[7];
141   size_t sz;
142
143   if ((buffer == NULL) || (buffer_size < 7))
144     return EINVAL;
145
146   sz = strftime (tmp, sizeof (tmp), "%z", &t_tm);
147   if (sz == 0)
148     return ENOMEM;
149   if (sz != 5)
150   {
151     DEBUG ("format_zone: strftime(\"%%z\") = \"%s\", want \"+hhmm\"", tmp);
152     sstrncpy (buffer, tmp, buffer_size);
153     return 0;
154   }
155
156   buffer[0] = tmp[0];
157   buffer[1] = tmp[1];
158   buffer[2] = tmp[2];
159   buffer[3] = ':';
160   buffer[4] = tmp[3];
161   buffer[5] = tmp[4];
162   buffer[6] = 0;
163
164   return 0;
165 } /* }}} int format_zone */
166
167 int format_rfc3339 (char *buffer, size_t buffer_size, struct tm const *t_tm, long nsec, _Bool print_nano, char const *zone) /* {{{ */
168 {
169   int len;
170   char *pos = buffer;
171   size_t size_left = buffer_size;
172
173   if ((len = strftime (pos, size_left, "%Y-%m-%dT%H:%M:%S", t_tm)) == 0)
174     return ENOMEM;
175   pos += len;
176   size_left -= len;
177
178   if (print_nano) {
179     if ((len = ssnprintf (pos, size_left, ".%09ld", nsec)) == 0)
180       return ENOMEM;
181     pos += len;
182     size_left -= len;
183   }
184
185   sstrncpy (buffer, zone, buffer_size);
186   return 0;
187 } /* }}} int format_rfc3339 */
188
189 int format_rfc3339_utc (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano, char const *zone) /* {{{ */
190 {
191   struct tm t_tm;
192   long nsec;
193   int status;
194
195   if ((status = get_utc_time (t, &t_tm, &nsec)) != 0)
196     return status;  /* The error should have already be reported. */
197
198   return format_rfc3339 (buffer, buffer_size, &t_tm, nsec, print_nano, zone);
199 } /* }}} int format_rfc3339_utc */
200
201 int format_rfc3339_local (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */
202 {
203   struct tm t_tm;
204   long nsec;
205   int status;
206   char zone[7];  /* +00:00 */
207
208   if ((status = get_local_time (t, &t_tm, &nsec)) != 0)
209     return status;  /* The error should have already be reported. */
210
211   if ((status = format_zone (zone, sizeof (zone))) != 0)
212     return status;
213
214   return format_rfc3339 (buffer, buffer_size, &t_tm, nsec, print_nano, zone);
215 } /* }}} int format_rfc3339_local */
216
217 /**********************************************************************
218  Public functions
219 ***********************************************************************/
220
221 int rfc3339 (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
222 {
223   if (buffer_size < RFC3339_SIZE)
224     return ENOMEM;
225
226   return format_rfc3339_utc (buffer, buffer_size, t, 0, utc_zone);
227 } /* }}} int rfc3339 */
228
229 int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
230 {
231   if (buffer_size < RFC3339NANO_SIZE)
232     return ENOMEM;
233
234   return format_rfc3339_utc (buffer, buffer_size, t, 1, utc_zone);
235 } /* }}} int rfc3339nano */
236
237 int rfc3339_zulu (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
238 {
239   if (buffer_size < RFC3339_ZULU_SIZE)
240     return ENOMEM;
241
242   return format_rfc3339_utc (buffer, buffer_size, t, 0, zulu_zone);
243 } /* }}} int rfc3339_zulu */
244
245 int rfc3339nano_zulu (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
246 {
247   if (buffer_size < RFC3339NANO_ZULU_SIZE)
248     return ENOMEM;
249
250   return format_rfc3339_utc (buffer, buffer_size, t, 1, zulu_zone);
251 } /* }}} int rfc3339nano_zulu */
252
253 int rfc3339_local (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
254 {
255   if (buffer_size < RFC3339_SIZE)
256     return ENOMEM;
257
258   return format_rfc3339_local (buffer, buffer_size, t, 0);
259 } /* }}} int rfc3339 */
260
261 int rfc3339nano_local (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
262 {
263   if (buffer_size < RFC3339NANO_SIZE)
264     return ENOMEM;
265
266   return format_rfc3339_local (buffer, buffer_size, t, 1);
267 } /* }}} int rfc3339nano */
268
269 /* vim: set sw=2 sts=2 et fdm=marker : */