add: Util.timecode static function

This commit is contained in:
lancegin
2017-06-30 10:58:37 +08:00
parent 6b4db177d7
commit 9a2d5da7ef
2 changed files with 31 additions and 1 deletions
+23
View File
@@ -92,4 +92,27 @@ export class Util {
return result;
}
/*
* format the time string to int
*
* @param {time}
* @type {Date}
* @desc the time need to be format
*
* @param {interval}
* @type {Int}
* @desc interval means the one-time password's life,
* default to be 30.
*
* @return {Int}
*/
static timecode(time, interval) {
let time_str = Date.parse(time).toString();
// fotmat the time, the ms is not needed.
let format_time = time_str.substring(0, time_str.length-3);
return parseInt(parseInt(format_time) / interval);
}
}
+8 -1
View File
@@ -31,6 +31,13 @@ describe('Util module test', function() {
let input = 49957590;
assert.equal("\u0000\u0000\u0000\u0000\u0002úJÖ", a.int_to_bytestring(input));
});
});
});
describe('static timecode() function', function() {
it("should format the time string to int", function() {
let now = new Date();
assert.equal("number", typeof(a.timecode(now, 30)));
});
});
});