-
Notifications
You must be signed in to change notification settings - Fork 0
/
zeitFormat.js
56 lines (42 loc) · 1.87 KB
/
zeitFormat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module.exports.uptime = zeit;
// From
// https://community.openhab.org/t/javascript-transform-example-with-system-uptime/40063
function zeit(i) {
let val = parseInt(i); // The value sent by OH is a string, so we parse into an integer
let days = 0; // Initialise variables
let hours = 0;
let minutes = 0;
let seconds = 0;
days = Math.floor(val / 86400); // Number of days
val = val - (days * 86400); // Remove days from val
hours = Math.floor(val / 3600); // Number of hours
val = val - (hours * 3600); // Remove hours from val
minutes = Math.floor(val / 60); // Number of minutes
val = val - (minutes * 60); // Remove hours from val
seconds = Math.floor(val / 1); // Number of seconds
let stringDays = ''; // Initialse string variables
let stringHours = '';
let stringMinutes = '';
let stringSeconds = '';
if (days === 1) {
stringDays = '1 day '; // Only 1 day so no 's'
} else if (days > 1) {
stringDays = days + ' days '; // More than 1 day so 's'
} // If days = 0 then stringDays remains ''
if (hours === 1) {
stringHours = '1 hour '; // Only 1 hour so no 's'
} else if (hours > 1) {
stringHours = hours + ' hours '; // More than 1 hour so 's'
} // If hours = 0 then stringHours remains ''
if (minutes === 1) {
stringMinutes = '1 min '; // Only 1 minute so no 's'
} else
stringMinutes = minutes + ' min '; // More than 1 minute or 0 minutes, so 's'
if (seconds === 1) {
stringSeconds = '1 sec'; // Only 1 second so no 's'
} else
stringSeconds = seconds + ' sec'; // More than 1 second or 0 seconds, so 's'
// If minutes = 0 then stringSeconds remains ''
var returnString = stringDays + stringHours + stringMinutes + stringSeconds
return returnString.trim(); // Removes the extraneous space at the end
}