users() fix date time parsing

This commit is contained in:
Sebastian Hildebrandt 2026-01-06 08:06:53 +01:00
parent c169b92772
commit c87c944cac

View File

@ -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 {