-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement utcnow() function for consistent UTC timestamps and update …
…timestamp defaults in models
- Loading branch information
Aleksandr Movchan
committed
Dec 13, 2024
1 parent
6a411fa
commit f41e8fa
Showing
4 changed files
with
64 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from sqlalchemy import DateTime | ||
from sqlalchemy.ext.compiler import compiles | ||
from sqlalchemy.sql.functions import FunctionElement | ||
|
||
|
||
class utcnow(FunctionElement): | ||
"""UTCNOW() expression for multiple dialects.""" | ||
|
||
inherit_cache = True | ||
type = DateTime() | ||
|
||
|
||
@compiles(utcnow) | ||
def default_sql_utcnow(element, compiler, **kw): | ||
"""Assume, by default, time zones work correctly. | ||
Note: | ||
This is a valid assumption for PostgreSQL and Oracle. | ||
""" | ||
return "CURRENT_TIMESTAMP" | ||
|
||
|
||
@compiles(utcnow, "sqlite") | ||
def sqlite_sql_utcnow(element, compiler, **kw): | ||
"""SQLite DATETIME('NOW') returns a correct `datetime.datetime` but does not add milliseconds to it. | ||
Directly call STRFTIME with the final %f modifier in order to get those. | ||
""" | ||
return r"(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW'))" | ||
|
||
|
||
@compiles(utcnow, "snowflake") | ||
def snowflake_sql_utcnow(element, compiler, **kw): | ||
"""In Snowflake, SYSDATE() returns the current timestamp for the system in the UTC time zone.""" | ||
return "SYSDATE()" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# ruff: noqa: S101 | ||
from datetime import datetime, timedelta, timezone | ||
|
||
from aana.storage.utcnow import utcnow | ||
|
||
|
||
def test_utcnow(db_session): | ||
"""Tests the utcnow() function.""" | ||
current_time_utc = datetime.now(tz=timezone.utc) | ||
result = db_session.execute(utcnow()).scalar() | ||
result = result.replace(tzinfo=timezone.utc) # Make result offset-aware | ||
assert isinstance(result, datetime) | ||
assert current_time_utc - result < timedelta(seconds=1) |