users() optimized parseDateTime function

This commit is contained in:
Sebastian Hildebrandt 2020-04-19 17:58:30 +02:00
parent 5714c98917
commit 0e9f3c0ff7
5 changed files with 89 additions and 34 deletions

View File

@ -30,6 +30,7 @@ For major (breaking) changes - version 3 and 2 see end of page.
| Version | Date | Comment |
| -------------- | -------------- | -------- |
| 4.23.4 | 2020-04-20 | `users()` optimized parseDateTime function |
| 4.23.3 | 2020-04-09 | recactored to avoid `cat` |
| 4.23.2 | 2020-04-08 | `cpu()` fixed getting base frequency for AMD Ryzen |
| 4.23.1 | 2020-03-11 | `diskLayout()` optimized detection linux |

View File

@ -83,6 +83,11 @@
</tr>
</thead>
<tbody>
<tr>
<th scope="row">4.23.4</th>
<td>2020-04-20</td>
<td><span class="code">users()</span> optimized parseDateTime function</td>
</tr>
<tr>
<th scope="row">4.23.3</th>
<td>2020-04-09</td>

View File

@ -207,7 +207,7 @@
<div class="title">Downloads last month</div>
</div>
<div class="col-xl-4 col-lg-4 col-md-4 col-12">
<div class="numbers">261</div>
<div class="numbers">266</div>
<div class="title">Dependends</div>
</div>
</div>

View File

@ -26,9 +26,42 @@ const _openbsd = (_platform === 'openbsd');
const _netbsd = (_platform === 'netbsd');
const _sunos = (_platform === 'sunos');
let _winDateFormat = {
dateFormat: '',
dateSeperator: '',
timeFormat: '',
timeSeperator: '',
amDesignator: ''
};
// --------------------------
// array of users online = sessions
function getWinCulture() {
return new Promise((resolve) => {
process.nextTick(() => {
if (!_winDateFormat) {
util.powerShell('(get-culture).DateTimeFormat')
.then(data => {
let lines = data.toString().split('\r\n');
_winDateFormat.dateFormat = util.getValue(lines, 'ShortDatePattern', ':');
_winDateFormat.dateSeperator = util.getValue(lines, 'DateSeparator', ':');
_winDateFormat.timeFormat = util.getValue(lines, 'ShortTimePattern', ':');
_winDateFormat.timeSeperator = util.getValue(lines, 'TimeSeparator', ':');
_winDateFormat.amDesignator = util.getValue(lines, 'AMDesignator', ':');
resolve(_winDateFormat);
})
.catch(() => {
resolve(_winDateFormat);
});
} else {
resolve(_winDateFormat);
}
});
});
}
function parseUsersLinux(lines, phase) {
let result = [];
let result_who = [];
@ -141,7 +174,7 @@ function parseUsersDarwin(lines) {
return result;
}
function parseUsersWin(lines) {
function parseUsersWin(lines, culture) {
let result = [];
const header = lines[0];
@ -164,7 +197,7 @@ function parseUsersWin(lines) {
if (lines[i].trim()) {
const user = lines[i].substring(headerDelimiter[0] + 1, headerDelimiter[1]).trim() || '';
const tty = lines[i].substring(headerDelimiter[1] + 1, headerDelimiter[2] - 2).trim() || '';
const dateTime = util.parseDateTime(lines[i].substring(headerDelimiter[5] + 1, 2000).trim()) || '';
const dateTime = util.parseDateTime(lines[i].substring(headerDelimiter[5] + 1, 2000).trim(), culture) || '';
result.push({
user: user,
tty: tty,
@ -252,7 +285,10 @@ function users(callback) {
if (stdout) {
// lines / split
let lines = stdout.toString().split('\r\n');
result = parseUsersWin(lines);
getWinCulture()
.then(culture => {
result = parseUsersWin(lines, culture);
});
}
if (callback) { callback(result); }
resolve(result);

View File

@ -119,45 +119,53 @@ function decodeEscapeSequence(str, base) {
});
}
function parseTime(t) {
function detectSplit(str) {
let seperator = '';
let part = 0;
str.split('').forEach(element => {
if (element >= '0' && element <= '9') {
if (part === 1) { part++; }
} else {
if (part === 0) { part++; }
if (part === 1) {
seperator += element;
}
}
});
return seperator;
}
function parseTime(t, timeFormat, timeSeperator) {
timeFormat = timeFormat || '';
timeSeperator = timeSeperator || '';
t = t.toUpperCase();
let hour = 0;
let min = 0;
let parts = t.split(':');
let splitter = detectSplit(t);
let parts = t.split(splitter);
if (parts.length >= 2) {
let isPM = (parts[1] && (parts[1].toLowerCase().indexOf('pm') > -1) || (parts[1].toLowerCase().indexOf('p.m.') > -1) || (parts[1].toLowerCase().indexOf('p. m.') > -1));
hour = parseInt(parts[0], 10);
min = parseInt(parts[1], 10);
hour = isPM && hour < 12 ? hour + 12 : hour;
return ('0' + hour).substr(-2) + ':' + ('0' + min).substr(-2);
}
parts = t.split('.');
if (parts.length >= 2) {
let isPM = (parts[1] && (parts[1].toLowerCase().indexOf('pm') > -1) || (parts[1].toLowerCase().indexOf('p.m.') > -1) || (parts[1].toLowerCase().indexOf('p. m.') > -1) || (parts[1].toLowerCase().indexOf('n') > -1) || (parts[1].toLowerCase().indexOf('ch') > -1) || (parts[1].toLowerCase().indexOf('ös') > -1));
hour = parseInt(parts[0], 10);
min = parseInt(parts[1], 10);
hour = isPM && hour < 12 ? hour + 12 : hour;
return ('0' + hour).substr(-2) + ':' + ('0' + min).substr(-2);
}
parts = t.split(' ');
if (parts.length >= 2) {
let isPM = ((t.toLowerCase().indexOf('pm') > -1) || (t.toLowerCase().indexOf('p.m.') > -1) || (t.toLowerCase().indexOf('p. m.') > -1) || (t.toLowerCase().indexOf('n') > -1) || (t.toLowerCase().indexOf('ch') > -1) || (t.toLowerCase().indexOf('ös') > -1));
hour = parseInt(parts[0], 10);
if (parts[1] === 'h' && parts[2]) {
min = parseInt(parts[2], 10);
} else {
min = parseInt(parts[1], 10);
if (parts[2]) {
parts[1] += parts[2];
}
let isPM = (parts[1] && (parts[1].toLowerCase().indexOf('pm') > -1) || (parts[1].toLowerCase().indexOf('p.m.') > -1) || (parts[1].toLowerCase().indexOf('p. m.') > -1) || (parts[1].toLowerCase().indexOf('n') > -1) || (parts[1].toLowerCase().indexOf('ch') > -1) || (parts[1].toLowerCase().indexOf('ös') > -1) || timeSeperator);
hour = parseInt(parts[0], 10);
min = parseInt(parts[1], 10);
hour = isPM && hour < 12 ? hour + 12 : hour;
return ('0' + hour).substr(-2) + ':' + ('0' + min).substr(-2);
}
}
function parseDateTime(dt) {
function parseDateTime(dt, culture) {
const result = {
date: '',
time: ''
};
culture = culture || {};
let dateFormat = (culture.dateFormat || '').toLowerCase();
let timeFormat = (culture.timeFormat || '');
// let dateSeperator = (culture.dateSeperator || '');
let timeSeperator = (culture.timeSeperator || '');
const parts = dt.split(' ');
if (parts[0]) {
if (parts[0].indexOf('/') >= 0) {
@ -167,13 +175,13 @@ function parseDateTime(dt) {
if (dtparts[0].length === 4) {
// Dateformat: yyyy/mm/dd
result.date = dtparts[0] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[2]).substr(-2);
} else if (dtparts[2].length === 2) {
} else if (dtparts[2].length === 2 || dateFormat.indexOf('/mm/') > -1) {
// Dateformat: dd/mm/yy
result.date = '20' + dtparts[2] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[0]).substr(-2);
} else {
// Dateformat: mm/dd/yyyy or dd/mm/yyyy
const isEN = ((dt.toLowerCase().indexOf('pm') > -1) || (dt.toLowerCase().indexOf('p.m.') > -1) || (dt.toLowerCase().indexOf('p. m.') > -1) || (dt.toLowerCase().indexOf('am') > -1) || (dt.toLowerCase().indexOf('a.m.') > -1) || (dt.toLowerCase().indexOf('a. m.') > -1));
if (isEN) {
if (isEN || dateFormat.indexOf('/d/') > -1 || dateFormat.indexOf('/dd/') > -1) {
// Dateformat: mm/dd/yyyy
result.date = dtparts[2] + '-' + ('0' + dtparts[0]).substr(-2) + '-' + ('0' + dtparts[1]).substr(-2);
} else {
@ -184,10 +192,15 @@ function parseDateTime(dt) {
}
}
if (parts[0].indexOf('.') >= 0) {
// Dateformat: dd.mm.yyyy
const dtparts = parts[0].split('.');
if (dtparts.length === 3) {
result.date = dtparts[2] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[0]).substr(-2);
if (dateFormat.indexOf('.d.') > -1 || dateFormat.indexOf('.dd.') > -1) {
// Dateformat: mm.dd.yyyy
result.date = dtparts[2] + '-' + ('0' + dtparts[0]).substr(-2) + '-' + ('0' + dtparts[1]).substr(-2);
} else {
// Dateformat: dd.mm.yyyy
result.date = dtparts[2] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[0]).substr(-2);
}
}
}
if (parts[0].indexOf('-') >= 0) {
@ -201,7 +214,7 @@ function parseDateTime(dt) {
if (parts[1]) {
parts.shift();
let time = parts.join(' ');
result.time = parseTime(time);
result.time = parseTime(time, timeFormat, timeSeperator);
}
return result;
}