Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion spec/providers/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ describe('Firestore Functions', () => {
expect(snapshot.data()['referenceVal'].path).to.equal('doc1/id');
});

it('should parse timestamp values', () => {
it('should parse timestamp values with precision to the millisecond', () => {
let raw = constructValue({
'timestampVal': {
'timestampValue': '2017-06-13T00:58:40.349Z',
Expand All @@ -307,6 +307,19 @@ describe('Firestore Functions', () => {
expect(snapshot.data()).to.deep.equal({'timestampVal': new Date('2017-06-13T00:58:40.349Z')});
});

it('should parse timestamp values with precision to the second', () => {
let raw = constructValue({
'timestampVal': {
'timestampValue': '2017-06-13T00:58:40Z',
},
});
let snapshot = firestore.dataConstructor({
data: { value: raw },
});
expect(snapshot.data()).to.deep.equal({'timestampVal': new Date('2017-06-13T00:58:40Z')});

});

it('should parse binary values', () => {
// Format defined in https://developers.google.com/discovery/v1/type-format
let raw = constructValue({
Expand Down
17 changes: 6 additions & 11 deletions src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ export function dateToTimestampProto(timeString) {
}
let date = new Date(timeString);
let seconds = Math.floor(date.getTime() / 1000);
let nanoString = timeString.substring(20, timeString.length - 1);

if (nanoString.length === 3) {
nanoString = `${nanoString}000000`;
} else if (nanoString.length === 6) {
nanoString = `${nanoString}000`;
let nanos = 0;
if (timeString.length > 20) {
const nanoString = timeString.substring(20, timeString.length - 1);
const trailingZeroes = 9 - nanoString.length;
nanos = parseInt(nanoString, 10) * Math.pow(10, trailingZeroes);
}
let proto = {
seconds: seconds,
nanos: parseInt(nanoString, 10),
};
return proto;
return { seconds, nanos };
};