summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ef07224)
raw | patch | inline | side by side (parent: ef07224)
author | Florian Forster <octo@collectd.org> | |
Tue, 24 Nov 2015 12:43:58 +0000 (13:43 +0100) | ||
committer | Florian Forster <octo@collectd.org> | |
Wed, 2 Dec 2015 09:21:06 +0000 (10:21 +0100) |
RFC 3339 is a "profile" of ISO 8601, i.e. (almost) all valid RFC 3339
strings are also valid ISO 8601 strings.
strings are also valid ISO 8601 strings.
src/daemon/utils_time.c | patch | blob | history | |
src/daemon/utils_time.h | patch | blob | history | |
src/postgresql.c | patch | blob | history |
index 5d905d9b7c2dbe9fad9fcf6e45ad027a0e56947b..f500e138b0a0226144c7e5311ae9ae1c7c324c32 100644 (file)
--- a/src/daemon/utils_time.c
+++ b/src/daemon/utils_time.c
} /* }}} cdtime_t cdtime */
#endif
-size_t cdtime_to_iso8601 (char *s, size_t max, cdtime_t t) /* {{{ */
+/* format_zone reads time zone information from "extern long timezone", exported
+ * by <time.h>, 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) /* {{{ */
+{
+ _Bool east = 0;
+ long hours;
+ long minutes;
+
+ minutes = timezone / 60;
+ if (minutes == 0) {
+ sstrncpy (buffer, "Z", buffer_size);
+ return;
+ }
+
+ if (minutes < 0)
+ {
+ east = 1;
+ minutes = minutes * (-1);
+ }
+
+ hours = minutes / 60;
+ minutes = minutes % 60;
+
+ ssnprintf (buffer, buffer_size, "%s%02ld:%02ld",
+ (east ? "+" : "-"), hours, minutes);
+} /* }}} int format_zone */
+
+static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */
{
struct timespec t_spec;
struct tm t_tm;
-
+ char base[20]; /* 2006-01-02T15:04:05 */
+ char nano[11]; /* .999999999 */
+ char zone[7]; /* +00:00 */
+ char *fields[] = {base, nano, zone};
size_t len;
CDTIME_T_TO_TIMESPEC (t, &t_spec);
NORMALIZE_TIMESPEC (t_spec);
- if (localtime_r ((time_t *)&t_spec.tv_sec, &t_tm) == NULL) {
+ if (localtime_r (&t_spec.tv_sec, &t_tm) == NULL) {
char errbuf[1024];
- ERROR ("cdtime_to_iso8601: localtime_r failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
- return (0);
+ int status = errno;
+ ERROR ("format_rfc3339: localtime_r failed: %s",
+ sstrerror (status, errbuf, sizeof (errbuf)));
+ return (status);
}
- len = strftime (s, max, "%Y-%m-%dT%H:%M:%S", &t_tm);
+ len = strftime (base, sizeof (base), "%Y-%m-%dT%H:%M:%S", &t_tm);
if (len == 0)
- return 0;
+ return ENOMEM;
- if (max - len > 2) {
- int n = snprintf (s + len, max - len, ".%09i", (int)t_spec.tv_nsec);
- len += (n < 0) ? 0
- : (((size_t) n) < (max - len)) ? ((size_t) n)
- : (max - len);
- }
+ if (print_nano)
+ ssnprintf (nano, sizeof (nano), ".%09ld", (long) t_spec.tv_nsec);
+ else
+ sstrncpy (nano, "", sizeof (nano));
- if (max - len > 3) {
- size_t n = strftime (s + len, max - len, "%z", &t_tm);
- len += (n < max - len) ? n : max - len;
- }
+ format_zone (zone, sizeof (zone));
+
+ if (strjoin (buffer, buffer_size, fields, STATIC_ARRAY_SIZE (fields), "") < 0)
+ return ENOMEM;
+ return 0;
+} /* }}} int cdtime_to_rfc3339nano */
+
+int rfc3339 (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
+{
+ if (buffer_size < RFC3339_SIZE)
+ return ENOMEM;
+
+ return format_rfc3339 (buffer, buffer_size, t, 0);
+} /* }}} size_t cdtime_to_rfc3339 */
+
+int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
+{
+ if (buffer_size < RFC3339NANO_SIZE)
+ return ENOMEM;
- s[max - 1] = '\0';
- return len;
-} /* }}} size_t cdtime_to_iso8601 */
+ return format_rfc3339 (buffer, buffer_size, t, 1);
+} /* }}} size_t cdtime_to_rfc3339nano */
/* vim: set sw=2 sts=2 et fdm=marker : */
index 6566a739bdf8be34b695a3d715dd90b7644e2f23..07e560afd373a8978c56b2691313c308d394355d 100644 (file)
--- a/src/daemon/utils_time.h
+++ b/src/daemon/utils_time.h
cdtime_t cdtime (void);
-/* format a cdtime_t value in ISO 8601 format:
- * returns the number of characters written to the string (not including the
- * terminating null byte or 0 on error; the function ensures that the string
- * is null terminated */
-size_t cdtime_to_iso8601 (char *s, size_t max, cdtime_t t);
+#define RFC3339_SIZE 26
+#define RFC3339NANO_SIZE 36
+
+/* rfc3339 formats a cdtime_t time in RFC 3339 format with second precision. */
+int rfc3339 (char *buffer, size_t buffer_size, cdtime_t t);
+
+/* rfc3339nano formats a cdtime_t time in RFC 3339 format with nanosecond
+ * precision. */
+int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t);
#endif /* UTILS_TIME_H */
/* vim: set sw=2 sts=2 et : */
diff --git a/src/postgresql.c b/src/postgresql.c
index bef3490ff0f16852d345c85bf8c167f851116597..e68fd3219aad4f439222114d5f8166e27fa06a51 100644 (file)
--- a/src/postgresql.c
+++ b/src/postgresql.c
{
c_psql_database_t *db;
- char time_str[32];
+ char time_str[RFC3339NANO_SIZE];
char values_name_str[1024];
char values_type_str[1024];
char values_str[1024];
assert (db->database != NULL);
assert (db->writers != NULL);
- if (cdtime_to_iso8601 (time_str, sizeof (time_str), vl->time) == 0) {
- log_err ("c_psql_write: Failed to convert time to ISO 8601 format");
+ if (rfc3339nano (time_str, sizeof (time_str), vl->time) != 0) {
+ log_err ("c_psql_write: Failed to convert time to RFC 3339 format");
return -1;
}