X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fdaemon%2Futils_time.c;h=86476ea63b4cd95728cfd843a48b52d26859d979;hb=ee830b46965eac77e608d536c51d15062b9842bb;hp=f500e138b0a0226144c7e5311ae9ae1c7c324c32;hpb=349d1538fc57c320270c471c9a8c7e11d5aa3631;p=collectd.git diff --git a/src/daemon/utils_time.c b/src/daemon/utils_time.c index f500e138..86476ea6 100644 --- a/src/daemon/utils_time.c +++ b/src/daemon/utils_time.c @@ -1,6 +1,6 @@ /** * collectd - src/utils_time.c - * Copyright (C) 2010 Florian octo Forster + * Copyright (C) 2010-2015 Florian octo Forster * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -21,15 +21,28 @@ * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian octo Forster + * Florian octo Forster **/ #include "collectd.h" + #include "utils_time.h" #include "plugin.h" #include "common.h" -#if HAVE_CLOCK_GETTIME +#ifndef DEFAULT_MOCK_TIME +# define DEFAULT_MOCK_TIME 1542455354518929408ULL +#endif + +#ifdef MOCK_TIME +cdtime_t cdtime_mock = (cdtime_t) MOCK_TIME; + +cdtime_t cdtime (void) +{ + return cdtime_mock; +} +#else /* !MOCK_TIME */ +# if HAVE_CLOCK_GETTIME cdtime_t cdtime (void) /* {{{ */ { int status; @@ -46,7 +59,7 @@ cdtime_t cdtime (void) /* {{{ */ return (TIMESPEC_TO_CDTIME_T (&ts)); } /* }}} cdtime_t cdtime */ -#else +# else /* !HAVE_CLOCK_GETTIME */ /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */ cdtime_t cdtime (void) /* {{{ */ { @@ -64,34 +77,39 @@ cdtime_t cdtime (void) /* {{{ */ return (TIMEVAL_TO_CDTIME_T (&tv)); } /* }}} cdtime_t cdtime */ +# endif #endif /* format_zone reads time zone information from "extern long timezone", exported * by , and formats it according to RFC 3339. This differs from * strftime()'s "%z" format by including a colon between hour and minute. */ -static void format_zone (char *buffer, size_t buffer_size) /* {{{ */ +static int format_zone (char *buffer, size_t buffer_size, struct tm const *tm) /* {{{ */ { - _Bool east = 0; - long hours; - long minutes; - - minutes = timezone / 60; - if (minutes == 0) { - sstrncpy (buffer, "Z", buffer_size); - return; - } + char tmp[7]; + size_t sz; - if (minutes < 0) + if ((buffer == NULL) || (buffer_size < 7)) + return EINVAL; + + sz = strftime (tmp, sizeof (tmp), "%z", tm); + if (sz == 0) + return ENOMEM; + if (sz != 5) { - east = 1; - minutes = minutes * (-1); + DEBUG ("format_zone: strftime(\"%%z\") = \"%s\", want \"+hhmm\"", tmp); + sstrncpy (buffer, tmp, buffer_size); + return 0; } - hours = minutes / 60; - minutes = minutes % 60; + buffer[0] = tmp[0]; + buffer[1] = tmp[1]; + buffer[2] = tmp[2]; + buffer[3] = ':'; + buffer[4] = tmp[3]; + buffer[5] = tmp[4]; + buffer[6] = 0; - ssnprintf (buffer, buffer_size, "%s%02ld:%02ld", - (east ? "+" : "-"), hours, minutes); + return 0; } /* }}} int format_zone */ static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */ @@ -103,13 +121,14 @@ static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool p char zone[7]; /* +00:00 */ char *fields[] = {base, nano, zone}; size_t len; + int status; CDTIME_T_TO_TIMESPEC (t, &t_spec); NORMALIZE_TIMESPEC (t_spec); if (localtime_r (&t_spec.tv_sec, &t_tm) == NULL) { char errbuf[1024]; - int status = errno; + status = errno; ERROR ("format_rfc3339: localtime_r failed: %s", sstrerror (status, errbuf, sizeof (errbuf))); return (status); @@ -124,12 +143,14 @@ static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool p else sstrncpy (nano, "", sizeof (nano)); - format_zone (zone, sizeof (zone)); + status = format_zone (zone, sizeof (zone), &t_tm); + if (status != 0) + return status; if (strjoin (buffer, buffer_size, fields, STATIC_ARRAY_SIZE (fields), "") < 0) return ENOMEM; return 0; -} /* }}} int cdtime_to_rfc3339nano */ +} /* }}} int format_rfc3339 */ int rfc3339 (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */ {