1 /**
2 * collectd - src/utils_time.h
3 * Copyright (C) 2010 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <ff at octo.it>
20 **/
22 #include "collectd.h"
23 #include "utils_time.h"
24 #include "plugin.h"
25 #include "common.h"
27 #if HAVE_CLOCK_GETTIME
28 cdtime_t cdtime (void) /* {{{ */
29 {
30 int status;
31 struct timespec ts = { 0, 0 };
33 status = clock_gettime (CLOCK_REALTIME, &ts);
34 if (status != 0)
35 {
36 char errbuf[1024];
37 ERROR ("cdtime: clock_gettime failed: %s",
38 sstrerror (errno, errbuf, sizeof (errbuf)));
39 return (0);
40 }
42 return (TIMESPEC_TO_CDTIME_T (&ts));
43 } /* }}} cdtime_t cdtime */
44 #else
45 /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
46 cdtime_t cdtime (void) /* {{{ */
47 {
48 int status;
49 struct timeval tv = { 0, 0 };
51 status = gettimeofday (&tv, /* struct timezone = */ NULL);
52 if (status != 0)
53 {
54 char errbuf[1024];
55 ERROR ("cdtime: gettimeofday failed: %s",
56 sstrerror (errno, errbuf, sizeof (errbuf)));
57 return (0);
58 }
60 return (TIMEVAL_TO_CDTIME_T (&tv));
61 } /* }}} cdtime_t cdtime */
62 #endif
64 size_t cdtime_to_iso8601 (char *s, size_t max, cdtime_t t) /* {{{ */
65 {
66 struct timespec t_spec;
67 struct tm t_tm;
69 size_t len;
71 CDTIME_T_TO_TIMESPEC (t, &t_spec);
72 NORMALIZE_TIMESPEC (t_spec);
74 if (localtime_r ((time_t *)&t_spec.tv_sec, &t_tm) == NULL) {
75 char errbuf[1024];
76 ERROR ("cdtime_to_iso8601: localtime_r failed: %s",
77 sstrerror (errno, errbuf, sizeof (errbuf)));
78 return (0);
79 }
81 len = strftime (s, max, "%Y-%m-%dT%H:%M:%S", &t_tm);
82 if (len == 0)
83 return 0;
85 if (max - len > 2) {
86 int n = snprintf (s + len, max - len, ".%09i", (int)t_spec.tv_nsec);
87 len += (n < max - len) ? n : max - len;
88 }
90 if (max - len > 3) {
91 int n = strftime (s + len, max - len, "%z", &t_tm);
92 len += (n < max - len) ? n : max - len;
93 }
95 s[max - 1] = '\0';
96 return len;
97 } /* }}} size_t cdtime_to_iso8601 */
99 /* vim: set sw=2 sts=2 et fdm=marker : */