Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

SelaseKay
Copy link
Contributor

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.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

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;
}
Copy link

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

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');
}
}
});
Copy link
Contributor

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.

@m4tbat
Copy link

m4tbat commented Mar 14, 2025

@Lyokone notice that the factory fromMillisecondsSinceEpoch still isn't fixed though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants