Skip to content

fix: treat SQLite database update time as UTC for session's last update time#1474

Merged
copybara-service[bot] merged 1 commit intomainfrom
copybara/772614326
Jun 27, 2025
Merged

fix: treat SQLite database update time as UTC for session's last update time#1474
copybara-service[bot] merged 1 commit intomainfrom
copybara/772614326

Conversation

@copybara-service
Copy link
Copy Markdown

@copybara-service copybara-service Bot commented Jun 17, 2025

fix: treat SQLite database update time as UTC for session's last update time

Fixes #1180

We are using func.now() to set the onupdate time for db, when SQLAlchemy generates the SQL to build the database, it actually translates func.now() into NOW() or CURRENT_TIMESTAMP. The value it returns depends on the database server settings. For example, if the global/default timezone for a db is set to be UTC, the update time will be set to be a UCT time; if the global time zone for a db is set to be a local time zone (e.g. America/Los_Angeles), the update time will be a local time.

Normally, the best practice is to set database server to use UTC. Applications will convert it into different time zones as needed.

For SQLite, there is no way to config the default timezone, it will just treat it as UTC. But because it is a naive datetime (with no timezone info), python will assume it is a local time and then covert it into a UTC, which is why we see the bug (e.g. we create a session at 2025-06-17 12:49:33 local time, but when we read the session, its last update time is 2025-06-17 19:49:33 local time).

The solution is converting the native datatime to be timezone aware before .timestamp().

The change in this CL only affects SQLite database.

@copybara-service copybara-service Bot force-pushed the copybara/772614326 branch 2 times, most recently from f7c2a84 to e750db8 Compare June 24, 2025 18:23
@copybara-service copybara-service Bot changed the title fix: Treat database update time as UTC for session's last update time fix: treat database update time as UTC for session's last update time Jun 24, 2025
@copybara-service copybara-service Bot force-pushed the copybara/772614326 branch from e750db8 to 85a0e5c Compare June 24, 2025 20:05
@fhinkel
Copy link
Copy Markdown

fhinkel commented Jun 26, 2025

fixes #1180

@copybara-service copybara-service Bot force-pushed the copybara/772614326 branch from 85a0e5c to 328f305 Compare June 27, 2025 18:18
@copybara-service copybara-service Bot changed the title fix: treat database update time as UTC for session's last update time fix: treat SQLite database update time as UTC for session's last update time Jun 27, 2025
…te time

Fixes #1180

We are using `func.now()` to set the `onupdate` time for db, when SQLAlchemy generates the SQL to build the database, it actually translates `func.now()` into `NOW()` or `CURRENT_TIMESTAMP`. The value it returns depends on the database server settings. For example, if the global/default timezone for a db is set to be UTC, the update time will be set to be a UCT time; if the global time zone for a db is set to be a local time zone (e.g. America/Los_Angeles), the update time will be a local time.

Normally, the best practice is to set database server to use UTC. Applications will convert it into different time zones as needed.

For SQLite, there is no way to config the default timezone, it will just treat it as UTC. But because it is a naive datetime (with no timezone info), python will assume it is a local time and then covert it into a UTC, which is why we see the bug (e.g. we create a session at 2025-06-17 12:49:33 local time, but when we read the session, its last update time is 2025-06-17 19:49:33 local time).

The solution is converting the native datatime to be timezone aware before `.timestamp()`.

The change in this CL only affects SQLite database.

PiperOrigin-RevId: 776654443
@copybara-service copybara-service Bot force-pushed the copybara/772614326 branch from 328f305 to 3f621ae Compare June 27, 2025 18:22
@copybara-service copybara-service Bot merged commit 3f621ae into main Jun 27, 2025
@copybara-service copybara-service Bot deleted the copybara/772614326 branch June 27, 2025 18:22
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.

Session lastUpdateTime not in UTC when using SQLite DB

2 participants