From c87c944cacef57a945cf069500de27b84f487ee5 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Tue, 6 Jan 2026 08:06:53 +0100 Subject: [PATCH] users() fix date time parsing --- lib/users.js | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/users.js b/lib/users.js index dd0d577..c138b6a 100644 --- a/lib/users.js +++ b/lib/users.js @@ -26,9 +26,22 @@ const _openbsd = _platform === 'openbsd'; const _netbsd = _platform === 'netbsd'; const _sunos = _platform === 'sunos'; +function parseDate(dtMon, dtDay) { + let dt = new Date().toISOString().slice(0, 10); + try { + dt = '' + new Date().getFullYear() + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(dtMon.toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + dtDay).slice(-2); + if (new Date(dt) > new Date()) { + dt = '' + (new Date().getFullYear() - 1) + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(dtMon.toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + dtDay).slice(-2); + } + } catch { + util.noop(); + } + return dt; +} + function parseUsersLinux(lines, phase) { const result = []; - const result_who = []; + let result_who = []; const result_w = {}; let w_first = true; let w_header = []; @@ -36,21 +49,27 @@ function parseUsersLinux(lines, phase) { let who_line = {}; let is_whopart = true; + let is_whoerror = false; lines.forEach((line) => { if (line === '---') { is_whopart = false; } else { const l = line.replace(/ +/g, ' ').split(' '); - // who part if (is_whopart) { - result_who.push({ - user: l[0], - tty: l[1], - date: l[2], - time: l[3], - ip: l && l.length > 4 ? l[4].replace(/\(/g, '').replace(/\)/g, '') : '' - }); + if (line.toLowerCase().indexOf('unexpected') >= 0 || line.toLowerCase().indexOf('unrecognized') >= 0) { + is_whoerror = true; + result_who = []; + } + if (!is_whoerror) { + result_who.push({ + user: l[0], + tty: l[1], + date: parseDate(l[2], l[3]), + time: l[4], + ip: l && l.length > 4 + 1 ? l[4 + 1].replace(/\(/g, '').replace(/\)/g, '') : '' + }); + } } else { // w part if (w_first) { @@ -116,18 +135,10 @@ function parseUsersDarwin(lines) { // who part if (is_whopart) { - let dt = '' + new Date().getFullYear() + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(l[2].toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + l[3]).slice(-2); - try { - if (new Date(dt) > new Date()) { - dt = '' + (new Date().getFullYear() - 1) + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(l[2].toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + l[3]).slice(-2); - } - } catch { - util.noop(); - } result_who.push({ user: l[0], tty: l[1], - date: dt, + date: parseDate(l[2], l[3]), time: l[4] }); } else {