forked from jeffsu/upbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htime.ms
52 lines (42 loc) · 1.12 KB
/
htime.ms
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
export class HumanTime {
private {
var SEC = 1000;
var MIN = SEC * 60;
var HOUR = MIN * 60;
var UNITS = [ 'd', 'h', 'm', 's' ];
var VALS = [ 24 * HOUR, HOUR, MIN, SEC ];
}
function now() {
return this._now || (this._now = (new Date()).getTime());
}
function ago(t) {
return this.now() - t;
}
function humanizeAgo(ms) {
var ago = this.ago(ms);
var human = this.humanize(ago);
if (human === '---') return human;
return human + ' ago';
}
function humanize(ms) {
if (isNaN(ms)) return "---";
var ret = [];
foreach (var v:i in VALS) {
var amount = Math.floor(ms / v);
ms -= amount * v;
if (amount > 0 || i == 3) ret.push(amount + UNITS[i]);
}
return ret.join(' ');
}
function pad(n) {
return (n < 10 ? "0" : '') + n;
}
function dateTime(d) {
if (!d) return '---';
var date = [d.getMonth(), d.getDay(), d.getFullYear()].join('-');
var minute = this.pad(d.getMinutes());
var second = this.pad(d.getSeconds());
var time = [d.getHours(), minute, second].join(':');
return date + ' ' + time;
};
}