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"
28 #include "utils_time.h"
29 #include "plugin.h"
30 #include "common.h"
32 #ifndef DEFAULT_MOCK_TIME
33 # define DEFAULT_MOCK_TIME 1542455354518929408ULL
34 #endif
36 #ifdef MOCK_TIME
37 cdtime_t cdtime_mock = (cdtime_t) MOCK_TIME;
39 cdtime_t cdtime (void)
40 {
41 return cdtime_mock;
42 }
43 #else /* !MOCK_TIME */
44 # if HAVE_CLOCK_GETTIME
45 cdtime_t cdtime (void) /* {{{ */
46 {
47 int status;
48 struct timespec ts = { 0, 0 };
50 status = clock_gettime (CLOCK_REALTIME, &ts);
51 if (status != 0)
52 {
53 char errbuf[1024];
54 ERROR ("cdtime: clock_gettime failed: %s",
55 sstrerror (errno, errbuf, sizeof (errbuf)));
56 return (0);
57 }
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) /* {{{ */
64 {
65 int status;
66 struct timeval tv = { 0, 0 };
68 status = gettimeofday (&tv, /* struct timezone = */ NULL);
69 if (status != 0)
70 {
71 char errbuf[1024];
72 ERROR ("cdtime: gettimeofday failed: %s",
73 sstrerror (errno, errbuf, sizeof (errbuf)));
74 return (0);
75 }
77 return (TIMEVAL_TO_CDTIME_T (&tv));
78 } /* }}} cdtime_t cdtime */
79 # endif
80 #endif
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 int format_zone (char *buffer, size_t buffer_size, struct tm const *tm) /* {{{ */
86 {
87 char tmp[7];
88 size_t sz;
90 if ((buffer == NULL) || (buffer_size < 7))
91 return EINVAL;
93 sz = strftime (tmp, sizeof (tmp), "%z", tm);
94 if (sz == 0)
95 return ENOMEM;
96 if (sz != 5)
97 {
98 DEBUG ("format_zone: strftime(\"%%z\") = \"%s\", want \"+hhmm\"", tmp);
99 sstrncpy (buffer, tmp, buffer_size);
100 return 0;
101 }
103 buffer[0] = tmp[0];
104 buffer[1] = tmp[1];
105 buffer[2] = tmp[2];
106 buffer[3] = ':';
107 buffer[4] = tmp[3];
108 buffer[5] = tmp[4];
109 buffer[6] = 0;
111 return 0;
112 } /* }}} int format_zone */
114 static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */
115 {
116 struct timespec t_spec;
117 struct tm t_tm;
118 char base[20]; /* 2006-01-02T15:04:05 */
119 char nano[11]; /* .999999999 */
120 char zone[7]; /* +00:00 */
121 char *fields[] = {base, nano, zone};
122 size_t len;
123 int status;
125 CDTIME_T_TO_TIMESPEC (t, &t_spec);
126 NORMALIZE_TIMESPEC (t_spec);
128 if (localtime_r (&t_spec.tv_sec, &t_tm) == NULL) {
129 char errbuf[1024];
130 int status = errno;
131 ERROR ("format_rfc3339: localtime_r failed: %s",
132 sstrerror (status, errbuf, sizeof (errbuf)));
133 return (status);
134 }
136 len = strftime (base, sizeof (base), "%Y-%m-%dT%H:%M:%S", &t_tm);
137 if (len == 0)
138 return ENOMEM;
140 if (print_nano)
141 ssnprintf (nano, sizeof (nano), ".%09ld", (long) t_spec.tv_nsec);
142 else
143 sstrncpy (nano, "", sizeof (nano));
145 status = format_zone (zone, sizeof (zone), &t_tm);
146 if (status != 0)
147 return status;
149 if (strjoin (buffer, buffer_size, fields, STATIC_ARRAY_SIZE (fields), "") < 0)
150 return ENOMEM;
151 return 0;
152 } /* }}} int format_rfc3339 */
154 int rfc3339 (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
155 {
156 if (buffer_size < RFC3339_SIZE)
157 return ENOMEM;
159 return format_rfc3339 (buffer, buffer_size, t, 0);
160 } /* }}} size_t cdtime_to_rfc3339 */
162 int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
163 {
164 if (buffer_size < RFC3339NANO_SIZE)
165 return ENOMEM;
167 return format_rfc3339 (buffer, buffer_size, t, 1);
168 } /* }}} size_t cdtime_to_rfc3339nano */
170 /* vim: set sw=2 sts=2 et fdm=marker : */