summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: eb336ef)
raw | patch | inline | side by side (parent: eb336ef)
author | Igor Peshansky <igorpeshansky@github.com> | |
Thu, 15 Sep 2016 15:08:44 +0000 (11:08 -0400) | ||
committer | Igor Peshansky <igorpeshansky@github.com> | |
Thu, 15 Sep 2016 18:29:30 +0000 (14:29 -0400) |
- Repurpose rfc3339/rfc3339nano to use UTC.
- Add rfc3339_local/rfc3339nano_local for local time.
- Factor out common bits; saner helper functions.
- Update comments.
- Add rfc3339_local/rfc3339nano_local for local time.
- Factor out common bits; saner helper functions.
- Update comments.
src/daemon/utils_time.c | patch | blob | history | |
src/daemon/utils_time.h | patch | blob | history | |
src/postgresql.c | patch | blob | history |
index af51d037c48aef48d8114b8f7df4ca2023293ab7..36ee8f2ae60ebf682633426ceb0bf61be9b2c47d 100644 (file)
--- a/src/daemon/utils_time.c
+++ b/src/daemon/utils_time.c
char errbuf[1024];
ERROR ("cdtime: clock_gettime failed: %s",
sstrerror (errno, errbuf, sizeof (errbuf)));
- return (0);
+ return 0;
}
- return (TIMESPEC_TO_CDTIME_T (&ts));
+ return TIMESPEC_TO_CDTIME_T (&ts);
} /* }}} cdtime_t cdtime */
# else /* !HAVE_CLOCK_GETTIME */
/* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
char errbuf[1024];
ERROR ("cdtime: gettimeofday failed: %s",
sstrerror (errno, errbuf, sizeof (errbuf)));
- return (0);
+ return 0;
}
- return (TIMEVAL_TO_CDTIME_T (&tv));
+ return TIMEVAL_TO_CDTIME_T (&tv);
} /* }}} cdtime_t cdtime */
# endif
#endif
+/**********************************************************************
+ Time retrieval functions
+***********************************************************************/
+
+static int get_utc_time (cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
+{
+ struct timespec t_spec;
+ int status;
+
+ CDTIME_T_TO_TIMESPEC (t, &t_spec);
+ NORMALIZE_TIMESPEC (t_spec);
+
+ if (gmtime_r (&t_spec.tv_sec, t_tm) == NULL) {
+ char errbuf[1024];
+ status = errno;
+ ERROR ("get_utc_time: gmtime_r failed: %s",
+ sstrerror (status, errbuf, sizeof (errbuf)));
+ return status;
+ }
+
+ *nsec = t_spec.tv_nsec;
+ return 0;
+} /* }}} int get_utc_time */
+
+static int get_local_time (cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
+{
+ struct timespec t_spec;
+ 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];
+ status = errno;
+ ERROR ("get_local_time: localtime_r failed: %s",
+ sstrerror (status, errbuf, sizeof (errbuf)));
+ return status;
+ }
+
+ *nsec = t_spec.tv_nsec;
+ return 0;
+} /* }}} int get_local_time */
+
+/**********************************************************************
+ Formatting functions
+***********************************************************************/
+
+static const char utc_zone[] = "+00:00";
+static const char zulu_zone[] = "Z";
+
/* 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 int format_zone (char *buffer, size_t buffer_size, struct tm const *tm) /* {{{ */
+static int format_zone (char *buffer, size_t buffer_size) /* {{{ */
{
+ struct tm t_tm = { 0 }; /* The value doesn't matter. */
char tmp[7];
size_t sz;
if ((buffer == NULL) || (buffer_size < 7))
return EINVAL;
- sz = strftime (tmp, sizeof (tmp), "%z", tm);
+ sz = strftime (tmp, sizeof (tmp), "%z", &t_tm);
if (sz == 0)
return ENOMEM;
if (sz != 5)
@@ -112,91 +164,106 @@ static int format_zone (char *buffer, size_t buffer_size, struct tm const *tm) /
return 0;
} /* }}} int format_zone */
-static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano, _Bool zulu) /* {{{ */
+int format_rfc3339 (char *buffer, size_t buffer_size, struct tm const *t_tm, long nsec, _Bool print_nano, char const *zone) /* {{{ */
{
- 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;
- int status;
-
- CDTIME_T_TO_TIMESPEC (t, &t_spec);
- NORMALIZE_TIMESPEC (t_spec);
+ int len;
+ char *pos = buffer;
+ size_t size_left = buffer_size;
- if (zulu) {
- if (gmtime_r (&t_spec.tv_sec, &t_tm) == NULL) {
- char errbuf[1024];
- status = errno;
- ERROR ("format_rfc3339: gmtime_r failed: %s",
- sstrerror (status, errbuf, sizeof (errbuf)));
- return (status);
- }
- } else {
- if (localtime_r (&t_spec.tv_sec, &t_tm) == NULL) {
- char errbuf[1024];
- status = errno;
- ERROR ("format_rfc3339: localtime_r failed: %s",
- sstrerror (status, errbuf, sizeof (errbuf)));
- return (status);
- }
- }
-
- len = strftime (base, sizeof (base), "%Y-%m-%dT%H:%M:%S", &t_tm);
- if (len == 0)
+ if ((len = strftime (pos, size_left, "%Y-%m-%dT%H:%M:%S", t_tm)) == 0)
return ENOMEM;
+ pos += len;
+ size_left -= len;
- if (print_nano)
- ssnprintf (nano, sizeof (nano), ".%09ld", (long) t_spec.tv_nsec);
- else
- sstrncpy (nano, "", sizeof (nano));
-
- if (zulu) {
- zone[0] = 'Z';
- zone[1] = 0;
- } else {
- status = format_zone (zone, sizeof (zone), &t_tm);
- if (status != 0)
- return status;
+ if (print_nano) {
+ if ((len = ssnprintf (pos, size_left, ".%09ld", nsec)) == 0)
+ return ENOMEM;
+ pos += len;
+ size_left -= len;
}
- if (strjoin (buffer, buffer_size, fields, STATIC_ARRAY_SIZE (fields), "") < 0)
- return ENOMEM;
+ sstrncpy (buffer, zone, buffer_size);
return 0;
} /* }}} int format_rfc3339 */
+int format_rfc3339_utc (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano, char const *zone) /* {{{ */
+{
+ struct tm t_tm;
+ long nsec;
+ int status;
+
+ if ((status = get_utc_time (t, &t_tm, &nsec)) != 0)
+ return status; /* The error should have already be reported. */
+
+ return format_rfc3339 (buffer, buffer_size, &t_tm, nsec, print_nano, zone);
+} /* }}} int format_rfc3339_utc */
+
+int format_rfc3339_local (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */
+{
+ struct tm t_tm;
+ long nsec;
+ int status;
+ char zone[7]; /* +00:00 */
+
+ if ((status = get_local_time (t, &t_tm, &nsec)) != 0)
+ return status; /* The error should have already be reported. */
+
+ if ((status = format_zone (zone, sizeof (zone))) != 0)
+ return status;
+
+ return format_rfc3339 (buffer, buffer_size, &t_tm, nsec, print_nano, zone);
+} /* }}} int format_rfc3339_local */
+
+/**********************************************************************
+ Public functions
+***********************************************************************/
+
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, 0);
-} /* }}} size_t cdtime_to_rfc3339 */
+ return format_rfc3339_utc (buffer, buffer_size, t, 0, utc_zone);
+} /* }}} int rfc3339 */
int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
{
if (buffer_size < RFC3339NANO_SIZE)
return ENOMEM;
- return format_rfc3339 (buffer, buffer_size, t, 1, 0);
-} /* }}} size_t cdtime_to_rfc3339nano */
+ return format_rfc3339_utc (buffer, buffer_size, t, 1, utc_zone);
+} /* }}} int rfc3339nano */
int rfc3339_zulu (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
{
if (buffer_size < RFC3339_ZULU_SIZE)
return ENOMEM;
- return format_rfc3339 (buffer, buffer_size, t, 0, 1);
-} /* }}} size_t cdtime_to_rfc3339 */
+ return format_rfc3339_utc (buffer, buffer_size, t, 0, zulu_zone);
+} /* }}} int rfc3339_zulu */
int rfc3339nano_zulu (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
{
if (buffer_size < RFC3339NANO_ZULU_SIZE)
return ENOMEM;
- return format_rfc3339 (buffer, buffer_size, t, 1, 1);
-} /* }}} size_t cdtime_to_rfc3339nano */
+ return format_rfc3339_utc (buffer, buffer_size, t, 1, zulu_zone);
+} /* }}} int rfc3339nano_zulu */
+
+int rfc3339_local (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
+{
+ if (buffer_size < RFC3339_SIZE)
+ return ENOMEM;
+
+ return format_rfc3339_local (buffer, buffer_size, t, 0);
+} /* }}} int rfc3339 */
+
+int rfc3339nano_local (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
+{
+ if (buffer_size < RFC3339NANO_SIZE)
+ return ENOMEM;
+
+ return format_rfc3339_local (buffer, buffer_size, t, 1);
+} /* }}} int rfc3339nano */
/* vim: set sw=2 sts=2 et fdm=marker : */
index 29db9d2444f1cf2a3beb5b6e8f1d19cf8a316db4..5456dcc142053a3a619afcecc79171dc576dd7a5 100644 (file)
--- a/src/daemon/utils_time.h
+++ b/src/daemon/utils_time.h
cdtime_t cdtime (void);
-#define RFC3339_SIZE 26
-#define RFC3339NANO_SIZE 36
+#define RFC3339_SIZE 26 /* 2006-01-02T15:04:05+00:00 */
+#define RFC3339NANO_SIZE 36 /* 2006-01-02T15:04:05.999999999+00:00 */
-#define RFC3339_ZULU_SIZE 21
-#define RFC3339NANO_ZULU_SIZE 31
+#define RFC3339_ZULU_SIZE 21 /* 2006-01-02T15:04:05Z */
+#define RFC3339NANO_ZULU_SIZE 31 /* 2006-01-02T15:04:05.999999999Z */
-/* rfc3339 formats a cdtime_t time in RFC 3339 format with second precision. */
+/* rfc3339 formats a cdtime_t time as UTC 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. */
+/* rfc3339nano formats a cdtime_t as UTC time in RFC 3339 format with
+ * nanosecond precision. */
int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t);
-/* rfc3339 formats a cdtime_t time in RFC 3339 zulu format with second
+/* rfc3339 formats a cdtime_t time as UTC in RFC 3339 zulu format with second
* precision. */
int rfc3339_zulu (char *buffer, size_t buffer_size, cdtime_t t);
-/* rfc3339nano formats a cdtime_t time in RFC 3339 zulu format with nanosecond
- * precision. */
+/* rfc3339nano formats a cdtime_t time as UTC in RFC 3339 zulu format with
+ * nanosecond precision. */
int rfc3339nano_zulu (char *buffer, size_t buffer_size, cdtime_t t);
+/* rfc3339 formats a cdtime_t time as local in RFC 3339 format with second
+ * precision. */
+int rfc3339_local (char *buffer, size_t buffer_size, cdtime_t t);
+
+/* rfc3339nano formats a cdtime_t time as local in RFC 3339 format with
+ * nanosecond precision. */
+int rfc3339nano_local (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 084eae453393a3bb86c0766966d2e45b5a514e5f..c92f6ee148a149a4c67aafd39ba862a8155784c6 100644 (file)
--- a/src/postgresql.c
+++ b/src/postgresql.c
assert (db->database != NULL);
assert (db->writers != NULL);
+ /* TODO: Should this be rfc3339nano_local()? */
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;