Skip to content

Instantly share code, notes, and snippets.

@os0x
Forked from cho45/duration.js
Created January 13, 2009 10:55
Show Gist options
  • Save os0x/46403 to your computer and use it in GitHub Desktop.
Save os0x/46403 to your computer and use it in GitHub Desktop.
function duration (dat) {
var ret = 0, map = {
sec:1, min:60, hour:3600, day:86400, week:604800, month:2592000, year:31536000
};
for (var k in dat) if (map[k]) ret += dat[k] * map[k];
return ret * 1000;
}
/*
function eq (obj, expect) {
if (obj == expect) {
print("ok\n");
} else {
print("ng\nexpect:\n" + uneval(expect) + "\nbut:\n" + uneval(obj) + "\n");
}
};
eq(duration({sec:1}), 1000);
eq(duration({min:1}), 60 * 1000);
eq(duration({hour:1}), 60 * 60 * 1000);
eq(duration({day:1}), 24 * 60 * 60 * 1000);
eq(duration({week:1}), 7 * 24 * 60 * 60 * 1000);
eq(duration({month:1}), 30 * 24 * 60 * 60 * 1000);
eq(duration({year:1}), 365 * 24 * 60 * 60 * 1000);
eq(duration({day:1, hour:12}), (24 * 60 * 60 + 12 * 60 * 60) * 1000);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment