-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(cloud_firestore): correct nanoseconds calculation for pre-1970 dates #17195
base: main
Are you sure you want to change the base?
Conversation
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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
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'); | ||
} | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 notice that the factory |
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.yaml
and 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?