Skip to content

Commit 0ee5ab1

Browse files
authored
fix(firestore): add timestamp tests & fix (#2945)
1 parent 93f3543 commit 0ee5ab1

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/timestamp.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class Timestamp implements Comparable<Timestamp> {
3131

3232
/// Create a [Timestamp] fromMillisecondsSinceEpoch
3333
factory Timestamp.fromMillisecondsSinceEpoch(int milliseconds) {
34-
final int seconds = (milliseconds / _kThousand).floor();
34+
final int seconds = (milliseconds ~/ _kThousand).floor();
3535
final int nanoseconds = (milliseconds - seconds * _kThousand) * _kMillion;
3636
return Timestamp(seconds, nanoseconds);
3737
}
3838

3939
/// Create a [Timestamp] fromMicrosecondsSinceEpoch
4040
factory Timestamp.fromMicrosecondsSinceEpoch(int microseconds) {
41-
final int seconds = (microseconds / _kMillion).floor();
41+
final int seconds = (microseconds ~/ _kMillion).floor();
4242
final int nanoseconds = (microseconds - seconds * _kMillion) * _kThousand;
4343
return Timestamp(seconds, nanoseconds);
4444
}
@@ -68,11 +68,11 @@ class Timestamp implements Comparable<Timestamp> {
6868

6969
// ignore: public_member_api_docs
7070
int get millisecondsSinceEpoch =>
71-
(seconds * _kThousand + nanoseconds / _kMillion).floor();
71+
(seconds * _kThousand + nanoseconds ~/ _kMillion);
7272

7373
// ignore: public_member_api_docs
7474
int get microsecondsSinceEpoch =>
75-
(seconds * _kMillion + nanoseconds / _kThousand).floor();
75+
(seconds * _kMillion + nanoseconds ~/ _kThousand);
7676

7777
/// Converts [Timestamp] to [DateTime]
7878
DateTime toDate() {
@@ -81,9 +81,11 @@ class Timestamp implements Comparable<Timestamp> {
8181

8282
@override
8383
int get hashCode => hashValues(seconds, nanoseconds);
84+
8485
@override
8586
bool operator ==(dynamic o) =>
8687
o is Timestamp && o.seconds == seconds && o.nanoseconds == nanoseconds;
88+
8789
@override
8890
int compareTo(Timestamp other) {
8991
if (seconds == other.seconds) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2020, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
8+
const int _kBillion = 1000000000;
9+
const int _kStartOfTime = -62135596800;
10+
const int _kEndOfTime = 253402300800;
11+
12+
void main() {
13+
group('$Timestamp', () {
14+
test('equality', () {
15+
expect(Timestamp(0, 0), equals(Timestamp(0, 0)));
16+
expect(Timestamp(123, 456), equals(Timestamp(123, 456)));
17+
});
18+
19+
test('validation', () {
20+
expect(() => Timestamp(0, -1), throwsArgumentError);
21+
expect(() => Timestamp(0, _kBillion + 1), throwsArgumentError);
22+
expect(() => Timestamp(_kStartOfTime - 1, 123), throwsArgumentError);
23+
expect(() => Timestamp(_kEndOfTime + 1, 123), throwsArgumentError);
24+
});
25+
26+
test('returns properties', () {
27+
Timestamp t = Timestamp(123, 456);
28+
expect(t.seconds, equals(123));
29+
expect(t.nanoseconds, equals(456));
30+
});
31+
32+
// https://github.com/FirebaseExtended/flutterfire/issues/1222
33+
test('does not exceed range', () {
34+
Timestamp maxTimestamp = Timestamp(_kEndOfTime - 1, _kBillion - 1);
35+
Timestamp.fromMicrosecondsSinceEpoch(maxTimestamp.microsecondsSinceEpoch);
36+
});
37+
});
38+
}

0 commit comments

Comments
 (0)