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>
28 #include "utils_time.h"
32 #ifndef DEFAULT_MOCK_TIME
33 # define DEFAULT_MOCK_TIME 1542455354518929408ULL
37 cdtime_t cdtime_mock = (cdtime_t) MOCK_TIME;
39 cdtime_t cdtime (void)
43 #else /* !MOCK_TIME */
44 # if HAVE_CLOCK_GETTIME
45 cdtime_t cdtime (void) /* {{{ */
48 struct timespec ts = { 0, 0 };
50 status = clock_gettime (CLOCK_REALTIME, &ts);
54 ERROR ("cdtime: clock_gettime failed: %s",
55 sstrerror (errno, errbuf, sizeof (errbuf)));
59 return (TIMESPEC_TO_CDTIME_T (&ts));
60 } /* }}} cdtime_t cdtime */
61 # else /* !HAVE_CLOCK_GETTIME */
62 /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
63 cdtime_t cdtime (void) /* {{{ */
66 struct timeval tv = { 0, 0 };
68 status = gettimeofday (&tv, /* struct timezone = */ NULL);
72 ERROR ("cdtime: gettimeofday failed: %s",
73 sstrerror (errno, errbuf, sizeof (errbuf)));
77 return (TIMEVAL_TO_CDTIME_T (&tv));
78 } /* }}} cdtime_t cdtime */
82 /* format_zone reads time zone information from "extern long timezone", exported
83 * by <time.h>, and formats it according to RFC 3339. This differs from
84 * strftime()'s "%z" format by including a colon between hour and minute. */
85 static void format_zone (char *buffer, size_t buffer_size) /* {{{ */
91 minutes = timezone / 60;
93 sstrncpy (buffer, "Z", buffer_size);
100 minutes = minutes * (-1);
103 hours = minutes / 60;
104 minutes = minutes % 60;
106 ssnprintf (buffer, buffer_size, "%s%02ld:%02ld",
107 (east ? "+" : "-"), hours, minutes);
108 } /* }}} int format_zone */
110 static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */
112 struct timespec t_spec;
114 char base[20]; /* 2006-01-02T15:04:05 */
115 char nano[11]; /* .999999999 */
116 char zone[7]; /* +00:00 */
117 char *fields[] = {base, nano, zone};
120 CDTIME_T_TO_TIMESPEC (t, &t_spec);
121 NORMALIZE_TIMESPEC (t_spec);
123 if (localtime_r (&t_spec.tv_sec, &t_tm) == NULL) {
126 ERROR ("format_rfc3339: localtime_r failed: %s",
127 sstrerror (status, errbuf, sizeof (errbuf)));
131 len = strftime (base, sizeof (base), "%Y-%m-%dT%H:%M:%S", &t_tm);
136 ssnprintf (nano, sizeof (nano), ".%09ld", (long) t_spec.tv_nsec);
138 sstrncpy (nano, "", sizeof (nano));
140 format_zone (zone, sizeof (zone));
142 if (strjoin (buffer, buffer_size, fields, STATIC_ARRAY_SIZE (fields), "") < 0)
145 } /* }}} int cdtime_to_rfc3339nano */
147 int rfc3339 (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
149 if (buffer_size < RFC3339_SIZE)
152 return format_rfc3339 (buffer, buffer_size, t, 0);
153 } /* }}} size_t cdtime_to_rfc3339 */
155 int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
157 if (buffer_size < RFC3339NANO_SIZE)
160 return format_rfc3339 (buffer, buffer_size, t, 1);
161 } /* }}} size_t cdtime_to_rfc3339nano */
163 /* vim: set sw=2 sts=2 et fdm=marker : */