git: bc8b95bb7695 - stable/14 - w: Fix idle time in json output, add login/idle times to json output
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 16 Sep 2025 13:59:13 UTC
The branch stable/14 has been updated by des:
URL: https://cgit.FreeBSD.org/src/commit/?id=bc8b95bb7695b372d0d25d261fa8f3d7beed7fc8
commit bc8b95bb7695b372d0d25d261fa8f3d7beed7fc8
Author: Marius Halden <marius.halden@modirum.com>
AuthorDate: 2025-08-29 14:36:32 +0000
Commit: Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2025-09-16 13:03:58 +0000
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
(cherry picked from commit 6e6febb54da91bf5e13007c3d8f4a54495273969)
---
usr.bin/w/pr_time.c | 26 +++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/usr.bin/w/pr_time.c b/usr.bin/w/pr_time.c
index 7be11b777e79..ff2da1bcc3c9 100644
--- a/usr.bin/w/pr_time.c
+++ b/usr.bin/w/pr_time.c
@@ -82,8 +82,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)
@@ -103,10 +108,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)
@@ -114,16 +125,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 */
}