f500e138b0a0226144c7e5311ae9ae1c7c324c32
1 /**
2 * collectd - src/utils_time.c
3 * Copyright (C) 2010 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 <ff at octo.it>
25 **/
27 #include "collectd.h"
28 #include "utils_time.h"
29 #include "plugin.h"
30 #include "common.h"
32 #if HAVE_CLOCK_GETTIME
33 cdtime_t cdtime (void) /* {{{ */
34 {
35 int status;
36 struct timespec ts = { 0, 0 };
38 status = clock_gettime (CLOCK_REALTIME, &ts);
39 if (status != 0)
40 {
41 char errbuf[1024];
42 ERROR ("cdtime: clock_gettime failed: %s",
43 sstrerror (errno, errbuf, sizeof (errbuf)));
44 return (0);
45 }
47 return (TIMESPEC_TO_CDTIME_T (&ts));
48 } /* }}} cdtime_t cdtime */
49 #else
50 /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
51 cdtime_t cdtime (void) /* {{{ */
52 {
53 int status;
54 struct timeval tv = { 0, 0 };
56 status = gettimeofday (&tv, /* struct timezone = */ NULL);
57 if (status != 0)
58 {
59 char errbuf[1024];
60 ERROR ("cdtime: gettimeofday failed: %s",
61 sstrerror (errno, errbuf, sizeof (errbuf)));
62 return (0);
63 }
65 return (TIMEVAL_TO_CDTIME_T (&tv));
66 } /* }}} cdtime_t cdtime */
67 #endif
69 /* format_zone reads time zone information from "extern long timezone", exported
70 * by <time.h>, and formats it according to RFC 3339. This differs from
71 * strftime()'s "%z" format by including a colon between hour and minute. */
72 static void format_zone (char *buffer, size_t buffer_size) /* {{{ */
73 {
74 _Bool east = 0;
75 long hours;
76 long minutes;
78 minutes = timezone / 60;
79 if (minutes == 0) {
80 sstrncpy (buffer, "Z", buffer_size);
81 return;
82 }
84 if (minutes < 0)
85 {
86 east = 1;
87 minutes = minutes * (-1);
88 }
90 hours = minutes / 60;
91 minutes = minutes % 60;
93 ssnprintf (buffer, buffer_size, "%s%02ld:%02ld",
94 (east ? "+" : "-"), hours, minutes);
95 } /* }}} int format_zone */
97 static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */
98 {
99 struct timespec t_spec;
100 struct tm t_tm;
101 char base[20]; /* 2006-01-02T15:04:05 */
102 char nano[11]; /* .999999999 */
103 char zone[7]; /* +00:00 */
104 char *fields[] = {base, nano, zone};
105 size_t len;
107 CDTIME_T_TO_TIMESPEC (t, &t_spec);
108 NORMALIZE_TIMESPEC (t_spec);
110 if (localtime_r (&t_spec.tv_sec, &t_tm) == NULL) {
111 char errbuf[1024];
112 int status = errno;
113 ERROR ("format_rfc3339: localtime_r failed: %s",
114 sstrerror (status, errbuf, sizeof (errbuf)));
115 return (status);
116 }
118 len = strftime (base, sizeof (base), "%Y-%m-%dT%H:%M:%S", &t_tm);
119 if (len == 0)
120 return ENOMEM;
122 if (print_nano)
123 ssnprintf (nano, sizeof (nano), ".%09ld", (long) t_spec.tv_nsec);
124 else
125 sstrncpy (nano, "", sizeof (nano));
127 format_zone (zone, sizeof (zone));
129 if (strjoin (buffer, buffer_size, fields, STATIC_ARRAY_SIZE (fields), "") < 0)
130 return ENOMEM;
131 return 0;
132 } /* }}} int cdtime_to_rfc3339nano */
134 int rfc3339 (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
135 {
136 if (buffer_size < RFC3339_SIZE)
137 return ENOMEM;
139 return format_rfc3339 (buffer, buffer_size, t, 0);
140 } /* }}} size_t cdtime_to_rfc3339 */
142 int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
143 {
144 if (buffer_size < RFC3339NANO_SIZE)
145 return ENOMEM;
147 return format_rfc3339 (buffer, buffer_size, t, 1);
148 } /* }}} size_t cdtime_to_rfc3339nano */
150 /* vim: set sw=2 sts=2 et fdm=marker : */