2 * collectd - src/utils_time.c
3 * Copyright (C) 2010-2015 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
31 #include "utils_time.h"
33 #ifndef DEFAULT_MOCK_TIME
34 #define DEFAULT_MOCK_TIME 1542455354518929408ULL
38 cdtime_t cdtime_mock = (cdtime_t)MOCK_TIME;
40 cdtime_t cdtime(void) { return cdtime_mock; }
41 #else /* !MOCK_TIME */
42 #if HAVE_CLOCK_GETTIME
43 cdtime_t cdtime(void) /* {{{ */
46 struct timespec ts = {0, 0};
48 status = clock_gettime(CLOCK_REALTIME, &ts);
51 ERROR("cdtime: clock_gettime failed: %s",
52 sstrerror(errno, errbuf, sizeof(errbuf)));
56 return TIMESPEC_TO_CDTIME_T(&ts);
57 } /* }}} cdtime_t cdtime */
58 #else /* !HAVE_CLOCK_GETTIME */
59 /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
60 cdtime_t cdtime(void) /* {{{ */
63 struct timeval tv = {0, 0};
65 status = gettimeofday(&tv, /* struct timezone = */ NULL);
68 ERROR("cdtime: gettimeofday failed: %s",
69 sstrerror(errno, errbuf, sizeof(errbuf)));
73 return TIMEVAL_TO_CDTIME_T(&tv);
74 } /* }}} cdtime_t cdtime */
78 /**********************************************************************
79 Time retrieval functions
80 ***********************************************************************/
82 static int get_utc_time(cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
84 struct timespec t_spec = CDTIME_T_TO_TIMESPEC(t);
85 NORMALIZE_TIMESPEC(t_spec);
87 if (gmtime_r(&t_spec.tv_sec, t_tm) == NULL) {
90 ERROR("get_utc_time: gmtime_r failed: %s",
91 sstrerror(status, errbuf, sizeof(errbuf)));
95 *nsec = t_spec.tv_nsec;
97 } /* }}} int get_utc_time */
99 static int get_local_time(cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
101 struct timespec t_spec = CDTIME_T_TO_TIMESPEC(t);
102 NORMALIZE_TIMESPEC(t_spec);
104 if (localtime_r(&t_spec.tv_sec, t_tm) == NULL) {
107 ERROR("get_local_time: localtime_r failed: %s",
108 sstrerror(status, errbuf, sizeof(errbuf)));
112 *nsec = t_spec.tv_nsec;
114 } /* }}} int get_local_time */
116 /**********************************************************************
118 ***********************************************************************/
120 static const char zulu_zone[] = "Z";
122 /* format_zone reads time zone information from "extern long timezone", exported
123 * by <time.h>, and formats it according to RFC 3339. This differs from
124 * strftime()'s "%z" format by including a colon between hour and minute. */
125 static int format_zone(char *buffer, size_t buffer_size,
126 struct tm const *tm) /* {{{ */
131 if ((buffer == NULL) || (buffer_size < 7))
134 sz = strftime(tmp, sizeof(tmp), "%z", tm);
138 DEBUG("format_zone: strftime(\"%%z\") = \"%s\", want \"+hhmm\"", tmp);
139 sstrncpy(buffer, tmp, buffer_size);
152 } /* }}} int format_zone */
154 int format_rfc3339(char *buffer, size_t buffer_size, struct tm const *t_tm,
155 long nsec, _Bool print_nano, char const *zone) /* {{{ */
159 size_t size_left = buffer_size;
161 if ((len = strftime(pos, size_left, "%Y-%m-%dT%H:%M:%S", t_tm)) == 0)
167 if ((len = ssnprintf(pos, size_left, ".%09ld", nsec)) == 0)
173 sstrncpy(pos, zone, size_left);
175 } /* }}} int format_rfc3339 */
177 int format_rfc3339_utc(char *buffer, size_t buffer_size, cdtime_t t,
178 _Bool print_nano) /* {{{ */
184 if ((status = get_utc_time(t, &t_tm, &nsec)) != 0)
185 return status; /* The error should have already be reported. */
187 return format_rfc3339(buffer, buffer_size, &t_tm, nsec, print_nano,
189 } /* }}} int format_rfc3339_utc */
191 int format_rfc3339_local(char *buffer, size_t buffer_size, cdtime_t t,
192 _Bool print_nano) /* {{{ */
197 char zone[7]; /* +00:00 */
199 if ((status = get_local_time(t, &t_tm, &nsec)) != 0)
200 return status; /* The error should have already be reported. */
202 if ((status = format_zone(zone, sizeof(zone), &t_tm)) != 0)
205 return format_rfc3339(buffer, buffer_size, &t_tm, nsec, print_nano, zone);
206 } /* }}} int format_rfc3339_local */
208 /**********************************************************************
210 ***********************************************************************/
212 int rfc3339(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
214 if (buffer_size < RFC3339_SIZE)
217 return format_rfc3339_utc(buffer, buffer_size, t, 0);
218 } /* }}} int rfc3339 */
220 int rfc3339nano(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
222 if (buffer_size < RFC3339NANO_SIZE)
225 return format_rfc3339_utc(buffer, buffer_size, t, 1);
226 } /* }}} int rfc3339nano */
228 int rfc3339_local(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
230 if (buffer_size < RFC3339_SIZE)
233 return format_rfc3339_local(buffer, buffer_size, t, 0);
234 } /* }}} int rfc3339 */
236 int rfc3339nano_local(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
238 if (buffer_size < RFC3339NANO_SIZE)
241 return format_rfc3339_local(buffer, buffer_size, t, 1);
242 } /* }}} int rfc3339nano */
244 /* vim: set sw=2 sts=2 et fdm=marker : */