-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Preserve sub-second precision when serializing unixTimestamp parameters #10644
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
base: v2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "type": "bugfix", | ||
| "category": "Serialization", | ||
| "description": "Preserve sub-second precision when serializing ``unixTimestamp`` request parameters. Timestamps with a fractional component are now sent as a fractional value (e.g. ``1704110400.123456``) instead of being truncated to whole seconds. Whole-second timestamps are unchanged." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -143,7 +143,14 @@ def _timestamp_iso8601(self, value): | |
| return value.strftime(timestamp_format) | ||
|
|
||
| def _timestamp_unixtimestamp(self, value): | ||
| return int(calendar.timegm(value.timetuple())) | ||
| timestamp = calendar.timegm(value.timetuple()) | ||
| if value.microsecond > 0: | ||
| # Add the microseconds as integers before dividing so the float is only | ||
| # rounded once. Dividing first and then adding rounds twice, which can | ||
| # produce a slightly different value, e.g. 1 + 3691 / 10**6 gives | ||
| # 1.0036909999999999 instead of 1.003691. | ||
| return (timestamp * 10**6 + value.microsecond) / 10**6 | ||
| return timestamp | ||
|
|
||
| def _timestamp_rfc822(self, value): | ||
| if isinstance(value, datetime.datetime): | ||
|
|
@@ -617,22 +624,12 @@ def _serialize_type_timestamp(self, serialized, value, shape, key): | |
| tag = 1 # Use tag 1 for unix timestamp | ||
| initial_byte = self._get_initial_byte(self.TAG_MAJOR_TYPE, tag) | ||
| serialized.extend(initial_byte) # Tagging the timestamp | ||
| additional_info, num_bytes = self._get_additional_info_and_num_bytes( | ||
| timestamp | ||
| ) | ||
|
|
||
| if num_bytes == 0: | ||
| initial_byte = self._get_initial_byte( | ||
| self.UNSIGNED_INT_MAJOR_TYPE, timestamp | ||
| ) | ||
| serialized.extend(initial_byte) | ||
| # Tag 1 permits either an integer or a floating-point epoch seconds | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code seems fine. just for my understanding why did we have all the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The But the current behavior is incorrect since we can't serialize negative timestamps. A timestamp value like |
||
| # value; a float is used when sub-second precision is present. | ||
| if isinstance(timestamp, float): | ||
| self._serialize_type_double(serialized, timestamp, shape, key) | ||
| else: | ||
| initial_byte = self._get_initial_byte( | ||
| self.UNSIGNED_INT_MAJOR_TYPE, additional_info | ||
| ) | ||
| serialized.extend( | ||
| initial_byte + timestamp.to_bytes(num_bytes, "big") | ||
| ) | ||
| self._serialize_type_integer(serialized, timestamp, shape, key) | ||
|
|
||
| def _serialize_type_float(self, serialized, value, shape, key): | ||
| if self._is_special_number(value): | ||
|
|
||
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.
_timestamp_rfc822()also calls this method. Because the new result is a float (https://docs.python.org/3/tutorial/floatingpoint.html#floating-point-arithmetic-issues-and-limitations) wouldn't far-future timestamps round up by one second?