forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
97 lines (80 loc) · 2.82 KB
/
utils.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
function init() {
function utils() {
return utils;
}
var MINUTE_IN_SECS = 60
, HOUR_IN_SECS = 3600
, DAY_IN_SECS = 86400;
utils.toFixed = function toFixed(value) {
if (value === 0) {
return '0';
} else {
var fixed = value.toFixed(2);
return fixed === '-0.00' ? '0.00' : fixed;
}
};
utils.timeAgo = function timeAgo(time, clientSettings) {
var now = Date.now()
, offset = time === -1 ? -1 : (now - time) / 1000
, parts = {};
if (offset < MINUTE_IN_SECS * -5) {
parts = { value: 'in the future' };
} else if (offset === -1) {
parts = { label: 'time ago' };
} else if (offset <= MINUTE_IN_SECS * 2) {
parts = { value: 1, label: 'min ago' };
} else if (offset < (MINUTE_IN_SECS * 60)) {
parts = { value: Math.round(Math.abs(offset / MINUTE_IN_SECS)), label: 'mins ago' };
} else if (offset < (HOUR_IN_SECS * 2)) {
parts = { value: 1, label: 'hr ago' };
} else if (offset < (HOUR_IN_SECS * 24)) {
parts = { value: Math.round(Math.abs(offset / HOUR_IN_SECS)), label: 'hrs ago' };
} else if (offset < DAY_IN_SECS) {
parts = { value: 1, label: 'day ago' };
} else if (offset <= (DAY_IN_SECS * 7)) {
parts = { value: Math.round(Math.abs(offset / DAY_IN_SECS)), label: 'day ago' };
} else {
parts = { value: 'long ago' };
}
if (offset > DAY_IN_SECS * 7) {
parts.status = 'warn';
} else if (offset < MINUTE_IN_SECS * -5 || offset > (MINUTE_IN_SECS * clientSettings.alarmTimeAgoUrgentMins)) {
parts.status = 'urgent';
} else if (offset > (MINUTE_IN_SECS * clientSettings.alarmTimeAgoWarnMins)) {
parts.status = 'warn';
} else {
parts.status = 'current';
}
return parts;
};
// some helpers for input "date"
utils.mergeInputTime = function mergeInputTime(timestring,datestring) {
var eventTimeParts = timestring.split(':');
var eventTime = new Date(datestring);
eventTime.setHours(eventTimeParts[0]);
eventTime.setMinutes(eventTimeParts[1]);
eventTime.setSeconds(0);
eventTime.setMilliseconds(0);
return eventTime;
};
utils.toDateInputValue = function toDateInputValue(datetime) {
var local = new Date(datetime);
local.setMinutes(datetime.getMinutes() - datetime.getTimezoneOffset());
return local.toJSON().slice(0,10);
};
utils.addMinutes = function addMinutes(datetime,h) {
datetime.setTime(datetime.getTime() + (h*60*1000));
return datetime;
};
utils.addHours = function addHours(datetime,h) {
datetime.setTime(datetime.getTime() + (h*60*60*1000));
return datetime;
};
utils.addDays = function addDays(datetime,h) {
datetime.setTime(datetime.getTime() + (h*24*60*60*1000));
return datetime;
};
return utils();
}
module.exports = init;