Skip to content

Commit

Permalink
Accept timestamp with missing nanoseconds (#41)
Browse files Browse the repository at this point in the history
This ports firebase/firebase-functions@0647a82 back to the GCloud SDK
  • Loading branch information
schmidt-sebastian authored Oct 24, 2017
1 parent 121edae commit c54030f
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 = {
Expand Down

0 comments on commit c54030f

Please sign in to comment.