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

Return TIMESTAMP types as timezone-aware native Python datetime objects #445

Merged
merged 1 commit into from
Dec 8, 2022

Conversation

amotl
Copy link
Member

@amotl amotl commented Sep 16, 2022

About

When requested, make the driver return timezone-aware native Python datetime objects for CrateDB's TIMESTAMP types. The user can easily supply the desired time zone by different means.

Rationale

@mfussenegger asked at #442 (review):

I think something common like setting the timezone for datetime objects could be made even easier for users.

I shared some initial thoughts about it at #442 (review) ff., and finally settled with this implementation, separately from the baseline implementation for the data type converter.

Details

This patch adds additional convenience based on the data type converter machinery added with #442.

  • A time_zone keyword argument can be passed to both the connect() method, or when creating new Cursor objects.
  • The time_zone attribute can also be changed at runtime on both the connection and cursor object instances.

Different ways to populate the time_zone value are supported. Some examples::

  • datetime.timezone.utc
  • datetime.timezone(datetime.timedelta(hours=7), name="MST")
  • pytz.timezone("Australia/Sydney")
  • zoneinfo.ZoneInfo("Australia/Sydney")
  • +0530 (UTC offset in string format)

Acknowledgements

This interface was inspired by the DBAPI drivers for MySQL and Elasticsearch. Thanks.

Synopsis

Use time_zone argument when creating a new connection. Here, use a pytz.timezone object.

>>> import pytz
>>> from crate.client import connect

>>> connect("localhost:4200", time_zone=pytz.timezone("Australia/Sydney"))
>>> cursor = connection.cursor()
>>> cursor.execute(stmt)
>>> cursor.fetchone()
['foo', datetime.datetime(2022, 7, 19, 4, 10, 36, 758000, tzinfo=<DstTzInfo 'Australia/Sydney' AEST+10:00:00 STD>)]

Use time_zone argument when creating a new cursor. Here, use an UTC offset string.

>>> from crate.client import connect

>>> connection = connect("localhost:4200")
>>> cursor = connection.cursor(time_zone="+0530")
>>> cursor.execute(stmt)
>>> cursor.fetchone()
['foo', datetime.datetime(2022, 7, 18, 23, 40, 36, 758000, tzinfo=datetime.timezone(datetime.timedelta(seconds=19800), '+0530'))]

References

Backlog

@amotl amotl changed the title Optionally return TIMESTAMP types as timezone-aware native Python datetime objects Return TIMESTAMP types as timezone-aware native Python datetime objects Sep 16, 2022
@amotl amotl force-pushed the amo/timezone-aware-datetimes branch from c2c0bfc to b8bc5e7 Compare September 16, 2022 18:08
Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

Thank you! It looks good, I have some comments around docs.

src/crate/client/doctests/cursor.txt Outdated Show resolved Hide resolved
src/crate/client/connection.py Show resolved Hide resolved
src/crate/client/cursor.py Show resolved Hide resolved
src/crate/client/connection.py Show resolved Hide resolved
Copy link
Member Author

@amotl amotl left a comment

Choose a reason for hiding this comment

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

Dear Marios,

I've added some commits and hopefully addressed your suggestions well. Please let me know about anything missing, or where the patch could be improved further.

With kind regards,
Andreas.

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

LGTM. thank you! Left a comment about a test method name.

src/crate/client/test_connection.py Show resolved Hide resolved
@amotl amotl force-pushed the amo/timezone-aware-datetimes branch from 349b92e to 4c0fcb9 Compare October 4, 2022 11:19
@amotl amotl marked this pull request as ready for review October 4, 2022 11:21
@amotl amotl changed the title Return TIMESTAMP types as timezone-aware native Python datetime objects [STACK] Return TIMESTAMP types as timezone-aware native Python datetime objects Oct 13, 2022
@amotl amotl marked this pull request as draft October 13, 2022 19:12
Base automatically changed from amo/type-converter to master October 18, 2022 17:53
@amotl amotl force-pushed the amo/timezone-aware-datetimes branch from 4c0fcb9 to 9ef9d61 Compare October 18, 2022 18:39
@amotl amotl changed the title [STACK] Return TIMESTAMP types as timezone-aware native Python datetime objects Return TIMESTAMP types as timezone-aware native Python datetime objects Oct 18, 2022
@amotl amotl force-pushed the amo/timezone-aware-datetimes branch 5 times, most recently from 135caf7 to eba6f89 Compare October 18, 2022 19:38
@amotl amotl marked this pull request as ready for review October 18, 2022 20:23
@amotl amotl force-pushed the amo/timezone-aware-datetimes branch 2 times, most recently from 98630d0 to 48d8bd7 Compare November 21, 2022 16:27
@amotl amotl force-pushed the amo/timezone-aware-datetimes branch from 48d8bd7 to 77dc5df Compare December 2, 2022 19:46
@amotl amotl requested a review from hammerhead December 2, 2022 19:47
This is additional API convenience based on the data type converter
machinery. A `time_zone` keyword argument can be passed to both the
`connect()` method, or when creating new `Cursor` objects.

The `time_zone` attribute can also be changed at runtime on both the
`connection` and `cursor` object instances.

Examples:

- connect('localhost:4200', time_zone=pytz.timezone("Australia/Sydney"))
- connection.cursor(time_zone="+0530")
@amotl amotl force-pushed the amo/timezone-aware-datetimes branch from 77dc5df to 8d8a6d1 Compare December 8, 2022 15:36
@amotl amotl merged commit 8e7ed99 into master Dec 8, 2022
@amotl amotl deleted the amo/timezone-aware-datetimes branch December 8, 2022 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants