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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ void runTimestampTests() {
);
});

test('implicitly converts a DateTime without losing microseconds',
() async {
final doc = await initializeTest('datetime-microseconds');
final date = DateTime.utc(2023, 11, 1, 0, 0, 0, 999, 999);

await doc.set(<String, Object?>{'foo': date});

final snapshot = await doc.get();
final timestamp = snapshot.data()!['foo'] as Timestamp;

expect(timestamp, Timestamp.fromDate(date));
expect(timestamp.microsecondsSinceEpoch, date.microsecondsSinceEpoch);
});

test('updates a $Timestamp & returns', () async {
DocumentReference<Map<String, dynamic>> doc =
await initializeTest('geo-point-update');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ class FirestoreMessageCodec extends StandardMessageCodec {
@override
void writeValue(WriteBuffer buffer, dynamic value) {
if (value is DateTime) {
buffer.putUint8(_kDateTime);
buffer.putInt64(value.millisecondsSinceEpoch);
final Timestamp timestamp = Timestamp.fromDate(value);
buffer.putUint8(_kTimestamp);
buffer.putInt64(timestamp.seconds);
buffer.putInt32(timestamp.nanoseconds);
} else if (value is Timestamp) {
buffer.putUint8(_kTimestamp);
buffer.putInt64(value.seconds);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2026, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';
import 'package:flutter_test/flutter_test.dart';

import 'utils/test_firestore_message_codec.dart';

void main() {
const TestFirestoreMessageCodec codec = TestFirestoreMessageCodec();

test('encodes DateTime without losing microseconds', () {
final dates = <DateTime>[
DateTime.utc(2023, 11, 1, 0, 0, 0, 0, 1),
DateTime.utc(2023, 11, 1, 0, 0, 0, 1, 1),
DateTime.utc(2023, 11, 1, 0, 0, 0, 999, 999),
DateTime.utc(1969, 12, 31, 23, 59, 59, 999, 999),
];

for (final date in dates) {
final encoded = codec.encodeMessage(date);
final decoded = codec.decodeMessage(encoded);

expect(decoded, Timestamp.fromDate(date));
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,18 @@ JSAny? jsify(Object? dartObject) {
}

if (dartObject is DateTime) {
return TimestampJsImpl.fromMillis(dartObject.millisecondsSinceEpoch.toJS)
as JSAny;
final timestamp = Timestamp.fromDate(dartObject);
return TimestampJsImpl(
timestamp.seconds.toJS,
timestamp.nanoseconds.toJS,
) as JSAny;
}

if (dartObject is Timestamp) {
return TimestampJsImpl.fromMillis(dartObject.millisecondsSinceEpoch.toJS)
as JSAny;
return TimestampJsImpl(
dartObject.seconds.toJS,
dartObject.nanoseconds.toJS,
) as JSAny;
}

if (dartObject is DocumentReference) {
Expand Down
Loading