Skip to content

Commit 4c40084

Browse files
committed
Preserve nano resolution when converting to ISO string.
1 parent badd089 commit 4c40084

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

packages/firestore/src/api/timestamp.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ export class Timestamp {
8383
);
8484
}
8585

86+
_toISOString(): string {
87+
const strMillis = this.toDate().toISOString(); // JS Date only has millis.
88+
// So let's replace the fraction w/ up to 9 digits (nano resolution).
89+
const fracs = (this.nanoseconds / 1e9).toFixed(9).substr(1); // .123456789
90+
const strNanos = strMillis.replace(/\.\d*/, '').replace('Z', fracs + 'Z');
91+
return strNanos;
92+
}
93+
8694
toString(): string {
8795
return (
8896
'Timestamp(seconds=' +

packages/firestore/src/remote/serializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export class JsonProtoSerializer {
208208
*/
209209
private toTimestamp(timestamp: Timestamp): string {
210210
if (this.options.useProto3Json) {
211-
return timestamp.toDate().toISOString();
211+
return timestamp._toISOString();
212212
} else {
213213
return {
214214
seconds: '' + timestamp.seconds,

packages/firestore/test/unit/api/timestamp.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ describe('Timestamp', () => {
3333
nanoseconds: 750000000
3434
});
3535
});
36+
37+
it('_toISOString', () => {
38+
expect(new Timestamp(0, 1001)._toISOString()).to.equal(
39+
'1970-01-01T00:00:00.000001001Z');
40+
expect(new Timestamp(0, 1000)._toISOString()).to.equal(
41+
'1970-01-01T00:00:00.000001000Z');
42+
expect(new Timestamp(1570664492, 537000000)._toISOString()).to.equal(
43+
'2019-10-09T23:41:32.537000000Z');
44+
});
3645
});

0 commit comments

Comments
 (0)