Commit 625b21ef authored by Alessandro Rubini's avatar Alessandro Rubini

lib/util: offer different time formats, for syslog mainly

Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 83bb333f
......@@ -16,7 +16,10 @@
#define C_BLUE 4
/* Return TAI date/time in human-readable form. Non-reentrant. */
char *format_time(uint64_t sec);
char *format_time(uint64_t sec, int format);
#define TIME_FORMAT_LEGACY 0
#define TIME_FORMAT_SYSLOG 1
#define TIME_FORMAT_SORTED 2
/* Color printf() variant. */
void cprintf(int color, const char *fmt, ...);
......
......@@ -73,7 +73,8 @@ void syslog_poll(void)
shw_pps_gen_get_time(&secs, NULL);
len = pp_sprintf(buf + UDP_END, /* 8 == user + 6 == info */
"<14> %s wr-node: Alive and Well", format_time(secs));
"<14> %s wr-node: Alive and Well",
format_time(secs, TIME_FORMAT_SYSLOG));
len += UDP_END;
memcpy(&syslog_addr.saddr, ip, 4);
fill_udp((void *)buf, len, &syslog_addr);
......
......@@ -41,7 +41,7 @@ static const int _ytab[2][12] = {
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
char *format_time(uint64_t sec)
char *format_time(uint64_t sec, int format)
{
struct tm t;
static char buf[64];
......@@ -69,9 +69,23 @@ char *format_time(uint64_t sec)
t.tm_mday = dayno + 1;
t.tm_isdst = 0;
sprintf(buf, "%s, %s %d, %d, %02d:%02d:%02d", _days[t.tm_wday],
_months[t.tm_mon], t.tm_mday, t.tm_year + YEAR0, t.tm_hour,
t.tm_min, t.tm_sec);
switch(format) {
case TIME_FORMAT_LEGACY:
default:
sprintf(buf, "%s, %s %d, %d, %02d:%02d:%02d", _days[t.tm_wday],
_months[t.tm_mon], t.tm_mday, t.tm_year + YEAR0,
t.tm_hour, t.tm_min, t.tm_sec);
break;
case TIME_FORMAT_SYSLOG:
sprintf(buf, "%s %2d %02d:%02d:%02d", _months[t.tm_mon],
t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
break;
case TIME_FORMAT_SORTED:
sprintf(buf, "%4d-%02d-%02d-%02d:%02d:%02d",
t.tm_year + YEAR0, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
break;
}
return buf;
}
......
......@@ -110,7 +110,7 @@ void wrc_mon_gui(void)
shw_pps_gen_get_time(&sec, &nsec);
cprintf(C_BLUE, "\n\nTAI Time: ");
cprintf(C_WHITE, "%s", format_time(sec));
cprintf(C_WHITE, "%s", format_time(sec, TIME_FORMAT_LEGACY));
/*show_ports */
wrpc_get_port_state(&state, NULL);
......
......@@ -53,7 +53,9 @@ static int cmd_time(const char *args[])
return 0;
}
pp_printf("%s +%d nanoseconds.\n", format_time(sec), nsec); /* fixme: clock freq is not always 125 MHz */
pp_printf("%s +%d nanoseconds.\n",
format_time(sec, TIME_FORMAT_LEGACY), nsec);
/* fixme: clock freq is not always 125 MHz */
return 0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment