From e0e745dd153b8ba0d212c2e202e4055972d94113 Mon Sep 17 00:00:00 2001 From: Brian Chen Date: Thu, 8 Apr 2021 10:48:31 -0500 Subject: [PATCH 1/3] fix: use BigInt when calculating nanos in Timestamp.fromMillis() --- dev/src/timestamp.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dev/src/timestamp.ts b/dev/src/timestamp.ts index 926b4c256..e3774e326 100644 --- a/dev/src/timestamp.ts +++ b/dev/src/timestamp.ts @@ -107,9 +107,9 @@ export class Timestamp implements firestore.Timestamp { */ static fromMillis(milliseconds: number): Timestamp { const seconds = Math.floor(milliseconds / 1000); - const nanos = Math.floor( - milliseconds * MS_TO_NANOS - seconds * 1000 * MS_TO_NANOS - ); + // Use BigInt to avoid floating point precision loss. + const bigIntNanos = BigInt(milliseconds * MS_TO_NANOS) - BigInt(seconds * 1000 * MS_TO_NANOS); + const nanos = Math.floor(Number(bigIntNanos)); return new Timestamp(seconds, nanos); } From 837eee087e87d35cda580f27cd696f01709d5cf7 Mon Sep 17 00:00:00 2001 From: Brian Chen Date: Thu, 8 Apr 2021 10:50:28 -0500 Subject: [PATCH 2/3] lint --- dev/src/timestamp.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dev/src/timestamp.ts b/dev/src/timestamp.ts index e3774e326..40ce6dac4 100644 --- a/dev/src/timestamp.ts +++ b/dev/src/timestamp.ts @@ -108,7 +108,8 @@ export class Timestamp implements firestore.Timestamp { static fromMillis(milliseconds: number): Timestamp { const seconds = Math.floor(milliseconds / 1000); // Use BigInt to avoid floating point precision loss. - const bigIntNanos = BigInt(milliseconds * MS_TO_NANOS) - BigInt(seconds * 1000 * MS_TO_NANOS); + const bigIntNanos = + BigInt(milliseconds * MS_TO_NANOS) - BigInt(seconds * 1000 * MS_TO_NANOS); const nanos = Math.floor(Number(bigIntNanos)); return new Timestamp(seconds, nanos); } From 09f685a23b8e8dfe4d7ed482a56e12b7c3aeadf3 Mon Sep 17 00:00:00 2001 From: Brian Chen Date: Thu, 8 Apr 2021 16:23:49 -0500 Subject: [PATCH 3/3] match web sdk with mathh.floor() --- dev/src/timestamp.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/dev/src/timestamp.ts b/dev/src/timestamp.ts index 40ce6dac4..69cb98e8e 100644 --- a/dev/src/timestamp.ts +++ b/dev/src/timestamp.ts @@ -107,10 +107,7 @@ export class Timestamp implements firestore.Timestamp { */ static fromMillis(milliseconds: number): Timestamp { const seconds = Math.floor(milliseconds / 1000); - // Use BigInt to avoid floating point precision loss. - const bigIntNanos = - BigInt(milliseconds * MS_TO_NANOS) - BigInt(seconds * 1000 * MS_TO_NANOS); - const nanos = Math.floor(Number(bigIntNanos)); + const nanos = Math.floor((milliseconds - seconds * 1000) * MS_TO_NANOS); return new Timestamp(seconds, nanos); }