fix(cloud_firestore): correct nanoseconds calculation for pre-1970 dates#17195
Merged
fix(cloud_firestore): correct nanoseconds calculation for pre-1970 dates#17195
Conversation
m4tbat
reviewed
Mar 13, 2025
Comment on lines
-43
to
+51
| final int seconds = microseconds ~/ _kMillion; | ||
| final int nanoseconds = (microseconds - seconds * _kMillion) * _kThousand; | ||
| int seconds = microseconds ~/ _kMillion; | ||
| int nanoseconds = (microseconds - seconds * _kMillion) * _kThousand; | ||
|
|
||
| // Matches implementation in Android SDK: | ||
| // https://github.com/firebase/firebase-android-sdk/blob/master/firebase-common/src/main/java/com/google/firebase/Timestamp.kt#L114-L121 | ||
| if (nanoseconds < 0) { | ||
| seconds -= 1; | ||
| nanoseconds += _kBillion; | ||
| } |
There was a problem hiding this comment.
@SelaseKay
Also the factory fromMillisecondsSinceEpoch should be fixed.
I personally would delegate the milliseconds one to the microseconds one:
factory Timestamp.fromMillisecondsSinceEpoch(int milliseconds) {
return fromMicrosecondsSinceEpoch(milliseconds * _kThousand);
}but I don't know if there are other considerations to be done
Lyokone
reviewed
Mar 14, 2025
Comment on lines
+84
to
+101
| test('Timestamp should not throw for dates before 1970', () { | ||
| final dates = [ | ||
| DateTime(1969, 06, 22, 0, 0, 0, 123), | ||
| DateTime(1969, 12, 31, 23, 59, 59, 999), | ||
| DateTime(1900, 01, 01, 12, 30, 45, 500), | ||
| DateTime(1800, 07, 04, 18, 15, 30, 250), | ||
| DateTime(0001, 01, 01, 00, 00, 00, 001), | ||
| ]; | ||
|
|
||
| for (final date in dates) { | ||
| try { | ||
| final timestamp = Timestamp.fromDate(date); | ||
| expect(timestamp, isA<Timestamp>()); | ||
| } catch (e) { | ||
| fail('Timestamp.fromDate threw an error: $e'); | ||
| } | ||
| } | ||
| }); |
Contributor
There was a problem hiding this comment.
You should be able to go from Date to Timestamp to Date and check that the value is still correct.
You are missing the second half of the test.
You should also have the same test were you store it in Firestore and then read firestore to see if it was correctly stored.
Lyokone
approved these changes
Mar 14, 2025
|
@Lyokone notice that the factory |
MichaelVerdon
approved these changes
Mar 19, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
Fixes #17189
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]).This will ensure a smooth and quick review process. Updating the
pubspec.yamland changelogs is not required.///).melos run analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?