Skip to content

Commit

Permalink
humanize
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsu committed Sep 12, 2012
1 parent 560dd40 commit 762211a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 47 deletions.
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
"description": "One stop show to your dashboard",
"keywords": ["javascript", "performance", "health", "monitoring", "monit", "haproxy", "forever", "process", "systems", "dashboard" ],
"author": "Jeff Su <[email protected]>",
<<<<<<< HEAD
"version": "0.2.3-pre4",
=======
"version": "0.3.0-alpha1",
>>>>>>> feature/dashboard
"licenses": [{ "type": "MIT" }],
"engines": { "node": ">=0.5.0" },
"directories" : { "lib" : "./lib/upbeat" },
Expand All @@ -21,7 +17,7 @@
"mochiscript": ">=0.6.14",
"js-yaml": ">=0.3.1",
"winston": ">=0.6.2",
"tempo": ">=0.1.0-pre10"
"tempo": ">=0.1.0-pre11"
},
"repository": {
"type": "git",
Expand Down
78 changes: 36 additions & 42 deletions www/htime.ms
Original file line number Diff line number Diff line change
@@ -1,58 +1,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 secondsAgo(t) {
return Math.floor((self.now() - t)/1000);
function ago(t) {
return this.now() - t;
}

function timeDistance(time) {
var now = this.now();
var sec = 0, min = 0, hr = 0, day = 0;
sec = this.secondsAgo(time);
if (isNaN(sec)) => '---';

if (sec >= 60) {
min = Math.floor(sec/60);
sec = sec % 60;
if (min >= 60) {
hr = Math.floor(min/60);
min = min % 60;
if (hr >= 24) {
day = Math.floor(hr/24);
hr = hr % 24;
}
}
}
var hrMinSec = [
['d', day],
['h', hr],
['m', min],
['s', sec]
];
var timeTxt = '';
var nStr;

foreach (var division in hrMinSec) {
var t = division[1];
if (t === 0 && division[0] != 's') continue;
nStr = t.toString();
timeTxt += (nStr + division[0] + ' ');
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) ret.push(amount + UNITS[i]);
}
return timeTxt;
};

return ret.join(' ');
}

function pad(n) {
return (n < 10 ? "0" : '') + n;
}

function dateTime(t) {
if (typeof t != 'number') return '';
var d = new Date(t);
var date = [d.getMonth(), d.getDay(), d.getFullYear()].join('-');
var pad = #(t) {
if (t < 10) => '0' + t.toString();
else return t.toString();
}
var minute = pad(d.getMinutes());
var second = pad(d.getSeconds());
var minute = this.pad(d.getMinutes());
var second = this.pad(d.getSeconds());
var time = [d.getHours(), minute, second].join(':');
return date + ' ' + time;
};
Expand Down

0 comments on commit 762211a

Please sign in to comment.