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 **/
27 #include "collectd.h"
29 #include "utils_time.h"
30 #include "plugin.h"
31 #include "common.h"
33 #ifndef DEFAULT_MOCK_TIME
34 # define DEFAULT_MOCK_TIME 1542455354518929408ULL
35 #endif
37 #ifdef MOCK_TIME
38 cdtime_t cdtime_mock = (cdtime_t) MOCK_TIME;
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 };
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 }
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 };
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 }
78 return (TIMEVAL_TO_CDTIME_T (&tv));
79 } /* }}} cdtime_t cdtime */
80 # endif
81 #endif
83 /* format_zone reads time zone information from "extern long timezone", exported
84 * by <time.h>, and formats it according to RFC 3339. This differs from
85 * strftime()'s "%z" format by including a colon between hour and minute. */
86 static int format_zone (char *buffer, size_t buffer_size, struct tm const *tm) /* {{{ */
87 {
88 char tmp[7];
89 size_t sz;
91 if ((buffer == NULL) || (buffer_size < 7))
92 return EINVAL;
94 sz = strftime (tmp, sizeof (tmp), "%z", tm);
95 if (sz == 0)
96 return ENOMEM;
97 if (sz != 5)
98 {
99 DEBUG ("format_zone: strftime(\"%%z\") = \"%s\", want \"+hhmm\"", tmp);
100 sstrncpy (buffer, tmp, buffer_size);
101 return 0;
102 }
104 buffer[0] = tmp[0];
105 buffer[1] = tmp[1];
106 buffer[2] = tmp[2];
107 buffer[3] = ':';
108 buffer[4] = tmp[3];
109 buffer[5] = tmp[4];
110 buffer[6] = 0;
112 return 0;
113 } /* }}} int format_zone */
115 static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano, _Bool zulu) /* {{{ */
116 {
117 struct timespec t_spec;
118 struct tm t_tm;
119 char base[20]; /* 2006-01-02T15:04:05 */
120 char nano[11]; /* .999999999 */
121 char zone[7]; /* +00:00 */
122 char *fields[] = {base, nano, zone};
123 size_t len;
124 int status;
126 CDTIME_T_TO_TIMESPEC (t, &t_spec);
127 NORMALIZE_TIMESPEC (t_spec);
129 if (zulu) {
130 if (gmtime_r (&t_spec.tv_sec, &t_tm) == NULL) {
131 char errbuf[1024];
132 status = errno;
133 ERROR ("format_rfc3339: gmtime_r failed: %s",
134 sstrerror (status, errbuf, sizeof (errbuf)));
135 return (status);
136 }
137 } else {
138 if (localtime_r (&t_spec.tv_sec, &t_tm) == NULL) {
139 char errbuf[1024];
140 status = errno;
141 ERROR ("format_rfc3339: localtime_r failed: %s",
142 sstrerror (status, errbuf, sizeof (errbuf)));
143 return (status);
144 }
145 }
147 len = strftime (base, sizeof (base), "%Y-%m-%dT%H:%M:%S", &t_tm);
148 if (len == 0)
149 return ENOMEM;
151 if (print_nano)
152 ssnprintf (nano, sizeof (nano), ".%09ld", (long) t_spec.tv_nsec);
153 else
154 sstrncpy (nano, "", sizeof (nano));
156 if (zulu) {
157 zone[0] = 'Z';
158 zone[1] = 0;
159 } else {
160 status = format_zone (zone, sizeof (zone), &t_tm);
161 if (status != 0)
162 return status;
163 }
165 if (strjoin (buffer, buffer_size, fields, STATIC_ARRAY_SIZE (fields), "") < 0)
166 return ENOMEM;
167 return 0;
168 } /* }}} int format_rfc3339 */
170 int rfc3339 (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
171 {
172 if (buffer_size < RFC3339_SIZE)
173 return ENOMEM;
175 return format_rfc3339 (buffer, buffer_size, t, 0, 0);
176 } /* }}} size_t cdtime_to_rfc3339 */
178 int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
179 {
180 if (buffer_size < RFC3339NANO_SIZE)
181 return ENOMEM;
183 return format_rfc3339 (buffer, buffer_size, t, 1, 0);
184 } /* }}} size_t cdtime_to_rfc3339nano */
186 int rfc3339_zulu (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
187 {
188 if (buffer_size < RFC3339_ZULU_SIZE)
189 return ENOMEM;
191 return format_rfc3339 (buffer, buffer_size, t, 0, 1);
192 } /* }}} size_t cdtime_to_rfc3339 */
194 int rfc3339nano_zulu (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
195 {
196 if (buffer_size < RFC3339NANO_ZULU_SIZE)
197 return ENOMEM;
199 return format_rfc3339 (buffer, buffer_size, t, 1, 1);
200 } /* }}} size_t cdtime_to_rfc3339nano */
202 /* vim: set sw=2 sts=2 et fdm=marker : */