fix(fcm): encode durations without floating-point rounding errors - #993
Open
Shubham-Padkonde wants to merge 2 commits into
Open
Shubham-Padkonde wants to merge 2 commits into
Shubham-Padkonde wants to merge 2 commits into
Conversation
encode_ttl and encode_milliseconds computed the nanoseconds from the float returned by timedelta.total_seconds(), so some durations were sent with rounding errors: an AndroidConfig.ttl of 86400.9 seconds (or timedelta(days=1, microseconds=900000)) was encoded as "86400.899999999s", and 1.123457 as "1.123456999s". The Duration string is now built from the exact days/seconds/microseconds of the timedelta.
There was a problem hiding this comment.
Code Review
This pull request refactors duration encoding in firebase_admin/_messaging_encoder.py by introducing an encode_duration helper method. This method uses exact integer fields of datetime.timedelta to avoid floating-point precision issues associated with total_seconds(). Corresponding test cases have been added to tests/test_messaging.py. The feedback suggests using Python's f-string integer formatting {nanos:09d} instead of str(nanos).zfill(9) for cleaner and more idiomatic code.
Comment on lines
302
to
303
| if nanos: | ||
| return f'{seconds}.{str(nanos).zfill(9)}s' |
There was a problem hiding this comment.
This branch has not been deployed
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
encode_ttl and encode_milliseconds computed the nanoseconds from the float returned by timedelta.total_seconds(), so some durations were sent with rounding errors: an AndroidConfig.ttl of 86400.9 seconds (or timedelta(days=1, microseconds=900000)) was encoded as "86400.899999999s", and 1.123457 as "1.123456999s". The Duration string is now built from the exact days/seconds/microseconds of the timedelta.
Hey there! So you want to contribute to a Firebase SDK?
Before you file this pull request, please read these guidelines:
Discussion
If not, go file an issue about this before creating a pull request to discuss.
Testing
API Changes
us make Firebase APIs better, please propose your change in an issue so that we
can discuss it together.