ydb-platform / ydb-sqlalchemy

YQL Dialect for SQLAlchemy
Apache License 2.0
20 stars 6 forks source link

Add support of timezone #42

Closed LuckySting closed 4 months ago

LuckySting commented 4 months ago

Problem

SQLAlchemy provides a mechanism to work with timezones. For instance, user may specify column as DateTime(timezone=True), which means that it stores a timezone-aware datetime.

Solution

Since the YDB doesn't support storing of timezone-aware datetime in tables, I propose a workaround converting all datetime for timezone=True columns to UTC. So an application may work with timezone-aware datetime, which is a best practice.

Example

table = sa.Table(
    "users",
    sa.MetaData(),
    sa.Column("id", sa.Unicode, primary_key=True),
    sa.Column("created_at", sa.DateTime(timezone=True)),
)

user = sa.insert(table).values({"id": 1, "crated_at": datetime(2024, 5, 10, 15, 32, tzinfo=timezone(timedelta(hours=4)))})

user = sa.select(table) # created_at == datetime(2024, 5, 10, 11, 32, tzinfo=timezone.utc)