Title: Fix Timestamp.from_datetime() precision for far-future datetimes by gaoflow · Pull Request #710 · msgpack/msgpack-python · GitHub
Open Graph Title: Fix Timestamp.from_datetime() precision for far-future datetimes by gaoflow · Pull Request #710 · msgpack/msgpack-python
X Title: Fix Timestamp.from_datetime() precision for far-future datetimes by gaoflow · Pull Request #710 · msgpack/msgpack-python
Description: Timestamp.from_datetime() computes the whole-second part from the float datetime.timestamp(). A float64 cannot hold microsecond precision for datetimes far from the epoch, so timestamp() rounds the seconds up while the exact microsecond is still used for the nanoseconds. The result is a Timestamp one second in the future, and near datetime.max it raises OverflowError: >>> import datetime as dt >>> from msgpack.ext import Timestamp >>> d = dt.datetime(3000, 1, 1, 0, 0, 0, 999999, tzinfo=dt.timezone.utc) >>> Timestamp.from_datetime(d).to_datetime() datetime.datetime(3000, 1, 1, 0, 0, 1, 999999, tzinfo=datetime.timezone.utc) # +1 second >>> Timestamp.from_datetime(dt.datetime(9999, 12, 31, 23, 59, 59, 999999, tzinfo=dt.timezone.utc)) OverflowError: date value out of range The Cython packer already uses integer timedelta arithmetic and is correct. This makes the pure-Python from_datetime() do the same, so the two paths agree and the round-trip invariant from_datetime(d).to_datetime() == d holds for far-future datetimes. Naive datetimes keep their existing local-time interpretation (matching datetime.timestamp()). Follow-up to #662, which fixed the pre-epoch rounding direction in the same method. Existing tests are unchanged; I added a regression test covering the far-future round-trip, the exact seconds/nanoseconds, agreement with packing the datetime directly, and the former OverflowError case. Full suite is green on both the C-extension and pure-Python paths.
Open Graph Description: Timestamp.from_datetime() computes the whole-second part from the float datetime.timestamp(). A float64 cannot hold microsecond precision for datetimes far from the epoch, so timestamp() rounds the...
X Description: Timestamp.from_datetime() computes the whole-second part from the float datetime.timestamp(). A float64 cannot hold microsecond precision for datetimes far from the epoch, so timestamp() rounds the...
Opengraph URL: https://github.com/msgpack/msgpack-python/pull/710
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:40c3e635-bcc5-6c1d-d640-f28afe7fe89b |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A58C:A4628:6ADFCA:9745B9:6A4E7302 |
| html-safe-nonce | 44fa0804115fc5f4bd2a0bf3a737d1edbde48920ba65f0ab268f7794d75d0183 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNThDOkE0NjI4OjZBREZDQTo5NzQ1Qjk6NkE0RTczMDIiLCJ2aXNpdG9yX2lkIjoiNjA5NzgyMDkxODgyMTU4MTU3MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8de9fdc39364e6db6ef7cb6f6bb2465fa8d072395d70f90e7e9b6830ccbe5171 |
| hovercard-subject-tag | pull_request:3985661475 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/msgpack/msgpack-python/pull/710/files |
| twitter:image | https://avatars.githubusercontent.com/u/3355843?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/3355843?s=400&v=4 |
| og:image:alt | Timestamp.from_datetime() computes the whole-second part from the float datetime.timestamp(). A float64 cannot hold microsecond precision for datetimes far from the epoch, so timestamp() rounds the... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 41b6ab3ba6d20a71766ac245b5a4a94c6fc672a9cd4da7d44c1b33ab8bf6a21c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/msgpack/msgpack-python git https://github.com/msgpack/msgpack-python.git |
| octolytics-dimension-user_id | 198264 |
| octolytics-dimension-user_login | msgpack |
| octolytics-dimension-repository_id | 2242705 |
| octolytics-dimension-repository_nwo | msgpack/msgpack-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 2242705 |
| octolytics-dimension-repository_network_root_nwo | msgpack/msgpack-python |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e6a744804e8e70f97b4d5a18a94dcc63db22f97a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width