From b6c1c9921651c608aad029f073d18fa336ab84d1 Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Fri, 8 Mar 2024 18:49:04 +0900 Subject: [PATCH 01/12] feat: Update Timestamp Constructor with nanoseconds --- .../lib/src/google_cloud_firestore/timestamp.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart index 5dcf7c1..dc20d43 100644 --- a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart +++ b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart @@ -16,7 +16,7 @@ String _toGoogleDateTime({required int seconds, required int nanoseconds}) { @immutable class Timestamp implements _Serializable { - Timestamp._({required this.seconds, required this.nanoseconds}) { + Timestamp({required this.seconds, required this.nanoseconds}) { const minSeconds = -62135596800; const maxSeconds = 253402300799; @@ -82,7 +82,7 @@ class Timestamp implements _Serializable { factory Timestamp.fromMillis(int milliseconds) { final seconds = (milliseconds / 1000).floor(); final nanos = (milliseconds - seconds * 1000) * _msToNanos; - return Timestamp._(seconds: seconds, nanoseconds: nanos); + return Timestamp(seconds: seconds, nanoseconds: nanos); } factory Timestamp._fromString(String timestampValue) { @@ -106,7 +106,7 @@ class Timestamp implements _Serializable { ); } - return Timestamp._( + return Timestamp( seconds: date.millisecondsSinceEpoch ~/ 1000, nanoseconds: nanos, ); From 3832b30594ecda709ae3d721af5361d291737a68 Mon Sep 17 00:00:00 2001 From: Kim Jiun <40026920+KKimj@users.noreply.github.com> Date: Fri, 12 Jul 2024 13:49:54 +0900 Subject: [PATCH 02/12] chore: Fix typo --- .../lib/src/google_cloud_firestore/timestamp.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart index ebe08c5..70ceede 100644 --- a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart +++ b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart @@ -14,6 +14,7 @@ String _toGoogleDateTime({required int seconds, required int nanoseconds}) { return '${formattedDate}Z'; } +@immutable final class Timestamp implements _Serializable { Timestamp._({required this.seconds, required this.nanoseconds}) { const minSeconds = -62135596800; From 0c85dde1f94aa0c6d35ce03df4e4b970e5582a17 Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Fri, 12 Jul 2024 18:43:45 +0900 Subject: [PATCH 03/12] chore: Fix typo --- .../lib/src/google_cloud_firestore/timestamp.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart index 70ceede..651f700 100644 --- a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart +++ b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart @@ -16,7 +16,7 @@ String _toGoogleDateTime({required int seconds, required int nanoseconds}) { @immutable final class Timestamp implements _Serializable { - Timestamp._({required this.seconds, required this.nanoseconds}) { + Timestamp({required this.seconds, required this.nanoseconds}) { const minSeconds = -62135596800; const maxSeconds = 253402300799; From 7a3e2b199e192214baf485c04928588aadf68191 Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Fri, 12 Jul 2024 19:00:05 +0900 Subject: [PATCH 04/12] chore: Add tests --- .../src/google_cloud_firestore/timestamp.dart | 11 ++++++++-- .../timestamp_test.dart | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart diff --git a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart index 651f700..5c7abe1 100644 --- a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart +++ b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart @@ -63,7 +63,7 @@ final class Timestamp implements _Serializable { /// Returns a new [Timestamp] representing the same point in time /// as the given date. factory Timestamp.fromDate(DateTime date) { - return Timestamp.fromMillis(date.millisecondsSinceEpoch); + return Timestamp.fromMicros(date.microsecondsSinceEpoch); } /// Creates a new timestamp from the given number of milliseconds. @@ -81,7 +81,13 @@ final class Timestamp implements _Serializable { /// as the given number of milliseconds. factory Timestamp.fromMillis(int milliseconds) { final seconds = (milliseconds / 1000).floor(); - final nanos = (milliseconds - seconds * 1000) * _msToNanos; + final nanos = (milliseconds - seconds * 1000 * 1000) * _msToNanos; + return Timestamp(seconds: seconds, nanoseconds: nanos); + } + + factory Timestamp.fromMicros(int microseconds) { + final seconds = (microseconds / 1000 / 1000).floor(); + final nanos = (microseconds - seconds * 1000 * 1000) * _usToNanos; return Timestamp(seconds: seconds, nanoseconds: nanos); } @@ -113,6 +119,7 @@ final class Timestamp implements _Serializable { } static const _msToNanos = 1000000; + static const _usToNanos = 1000; final int seconds; final int nanoseconds; diff --git a/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart b/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart new file mode 100644 index 0000000..6bf4ad6 --- /dev/null +++ b/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart @@ -0,0 +1,20 @@ +import 'package:dart_firebase_admin/firestore.dart'; +import 'package:test/test.dart'; + +void main() { + group('Timestamp', () { + test('constructor', () { + final now = DateTime.now().toUtc(); + final timestamp = Timestamp.fromDate(now); + final seconds = now.millisecondsSinceEpoch ~/ 1000; + final nanoseconds = + (now.microsecondsSinceEpoch - seconds * 1000 * 1000) * 1000; + expect(timestamp, Timestamp(seconds: seconds, nanoseconds: nanoseconds)); + }); + test('fromDate constructor', () { + final now = DateTime.now().toUtc(); + final timestamp = Timestamp.fromDate(now); + expect(timestamp.seconds, now.millisecondsSinceEpoch ~/ 1000); + }); + }); +} From 1cf1a87c56c8b4b8ffb784907428ae64e177bcf9 Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Sat, 13 Jul 2024 15:00:33 +0900 Subject: [PATCH 05/12] chore: update docstring --- .../src/google_cloud_firestore/timestamp.dart | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart index 5c7abe1..1b74ee3 100644 --- a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart +++ b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart @@ -14,6 +14,16 @@ String _toGoogleDateTime({required int seconds, required int nanoseconds}) { return '${formattedDate}Z'; } +/// A Timestamp represents a point in time independent of any time zone or calendar, +/// represented as seconds and fractions of seconds at nanosecond resolution in UTC +/// Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends +/// the Gregorian calendar backwards to year one. It is encoded assuming all minutes +/// are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second table +/// is needed for interpretation. Range is from 0001-01-01T00:00:00Z to +/// 9999-12-31T23:59:59.999999999Z. By restricting to that range, we ensure that we +/// can convert to and from RFC 3339 date strings. +/// +/// For more information, see [the reference timestamp definition](https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto) @immutable final class Timestamp implements _Serializable { Timestamp({required this.seconds, required this.nanoseconds}) { @@ -85,6 +95,19 @@ final class Timestamp implements _Serializable { return Timestamp(seconds: seconds, nanoseconds: nanos); } + /// Creates a new timestamp from the given number of microseconds. + /// + /// ```dart + /// final documentRef = firestore.doc('col/doc'); + /// + /// documentRef.set({ 'startTime': Timestamp.fromMicros(42) }); + /// ``` + /// + /// - [microseconds]: Number of microseconds since Unix epoch + /// 1970-01-01T00:00:00Z. + /// + /// Returns a new [Timestamp] representing the same point in time + /// as the given number of microseconds. factory Timestamp.fromMicros(int microseconds) { final seconds = (microseconds / 1000 / 1000).floor(); final nanos = (microseconds - seconds * 1000 * 1000) * _usToNanos; From 45e0adbafa1bfd42d8e062b25a3aebb36b56cb4f Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Tue, 16 Jul 2024 10:05:56 +0200 Subject: [PATCH 06/12] Changelog --- packages/dart_firebase_admin/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/dart_firebase_admin/CHANGELOG.md b/packages/dart_firebase_admin/CHANGELOG.md index dc6bf86..ead2b7b 100644 --- a/packages/dart_firebase_admin/CHANGELOG.md +++ b/packages/dart_firebase_admin/CHANGELOG.md @@ -2,6 +2,7 @@ - Fixes crash when updating users (thanks to @HeySreelal) - Marked various classes that cannot be extended as base/final. +- Added a default constructor on `Timestamp` (thanks to @KKimj) ## 0.3.1 From a8ee7e2e851b910ccf08b4934f70b55b1b0ccb0c Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Tue, 16 Jul 2024 10:07:28 +0200 Subject: [PATCH 07/12] Swap --- .../test/google_cloud_firestore/timestamp_test.dart | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart b/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart index 6bf4ad6..945459a 100644 --- a/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart +++ b/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart @@ -5,12 +5,16 @@ void main() { group('Timestamp', () { test('constructor', () { final now = DateTime.now().toUtc(); - final timestamp = Timestamp.fromDate(now); final seconds = now.millisecondsSinceEpoch ~/ 1000; final nanoseconds = (now.microsecondsSinceEpoch - seconds * 1000 * 1000) * 1000; - expect(timestamp, Timestamp(seconds: seconds, nanoseconds: nanoseconds)); + + expect( + Timestamp(seconds: seconds, nanoseconds: nanoseconds), + Timestamp.fromDate(now), + ); }); + test('fromDate constructor', () { final now = DateTime.now().toUtc(); final timestamp = Timestamp.fromDate(now); From 8dbebc4824583a36981451167d74e8b59dfc0218 Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Tue, 16 Jul 2024 10:09:19 +0200 Subject: [PATCH 08/12] Format --- .../lib/src/google_cloud_firestore/timestamp.dart | 2 ++ .../test/google_cloud_firestore/timestamp_test.dart | 1 + 2 files changed, 3 insertions(+) diff --git a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart index 1b74ee3..d7cbb47 100644 --- a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart +++ b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart @@ -92,6 +92,7 @@ final class Timestamp implements _Serializable { factory Timestamp.fromMillis(int milliseconds) { final seconds = (milliseconds / 1000).floor(); final nanos = (milliseconds - seconds * 1000 * 1000) * _msToNanos; + return Timestamp(seconds: seconds, nanoseconds: nanos); } @@ -111,6 +112,7 @@ final class Timestamp implements _Serializable { factory Timestamp.fromMicros(int microseconds) { final seconds = (microseconds / 1000 / 1000).floor(); final nanos = (microseconds - seconds * 1000 * 1000) * _usToNanos; + return Timestamp(seconds: seconds, nanoseconds: nanos); } diff --git a/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart b/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart index 945459a..1ade9ae 100644 --- a/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart +++ b/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart @@ -18,6 +18,7 @@ void main() { test('fromDate constructor', () { final now = DateTime.now().toUtc(); final timestamp = Timestamp.fromDate(now); + expect(timestamp.seconds, now.millisecondsSinceEpoch ~/ 1000); }); }); From 8a01ad4b7ae708d938a1c3027f3379ac093a1596 Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Thu, 18 Jul 2024 11:42:41 +0900 Subject: [PATCH 09/12] fix: Fix error with Timestamp.fromMillis --- .../lib/src/google_cloud_firestore/timestamp.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart index d7cbb47..44cd7d4 100644 --- a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart +++ b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart @@ -91,7 +91,7 @@ final class Timestamp implements _Serializable { /// as the given number of milliseconds. factory Timestamp.fromMillis(int milliseconds) { final seconds = (milliseconds / 1000).floor(); - final nanos = (milliseconds - seconds * 1000 * 1000) * _msToNanos; + final nanos = (milliseconds - seconds * 1000) * _msToNanos; return Timestamp(seconds: seconds, nanoseconds: nanos); } From 1be3baa01d96abf16ebb1cff4b6553733ac4c9af Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Thu, 18 Jul 2024 11:42:55 +0900 Subject: [PATCH 10/12] chore: Add tests --- .../timestamp_test.dart | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart b/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart index 1ade9ae..265c2e1 100644 --- a/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart +++ b/packages/dart_firebase_admin/test/google_cloud_firestore/timestamp_test.dart @@ -21,5 +21,27 @@ void main() { expect(timestamp.seconds, now.millisecondsSinceEpoch ~/ 1000); }); + + test('fromMillis constructor', () { + final now = DateTime.now().toUtc(); + final timestamp = Timestamp.fromMillis(now.millisecondsSinceEpoch); + + expect(timestamp.seconds, now.millisecondsSinceEpoch ~/ 1000); + expect( + timestamp.nanoseconds, + (now.millisecondsSinceEpoch % 1000) * (1000 * 1000), + ); + }); + + test('fromMicros constructor', () { + final now = DateTime.now().toUtc(); + final timestamp = Timestamp.fromMicros(now.microsecondsSinceEpoch); + + expect(timestamp.seconds, now.microsecondsSinceEpoch ~/ (1000 * 1000)); + expect( + timestamp.nanoseconds, + (now.microsecondsSinceEpoch % (1000 * 1000)) * 1000, + ); + }); }); } From 8bc4fe75482f96b8a712ba6127745b61f4642416 Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Thu, 18 Jul 2024 11:45:57 +0900 Subject: [PATCH 11/12] chore: Remove white-space --- .../lib/src/google_cloud_firestore/timestamp.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart index 44cd7d4..b54989f 100644 --- a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart +++ b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart @@ -92,7 +92,6 @@ final class Timestamp implements _Serializable { factory Timestamp.fromMillis(int milliseconds) { final seconds = (milliseconds / 1000).floor(); final nanos = (milliseconds - seconds * 1000) * _msToNanos; - return Timestamp(seconds: seconds, nanoseconds: nanos); } @@ -112,7 +111,6 @@ final class Timestamp implements _Serializable { factory Timestamp.fromMicros(int microseconds) { final seconds = (microseconds / 1000 / 1000).floor(); final nanos = (microseconds - seconds * 1000 * 1000) * _usToNanos; - return Timestamp(seconds: seconds, nanoseconds: nanos); } From 0bb4ee2c6bb8632de9a067999733ee97bbbdf9ab Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Thu, 18 Jul 2024 11:47:19 +0900 Subject: [PATCH 12/12] chore: Restore white-space --- .../lib/src/google_cloud_firestore/timestamp.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart index b54989f..44cd7d4 100644 --- a/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart +++ b/packages/dart_firebase_admin/lib/src/google_cloud_firestore/timestamp.dart @@ -92,6 +92,7 @@ final class Timestamp implements _Serializable { factory Timestamp.fromMillis(int milliseconds) { final seconds = (milliseconds / 1000).floor(); final nanos = (milliseconds - seconds * 1000) * _msToNanos; + return Timestamp(seconds: seconds, nanoseconds: nanos); } @@ -111,6 +112,7 @@ final class Timestamp implements _Serializable { factory Timestamp.fromMicros(int microseconds) { final seconds = (microseconds / 1000 / 1000).floor(); final nanos = (microseconds - seconds * 1000 * 1000) * _usToNanos; + return Timestamp(seconds: seconds, nanoseconds: nanos); }