w: Fix idle time in json output, add login/idle times to json output

Currently the idle time will show as `true` part of the time in the json
output and quoting depends on what is being printed. Make sure it's
always printed correctly and for consistency treated as a string in the
json output.

Login time delta and since times are currently exposed in the xml
output, expose these times in the json output as well.

In the json and xml outputs expose the number of seconds idle as a new
field or attribute respectively.

MFC after:	1 week
Sponsored by:	Modirum MDPay
Event:		Oslo Hackathon 202508
Differential Revision:	https://reviews.freebsd.org/D52237
This commit is contained in:
Marius Halden
2025-08-29 16:36:32 +02:00
committed by Dag-Erling Smørgrav
parent 86a0941db7
commit 6e6febb54d
+19 -7
View File
@@ -79,8 +79,13 @@ pr_attime(time_t *started, time_t *now)
(void)wcsftime(buf, sizeof(buf), fmt, &tp);
len = wcslen(buf);
width = wcswidth(buf, len);
xo_attr("since", "%lu", (unsigned long) *started);
xo_attr("delta", "%lu", (unsigned long) diff);
if (xo_get_style(NULL) == XO_STYLE_XML) {
xo_attr("since", "%lu", (unsigned long)*started);
xo_attr("delta", "%lu", (unsigned long)diff);
} else {
xo_emit("{e:login-time-since/%lu}{e:login-time-delta/%lu}",
(unsigned long)*started, (unsigned long)diff);
}
if (len == width)
xo_emit("{:login-time/%-7.7ls/%ls}", buf);
else if (width < 7)
@@ -100,10 +105,16 @@ pr_attime(time_t *started, time_t *now)
int
pr_idle(time_t idle)
{
/* In encoded formats, emit the raw data as well */
if (xo_get_style(NULL) == XO_STYLE_XML)
xo_attr("seconds", "%lu", (unsigned long) idle);
else
xo_emit("{e:idle-seconds/%lu}", (unsigned long) idle);
/* If idle more than 36 hours, print as a number of days. */
if (idle >= 36 * 3600) {
int days = idle / 86400;
xo_emit(" {:idle/%dday%s} ", days, days > 1 ? "s" : " " );
xo_emit(" {q:idle/%dday%s} ", days, days > 1 ? "s" : " " );
if (days >= 100)
return (2);
if (days >= 10)
@@ -111,16 +122,17 @@ pr_idle(time_t idle)
}
/* If idle more than an hour, print as HH:MM. */
else if (idle >= 3600)
xo_emit(" {:idle/%2d:%02d/} ",
else if (idle >= 3600) {
xo_emit(" {q:idle/%2d:%02d} ",
(int)(idle / 3600), (int)((idle % 3600) / 60));
}
else if (idle / 60 == 0)
xo_emit(" - ");
xo_emit(" - {q:idle//0}");
/* Else print the minutes idle. */
else
xo_emit(" {:idle/%2d} ", (int)(idle / 60));
xo_emit(" {q:idle/%2d} ", (int)(idle / 60));
return (0); /* not idle longer than 9 days */
}