output: unify time stamp creations

Use iso time stamp in both mustach and html outputs.  Effectively this is a
removal of libc langinfo D_T_FMT format, that pulled a lot of gnulib stuff
to project almost unnecessarily.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2017-11-14 11:00:29 +00:00
parent ff3d9523e6
commit 6d737a7607
No known key found for this signature in database
GPG key ID: A9553245FDE9B739
6 changed files with 36 additions and 67 deletions

View file

@ -277,6 +277,7 @@ extern int parse_color_mode(const char *restrict optarg);
extern double strtod_or_err(const char *restrict str, const char *restrict errmesg);
extern void __attribute__ ((noreturn)) print_version(void);
extern void __attribute__ ((noreturn)) usage(int status);
extern void dp_time_tool(FILE *file, const char *path, int epoch);
extern int (*parse_ipaddr) (struct conf_t *state, const char *restrict src,
union ipaddr_t *restrict dst);

View file

@ -45,14 +45,12 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "close-stream.h"
#include "dhcpd-pools.h"
#include "error.h"
#include "mustach.h"
#include "strftime.h"
#include "xalloc.h"
/*! \struct expl
@ -69,35 +67,6 @@ struct expl {
static int must_enter(void *closure, const char *name);
static int must_leave(void *closure);
static void dp_time_tool(FILE *file, const char *path, int epoch)
{
time_t t;
/* a file or now */
if (path) {
struct stat st;
stat(path, &st);
t = st.st_mtime;
} else
t = time(NULL);
/* epoc or iso time stamp */
if (epoch)
fprintf(file, "%ld", t);
else {
char time_stamp[64];
struct tm tm;
int len;
localtime_r(&t, &tm);
len = snprintf(time_stamp, sizeof(time_stamp), "%4d-%.2d-%.2dT%02d:%02d:%02d",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
strftime(time_stamp + len, sizeof(time_stamp) - len, "%z", &tm);
fprintf(file, "%s", time_stamp);
}
}
/*! \brief Template base level tag parser and printer. */
static int must_put_base(void *closure, const char *name, int escape
__attribute__ ((unused)), FILE *file)

View file

@ -47,6 +47,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "error.h"
@ -544,6 +546,36 @@ void clean_up(struct conf_t *state)
}
}
/*! \brief Print a time stamp of a path or now to output file. */
void dp_time_tool(FILE *file, const char *path, int epoch)
{
time_t t;
/* a file or now */
if (path) {
struct stat st;
stat(path, &st);
t = st.st_mtime;
} else
t = time(NULL);
/* epoc or iso time stamp */
if (epoch)
fprintf(file, "%ld", t);
else {
char time_stamp[64];
struct tm tm;
int len;
localtime_r(&t, &tm);
len = snprintf(time_stamp, sizeof(time_stamp), "%4d-%.2d-%.2dT%02d:%02d:%02d",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
strftime(time_stamp + len, sizeof(time_stamp) - len, "%z", &tm);
fprintf(file, "%s", time_stamp);
}
}
/*! \brief A version printing. */
void __attribute__ ((__noreturn__)) print_version(void)
{

View file

@ -609,22 +609,6 @@ static int output_json(struct conf_t *state, const int print_mac_addreses)
*/
static void html_header(struct conf_t *state, FILE *restrict f)
{
char outstr[200];
struct tm *tmp, result;
struct stat statbuf;
stat(state->dhcpdlease_file, &statbuf);
tmp = localtime_r(&statbuf.st_mtime, &result);
if (tmp == NULL) {
error(EXIT_FAILURE, errno, "html_header: localtime");
}
setlocale(LC_CTYPE, "");
setlocale(LC_NUMERIC, "");
if (strftime(outstr, sizeof(outstr), nl_langinfo(D_T_FMT), &result) == 0) {
error(EXIT_FAILURE, 0, "html_header: strftime returned 0");
}
fprintf(f, "<!DOCTYPE html>\n");
fprintf(f, "<html>\n");
fprintf(f, "<head>\n");
@ -641,8 +625,9 @@ static void html_header(struct conf_t *state, FILE *restrict f)
fprintf(f, "<body>\n");
fprintf(f, "<div class=\"container\">\n");
fprintf(f, "<h2>ISC DHCPD status</h2>\n");
fprintf(f, "<small>File %s was last modified at %s</small><hr />\n", state->dhcpdlease_file,
outstr);
fprintf(f, "<small>File %s was last modified at ", state->dhcpdlease_file);
dp_time_tool(f, state->dhcpdlease_file, 0);
fprintf(f, "</small><hr />\n");
}
/*! \brief Footer for full html output format.