Added library link check and addressed review comments
[collectd.git] / src / daemon / utils_time.c
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  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_time.h"
32
33 #ifndef DEFAULT_MOCK_TIME
34 #define DEFAULT_MOCK_TIME 1542455354518929408ULL
35 #endif
36
37 #ifdef MOCK_TIME
38 cdtime_t cdtime_mock = (cdtime_t)MOCK_TIME;
39
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};
47
48   status = clock_gettime(CLOCK_REALTIME, &ts);
49   if (status != 0) {
50     ERROR("cdtime: clock_gettime failed: %s", STRERRNO);
51     return 0;
52   }
53
54   return TIMESPEC_TO_CDTIME_T(&ts);
55 } /* }}} cdtime_t cdtime */
56 #else /* !HAVE_CLOCK_GETTIME */
57 /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
58 cdtime_t cdtime(void) /* {{{ */
59 {
60   int status;
61   struct timeval tv = {0, 0};
62
63   status = gettimeofday(&tv, /* struct timezone = */ NULL);
64   if (status != 0) {
65     ERROR("cdtime: gettimeofday failed: %s", STRERRNO);
66     return 0;
67   }
68
69   return TIMEVAL_TO_CDTIME_T(&tv);
70 } /* }}} cdtime_t cdtime */
71 #endif
72 #endif
73
74 /**********************************************************************
75  Time retrieval functions
76 ***********************************************************************/
77
78 static int get_utc_time(cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
79 {
80   struct timespec t_spec = CDTIME_T_TO_TIMESPEC(t);
81   NORMALIZE_TIMESPEC(t_spec);
82
83   if (gmtime_r(&t_spec.tv_sec, t_tm) == NULL) {
84     int status = errno;
85     ERROR("get_utc_time: gmtime_r failed: %s", STRERRNO);
86     return status;
87   }
88
89   *nsec = t_spec.tv_nsec;
90   return 0;
91 } /* }}} int get_utc_time */
92
93 static int get_local_time(cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
94 {
95   struct timespec t_spec = CDTIME_T_TO_TIMESPEC(t);
96   NORMALIZE_TIMESPEC(t_spec);
97
98   if (localtime_r(&t_spec.tv_sec, t_tm) == NULL) {
99     int status = errno;
100     ERROR("get_local_time: localtime_r failed: %s", STRERRNO);
101     return status;
102   }
103
104   *nsec = t_spec.tv_nsec;
105   return 0;
106 } /* }}} int get_local_time */
107
108 /**********************************************************************
109  Formatting functions
110 ***********************************************************************/
111
112 static const char zulu_zone[] = "Z";
113
114 /* format_zone reads time zone information from "extern long timezone", exported
115  * by <time.h>, and formats it according to RFC 3339. This differs from
116  * strftime()'s "%z" format by including a colon between hour and minute. */
117 static int format_zone(char *buffer, size_t buffer_size,
118                        struct tm const *tm) /* {{{ */
119 {
120   char tmp[7];
121   size_t sz;
122
123   if ((buffer == NULL) || (buffer_size < 7))
124     return EINVAL;
125
126   sz = strftime(tmp, sizeof(tmp), "%z", tm);
127   if (sz == 0)
128     return ENOMEM;
129   if (sz != 5) {
130     DEBUG("format_zone: strftime(\"%%z\") = \"%s\", want \"+hhmm\"", tmp);
131     sstrncpy(buffer, tmp, buffer_size);
132     return 0;
133   }
134
135   buffer[0] = tmp[0];
136   buffer[1] = tmp[1];
137   buffer[2] = tmp[2];
138   buffer[3] = ':';
139   buffer[4] = tmp[3];
140   buffer[5] = tmp[4];
141   buffer[6] = 0;
142
143   return 0;
144 } /* }}} int format_zone */
145
146 int format_rfc3339(char *buffer, size_t buffer_size, struct tm const *t_tm,
147                    long nsec, _Bool print_nano, char const *zone) /* {{{ */
148 {
149   int len;
150   char *pos = buffer;
151   size_t size_left = buffer_size;
152
153   if ((len = strftime(pos, size_left, "%Y-%m-%dT%H:%M:%S", t_tm)) == 0)
154     return ENOMEM;
155   pos += len;
156   size_left -= len;
157
158   if (print_nano) {
159     if ((len = snprintf(pos, size_left, ".%09ld", nsec)) == 0)
160       return ENOMEM;
161     pos += len;
162     size_left -= len;
163   }
164
165   sstrncpy(pos, zone, size_left);
166   return 0;
167 } /* }}} int format_rfc3339 */
168
169 int format_rfc3339_utc(char *buffer, size_t buffer_size, cdtime_t t,
170                        _Bool print_nano) /* {{{ */
171 {
172   struct tm t_tm;
173   long nsec = 0;
174   int status;
175
176   if ((status = get_utc_time(t, &t_tm, &nsec)) != 0)
177     return status; /* The error should have already be reported. */
178
179   return format_rfc3339(buffer, buffer_size, &t_tm, nsec, print_nano,
180                         zulu_zone);
181 } /* }}} int format_rfc3339_utc */
182
183 int format_rfc3339_local(char *buffer, size_t buffer_size, cdtime_t t,
184                          _Bool print_nano) /* {{{ */
185 {
186   struct tm t_tm;
187   long nsec = 0;
188   int status;
189   char zone[7]; /* +00:00 */
190
191   if ((status = get_local_time(t, &t_tm, &nsec)) != 0)
192     return status; /* The error should have already be reported. */
193
194   if ((status = format_zone(zone, sizeof(zone), &t_tm)) != 0)
195     return status;
196
197   return format_rfc3339(buffer, buffer_size, &t_tm, nsec, print_nano, zone);
198 } /* }}} int format_rfc3339_local */
199
200 /**********************************************************************
201  Public functions
202 ***********************************************************************/
203
204 int rfc3339(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
205 {
206   if (buffer_size < RFC3339_SIZE)
207     return ENOMEM;
208
209   return format_rfc3339_utc(buffer, buffer_size, t, 0);
210 } /* }}} int rfc3339 */
211
212 int rfc3339nano(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
213 {
214   if (buffer_size < RFC3339NANO_SIZE)
215     return ENOMEM;
216
217   return format_rfc3339_utc(buffer, buffer_size, t, 1);
218 } /* }}} int rfc3339nano */
219
220 int rfc3339_local(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
221 {
222   if (buffer_size < RFC3339_SIZE)
223     return ENOMEM;
224
225   return format_rfc3339_local(buffer, buffer_size, t, 0);
226 } /* }}} int rfc3339 */
227
228 int rfc3339nano_local(char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
229 {
230   if (buffer_size < RFC3339NANO_SIZE)
231     return ENOMEM;
232
233   return format_rfc3339_local(buffer, buffer_size, t, 1);
234 } /* }}} int rfc3339nano */