Skip to content

Commit c87c944

Browse files
users() fix date time parsing
1 parent c169b92 commit c87c944

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

lib/users.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,50 @@ const _openbsd = _platform === 'openbsd';
2626
const _netbsd = _platform === 'netbsd';
2727
const _sunos = _platform === 'sunos';
2828

29+
function parseDate(dtMon, dtDay) {
30+
let dt = new Date().toISOString().slice(0, 10);
31+
try {
32+
dt = '' + new Date().getFullYear() + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(dtMon.toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + dtDay).slice(-2);
33+
if (new Date(dt) > new Date()) {
34+
dt = '' + (new Date().getFullYear() - 1) + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(dtMon.toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + dtDay).slice(-2);
35+
}
36+
} catch {
37+
util.noop();
38+
}
39+
return dt;
40+
}
41+
2942
function parseUsersLinux(lines, phase) {
3043
const result = [];
31-
const result_who = [];
44+
let result_who = [];
3245
const result_w = {};
3346
let w_first = true;
3447
let w_header = [];
3548
const w_pos = [];
3649
let who_line = {};
3750

3851
let is_whopart = true;
52+
let is_whoerror = false;
3953
lines.forEach((line) => {
4054
if (line === '---') {
4155
is_whopart = false;
4256
} else {
4357
const l = line.replace(/ +/g, ' ').split(' ');
44-
4558
// who part
4659
if (is_whopart) {
47-
result_who.push({
48-
user: l[0],
49-
tty: l[1],
50-
date: l[2],
51-
time: l[3],
52-
ip: l && l.length > 4 ? l[4].replace(/\(/g, '').replace(/\)/g, '') : ''
53-
});
60+
if (line.toLowerCase().indexOf('unexpected') >= 0 || line.toLowerCase().indexOf('unrecognized') >= 0) {
61+
is_whoerror = true;
62+
result_who = [];
63+
}
64+
if (!is_whoerror) {
65+
result_who.push({
66+
user: l[0],
67+
tty: l[1],
68+
date: parseDate(l[2], l[3]),
69+
time: l[4],
70+
ip: l && l.length > 4 + 1 ? l[4 + 1].replace(/\(/g, '').replace(/\)/g, '') : ''
71+
});
72+
}
5473
} else {
5574
// w part
5675
if (w_first) {
@@ -116,18 +135,10 @@ function parseUsersDarwin(lines) {
116135

117136
// who part
118137
if (is_whopart) {
119-
let dt = '' + new Date().getFullYear() + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(l[2].toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + l[3]).slice(-2);
120-
try {
121-
if (new Date(dt) > new Date()) {
122-
dt = '' + (new Date().getFullYear() - 1) + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(l[2].toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + l[3]).slice(-2);
123-
}
124-
} catch {
125-
util.noop();
126-
}
127138
result_who.push({
128139
user: l[0],
129140
tty: l[1],
130-
date: dt,
141+
date: parseDate(l[2], l[3]),
131142
time: l[4]
132143
});
133144
} else {

0 commit comments

Comments
 (0)