}
}
-/* Writes the current time to 'string' based on 'template'.
- * The current time is either local time or UTC based on 'utc'. */
+/* Writes time 'when' to 'string' based on 'template', in local time or UTC
+ * based on 'utc'. */
void
-ds_put_strftime(struct ds *ds, const char *template, bool utc)
+ds_put_strftime(struct ds *ds, const char *template, time_t when, bool utc)
{
struct tm tm;
- time_t now = time_wall();
if (utc) {
- gmtime_r(&now, &tm);
+ gmtime_r(&when, &tm);
} else {
- localtime_r(&now, &tm);
+ localtime_r(&when, &tm);
}
for (;;) {
}
}
+/* Returns a malloc()'d string for time 'when' based on 'template', in local
+ * time or UTC based on 'utc'. */
+char *
+xastrftime(const char *template, time_t when, bool utc)
+{
+ struct ds s;
+
+ ds_init(&s);
+ ds_put_strftime(&s, template, when, utc);
+ return s.string;
+}
+
int
ds_get_line(struct ds *ds, FILE *file)
{
/*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
+#include <time.h>
#include "compiler.h"
-struct tm;
-
/* A "dynamic string", that is, a buffer that can be used to construct a
* string across a series of operations that extend or modify it.
*
void ds_put_format_valist(struct ds *, const char *, va_list)
PRINTF_FORMAT(2, 0);
void ds_put_printable(struct ds *, const char *, size_t);
-void ds_put_strftime(struct ds *, const char *, bool utc)
- STRFTIME_FORMAT(2);
void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
uintptr_t ofs, bool ascii);
int ds_get_line(struct ds *, FILE *);
int ds_get_preprocessed_line(struct ds *, FILE *);
int ds_get_test_line(struct ds *, FILE *);
+void ds_put_strftime(struct ds *, const char *template, time_t when, bool utc)
+ STRFTIME_FORMAT(2);
+char *xastrftime(const char *template, time_t when, bool utc)
+ STRFTIME_FORMAT(1);
+
char *ds_cstr(struct ds *);
const char *ds_cstr_ro(const struct ds *);
char *ds_steal_cstr(struct ds *);
ds_clear(line);
}
-static void
-table_format_timestamp__(char *s, size_t size)
+static char *
+table_format_timestamp__(void)
{
- time_t now = time_wall();
- struct tm tm;
- strftime(s, size, "%Y-%m-%d %H:%M:%S", gmtime_r(&now, &tm));
+ return xastrftime("%Y-%m-%d %H:%M:%S", time_wall(), true);
}
static void
table_print_timestamp__(const struct table *table)
{
if (table->timestamp) {
- char s[32];
-
- table_format_timestamp__(s, sizeof s);
+ char *s = table_format_timestamp__();
puts(s);
+ free(s);
}
}
json_object_put_string(json, "caption", table->caption);
}
if (table->timestamp) {
- char s[32];
-
- table_format_timestamp__(s, sizeof s);
+ char *s = table_format_timestamp__();
json_object_put_string(json, "time", s);
+ free(s);
}
headings = json_array_create_empty();
now = time_wall();
if (now < 0) {
- struct tm tm;
- char s[128];
-
- gmtime_r(&now, &tm);
- strftime(s, sizeof s, "%a, %d %b %Y %H:%M:%S", &tm);
+ char *s = xastrftime("%a, %d %b %Y %H:%M:%S", now, true);
VLOG_ERR("current time is negative: %s (%ld)", s, (long int) now);
+ free(s);
}
unixctl_command_register(
break;
case 'd':
p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp);
- ds_put_strftime(s, tmp, false);
+ ds_put_strftime(s, tmp, time_wall(), false);
break;
case 'D':
p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp);
- ds_put_strftime(s, tmp, true);
+ ds_put_strftime(s, tmp, time_wall(), true);
break;
case 'm':
/* Format user-supplied log message and trim trailing new-lines. */
date = shash_find_data(json_object(json), "_date");
if (date && date->type == JSON_INTEGER) {
time_t t = json_integer(date);
- struct tm tm;
- char s[128];
-
- strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", gmtime_r(&t, &tm));
- printf(" %s", s);
+ char *s = xastrftime(" %Y-%m-%d %H:%M:%S", t, true);
+ fputs(s, stdout);
+ free(s);
}
comment = shash_find_data(json_object(json), "_comment");
run(retval, "vconn_recv");
if (timestamp) {
- time_t now = time_wall();
- struct tm tm;
- char s[32];
-
- strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ",
- gmtime_r(&now, &tm));
+ char *s = xastrftime("%Y-%m-%d %H:%M:%S: ", time_wall(), true);
fputs(s, stderr);
+ free(s);
}
ofptype_decode(&type, b->data);