File tree 3 files changed +16
-2
lines changed
3 files changed +16
-2
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @firebase/firestore ' : patch
3
+ ---
4
+
5
+ Fixed a bug where decimal inputs to ` Timestamp.fromMillis() ` calculated incorrectly due to floating point precision loss
Original file line number Diff line number Diff line change @@ -21,6 +21,9 @@ import { primitiveComparator } from '../util/misc';
21
21
// The earliest date supported by Firestore timestamps (0001-01-01T00:00:00Z).
22
22
const MIN_SECONDS = - 62135596800 ;
23
23
24
+ // Number of nanoseconds in a millisecond.
25
+ const MS_TO_NANOS = 1e6 ;
26
+
24
27
/**
25
28
* A `Timestamp` represents a point in time independent of any time zone or
26
29
* calendar, represented as seconds and fractions of seconds at nanosecond
@@ -66,7 +69,7 @@ export class Timestamp {
66
69
*/
67
70
static fromMillis ( milliseconds : number ) : Timestamp {
68
71
const seconds = Math . floor ( milliseconds / 1000 ) ;
69
- const nanos = ( milliseconds - seconds * 1000 ) * 1e6 ;
72
+ const nanos = Math . floor ( ( milliseconds - seconds * 1000 ) * MS_TO_NANOS ) ;
70
73
return new Timestamp ( seconds , nanos ) ;
71
74
}
72
75
@@ -138,7 +141,7 @@ export class Timestamp {
138
141
* the number of milliseconds since Unix epoch 1970-01-01T00:00:00Z.
139
142
*/
140
143
toMillis ( ) : number {
141
- return this . seconds * 1000 + this . nanoseconds / 1e6 ;
144
+ return this . seconds * 1000 + this . nanoseconds / MS_TO_NANOS ;
142
145
}
143
146
144
147
_compareTo ( other : Timestamp ) : number {
Original file line number Diff line number Diff line change @@ -134,6 +134,12 @@ describe('Timestamp', () => {
134
134
expect ( t1 >= t2 ) . to . be . false ;
135
135
} ) ;
136
136
137
+ it ( 'handles decimal inputs in fromMillis()' , ( ) => {
138
+ const actual = Timestamp . fromMillis ( 1000.1 ) ;
139
+ const expected = new Timestamp ( 1 , 100000 ) ;
140
+ expect ( actual . isEqual ( expected ) ) . to . be . true ;
141
+ } ) ;
142
+
137
143
it ( 'serializes to JSON' , ( ) => {
138
144
expect ( new Timestamp ( 123 , 456 ) . toJSON ( ) ) . to . deep . equal ( {
139
145
seconds : 123 ,
You can’t perform that action at this time.
0 commit comments