From c54030f16f4d14cc21ccc159ee73192b310ba21c Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Mon, 23 Oct 2017 18:36:39 -0700 Subject: [PATCH] Accept timestamp with missing nanoseconds (#41) This ports https://github.com/firebase/firebase-functions/commit/0647a82fd8e3dce954a7125d17f7a4bec483f48e back to the GCloud SDK --- src/convert.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/convert.js b/src/convert.js index 27b417fb9..aff8f5155 100644 --- a/src/convert.js +++ b/src/convert.js @@ -49,18 +49,15 @@ function convertTimestamp(timestampValue, argumentName) { if (is.string(timestampValue)) { let date = new Date(timestampValue); let seconds = Math.floor(date.getTime() / 1000); - let nanos = null; + let nanos = 0; - let nanoString = timestampValue.substring(20, timestampValue.length - 1); - - if (nanoString.length === 3) { - nanoString = `${nanoString}000000`; - } else if (nanoString.length === 6) { - nanoString = `${nanoString}000`; - } - - if (nanoString.length === 9) { - nanos = parseInt(nanoString); + if (timestampValue.length > 20) { + const nanoString = timestampValue.substring( + 20, + timestampValue.length - 1 + ); + const trailingZeroes = 9 - nanoString.length; + nanos = parseInt(nanoString, 10) * Math.pow(10, trailingZeroes); } if (isNaN(seconds) || isNaN(nanos)) { @@ -70,10 +67,7 @@ function convertTimestamp(timestampValue, argumentName) { ); } - timestampProto = { - seconds: seconds, - nanos: nanos, - }; + timestampProto = {seconds, nanos}; } else if (is.defined(timestampValue)) { validate.isObject('timestampValue', timestampValue); timestampProto = {