diff --git a/src/util.js b/src/util.js index b710945..4ff0334 100644 --- a/src/util.js +++ b/src/util.js @@ -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); + } + } \ No newline at end of file diff --git a/test/util_test.js b/test/util_test.js index 78b487c..cfb6435 100644 --- a/test/util_test.js +++ b/test/util_test.js @@ -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))); + }); + }); });