From b5092a4444626146cf7941dac947841a9453c5aa Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 26 Apr 2016 11:26:15 +0200 Subject: [PATCH] feat(toTimestamp): handle timestamps. --- src/xapi-object-to-xo.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/xapi-object-to-xo.js b/src/xapi-object-to-xo.js index 3c76e8f20..72b5af5b0 100644 --- a/src/xapi-object-to-xo.js +++ b/src/xapi-object-to-xo.js @@ -35,18 +35,28 @@ function link (obj, prop, idField = '$id') { // Parse a string date time to a Unix timestamp (in seconds). // +// If the value is a number or can be converted as one, it is assumed +// to already be a timestamp and returned. +// // If there are no data or if the timestamp is 0, returns null. function toTimestamp (date) { if (!date) { return null } - const ms = parseDateTime(date).getTime() + const timestamp = +date + + // Not NaN. + if (timestamp === timestamp) { // eslint-disable-line no-self-compare + return timestamp + } + + const ms = parseDateTime(date) if (!ms) { return null } - return Math.round(ms / 1000) + return Math.round(ms.getTime() / 1000) } // ===================================================================