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

Timestamp values: Remove the use of "naive" Python datetime objects #667

Merged
merged 1 commit into from
Nov 4, 2024

Conversation

amotl
Copy link
Member

@amotl amotl commented Nov 1, 2024

About

Python: Remove invocations to deprecated datetime.utcfromtimestamp().
World: Make datetime handling like it's 2024.

Summary

This is a possible BREAKING CHANGE about returned Python datetime objects:

Removed the use of "naive" Python datetime objects, i.e. instances without tzinfo attribute set.

Details

When no time_zone information is specified when creating a database connection or cursor, datetime objects will now use Coordinated Universal Time (UTC), like CrateDB is storing timestamp values in this format.

This update is coming from a deprecation of Python's datetime.utcfromtimestamp(), which is effectively also phasing out the use of "naive" timestamp objects in Python, in favor of using timezone-aware objects, also to represent datetimes in UTC. It may be a breaking change for some users of the library that don't expect to receive "aware" datetime objects from now on.

DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).

@cla-bot cla-bot bot added the cla-signed label Nov 1, 2024
@amotl amotl force-pushed the remove-utcfromtimestamp branch from dda9da9 to c7055bf Compare November 1, 2024 15:08
@amotl amotl requested review from simonprickett and kneth November 1, 2024 15:38
@amotl amotl marked this pull request as ready for review November 1, 2024 15:39
Python: Remove invocations to deprecated `datetime.utcfromtimestamp()`.

This is a possible BREAKING CHANGE about returned Python ``datetime``
objects:

> Removed the use of "naive" Python ``datetime`` objects, i.e. instances
without ``tzinfo`` attribute set.

When no ``time_zone`` information is specified when creating a database
connection or cursor, ``datetime`` objects will now use Coordinated
Universal Time (UTC), like CrateDB is storing timestamp values in this
format.

This update is coming from a deprecation of Python's
``datetime.utcfromtimestamp()``, which is effectively also phasing out
the use of "naive" timestamp objects in Python, in favor of using
timezone-aware objects, also to represent datetimes in UTC. It may be a
breaking change for some users of the library that don't expect to
receive "aware" ``datetime`` objects from now on.

DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
"""
https://docs.python.org/3/library/datetime.html
"""
if value is None:
return None
return datetime.utcfromtimestamp(value / 1e3)
return dt.datetime.fromtimestamp(value / 1e3, tz=dt.timezone.utc)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be faster to value * 1e-3?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk.

tests/client/test_cursor.py Show resolved Hide resolved
@amotl amotl merged commit cea5958 into master Nov 4, 2024
17 checks passed
@amotl amotl deleted the remove-utcfromtimestamp branch November 4, 2024 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants