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 "common.h"
30 #include "plugin.h"
31 #include "utils_time.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) { return cdtime_mock; }
41 #else /* !MOCK_TIME */
42 #if HAVE_CLOCK_GETTIME
43 cdtime_t cdtime(void) /* {{{ */
44 {
45 int status;
46 struct timespec ts = {0, 0};
48 status = clock_gettime(CLOCK_REALTIME, &ts);
49 if (status != 0) {
50 char errbuf[1024];
51 ERROR("cdtime: clock_gettime failed: %s",
52 sstrerror(errno, errbuf, sizeof(errbuf)));
53 return (0);
54 }
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) /* {{{ */
61 {
62 int status;
63 struct timeval tv = {0, 0};
65 status = gettimeofday(&tv, /* struct timezone = */ NULL);
66 if (status != 0) {
67 char errbuf[1024];
68 ERROR("cdtime: gettimeofday failed: %s",
69 sstrerror(errno, errbuf, sizeof(errbuf)));
70 return (0);
71 }
73 return (TIMEVAL_TO_CDTIME_T(&tv));
74 } /* }}} cdtime_t cdtime */
75 #endif
76 #endif
78 /* format_zone reads time zone information from "extern long timezone", exported
79 * by <time.h>, and formats it according to RFC 3339. This differs from
80 * strftime()'s "%z" format by including a colon between hour and minute. */
81 static int format_zone(char *buffer, size_t buffer_size,
82 struct tm const *tm) /* {{{ */
83 {
84 char tmp[7];
85 size_t sz;
87 if ((buffer == NULL) || (buffer_size < 7))
88 return EINVAL;
90 sz = strftime(tmp, sizeof(tmp), "%z", tm);
91 if (sz == 0)
92 return ENOMEM;
93 if (sz != 5) {
94 DEBUG("format_zone: strftime(\"%%z\") = \"%s\", want \"+hhmm\"", tmp);
95 sstrncpy(buffer, tmp, buffer_size);
96 return 0;
97 }
99 buffer[0] = tmp[0];
100 buffer[1] = tmp[1];
101 buffer[2] = tmp[2];
102 buffer[3] = ':';
103 buffer[4] = tmp[3];
104 buffer[5] = tmp[4];
105 buffer[6] = 0;
107 return 0;
108 } /* }}} int format_zone */
110 static int format_rfc3339(char *buffer, size_t buffer_size, cdtime_t t,
111 _Bool print_nano) /* {{{ */
112 {
113 struct timespec t_spec;
114 struct tm t_tm;
115 char base[20]; /* 2006-01-02T15:04:05 */
116 char nano[11]; /* .999999999 */
117 char zone[7]; /* +00:00 */
118 char *fields[] = {base, nano, zone};
119 size_t len;
120 int status;
122 CDTIME_T_TO_TIMESPEC(t, &t_spec);
123 NORMALIZE_TIMESPEC(t_spec);
125 if (localtime_r(&t_spec.tv_sec, &t_tm) == NULL) {
126 char errbuf[1024];
127 status = errno;
128 ERROR("format_rfc3339: localtime_r failed: %s",
129 sstrerror(status, errbuf, sizeof(errbuf)));
130 return (status);
131 }
133 len = strftime(base, sizeof(base), "%Y-%m-%dT%H:%M:%S", &t_tm);
134 if (len == 0)
135 return ENOMEM;
137 if (print_nano)
138 ssnprintf(nano, sizeof(nano), ".%09ld", (long)t_spec.tv_nsec);
139 else
140 sstrncpy(nano, "", sizeof(nano));
142 status = format_zone(zone, sizeof(zone), &t_tm);
143 if (status != 0)
144 return status;
146 if (strjoin(buffer, buffer_size, fields, STATIC_ARRAY_SIZE(fields), "") < 0)
147 return ENOMEM;
148 return 0;
149 } /* }}} int format_rfc3339 */
151 int rfc3339(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
152 {
153 if (buffer_size < RFC3339_SIZE)
154 return ENOMEM;
156 return format_rfc3339(buffer, buffer_size, t, 0);
157 } /* }}} size_t cdtime_to_rfc3339 */
159 int rfc3339nano(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
160 {
161 if (buffer_size < RFC3339NANO_SIZE)
162 return ENOMEM;
164 return format_rfc3339(buffer, buffer_size, t, 1);
165 } /* }}} size_t cdtime_to_rfc3339nano */
167 /* vim: set sw=2 sts=2 et fdm=marker : */