Commit 3f621ae
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.
PiperOrigin-RevId: 7766544431 parent 4e765ae commit 3f621ae
2 files changed
Lines changed: 36 additions & 11 deletions
File tree
- src/google/adk/sessions
- tests/unittests/sessions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
144 | 145 | | |
145 | 146 | | |
146 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
147 | 163 | | |
148 | 164 | | |
149 | 165 | | |
| |||
412 | 428 | | |
413 | 429 | | |
414 | 430 | | |
415 | | - | |
| 431 | + | |
416 | 432 | | |
417 | 433 | | |
418 | 434 | | |
| |||
473 | 489 | | |
474 | 490 | | |
475 | 491 | | |
476 | | - | |
| 492 | + | |
477 | 493 | | |
478 | 494 | | |
479 | 495 | | |
| |||
496 | 512 | | |
497 | 513 | | |
498 | 514 | | |
499 | | - | |
| 515 | + | |
500 | 516 | | |
501 | 517 | | |
502 | 518 | | |
| |||
529 | 545 | | |
530 | 546 | | |
531 | 547 | | |
532 | | - | |
| 548 | + | |
533 | 549 | | |
534 | 550 | | |
535 | 551 | | |
536 | 552 | | |
537 | | - | |
538 | | - | |
| 553 | + | |
| 554 | + | |
539 | 555 | | |
540 | 556 | | |
541 | 557 | | |
| |||
577 | 593 | | |
578 | 594 | | |
579 | 595 | | |
580 | | - | |
| 596 | + | |
581 | 597 | | |
582 | 598 | | |
583 | 599 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
66 | 68 | | |
67 | 69 | | |
68 | 70 | | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
73 | 82 | | |
74 | 83 | | |
75 | 84 | | |
| |||
0 commit comments