Skip to content

Normalize trial timestamps to UTC in RDBStorage and JournalStorage - #6776

Merged
not522 merged 5 commits into
optuna:masterfrom
c-bata:follow-up-6755
Jul 31, 2026
Merged

not522 merged 5 commits into
optuna:masterfrom
c-bata:follow-up-6755

Conversation

@c-bata

@c-bata c-bata commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

Motivation

Refs #6755

RDBStorage and JournalStorage currently create trial timestamps using timezone-naive datetime.now().
As a result, the persisted values represent the local wall-clock time of the process writing to the storage.

This makes datetime_start and datetime_complete dependent on the writer's local timezone. In particular, when using the gRPC storage proxy, changing the timezone of the proxy process changes the timestamps written to the database, even if the workers and database remain unchanged.

This PR normalizes newly stored trial timestamps to UTC while preserving the existing public representation of trial datetimes.

Description of the changes

The datetime representations after this change are:

Layer Representation
RDBStorage database columns Timezone-naive UTC
JournalStorage logs Timezone-aware UTC
FrozenTrial.datetime_start and datetime_complete Timezone-naive local time

The representations used by RDBStorage and JournalStorage intentionally differ.

  • RDBStorage continues to use timezone-naive database columns because timezone-aware column types are not portable across all supported database backends. Using them would also require a schema migration on backends that support them.
  • JournalStorage stores JSON-compatible strings and can preserve the UTC offset without a schema migration, so new Journal entries use timezone-aware ISO 8601 values such as 2026-07-28T10:00:00.000000+00:00.

The public FrozenTrial representation remains timezone-naive local time for backward compatibility.
Changing it to a timezone-aware datetime would break existing code that compares or subtracts it from a timezone-naive datetime.now(), including existing Optuna functionality such as plot_timeline().

@c-bata
c-bata marked this pull request as draft July 28, 2026 11:49
@c-bata c-bata added the compatibility Change that breaks compatibility. label Jul 28, 2026
@c-bata
c-bata marked this pull request as ready for review July 29, 2026 06:27
@c-bata c-bata assigned not522 and y0z Jul 29, 2026
@c-bata c-bata added this to the v5.0.0 milestone Jul 29, 2026
@c-bata

c-bata commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

@y0z @not522 Could you review this PR?

@c-bata

c-bata commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

Since verifying UTC normalization requires inspecting backend-specific persisted data directly, such as querying the RDB with raw SQL or inspecting the Journal log file, I manually verified the behavior using the steps below. All commands below were run in the Asia/Tokyo timezone (UTC+09:00).

RDBStorage (SQLite3)

Script

import os
import optuna

branch_name = "master"
base_dir = os.path.dirname(__file__)
storage = optuna.storages.get_storage(f"sqlite:///{base_dir}/{branch_name}.sqlite3")

study = optuna.create_study(storage=storage)
study.optimize(lambda t: t.suggest_float("x", -10, 10), n_trials=5)

print(f"{study.trials[0].datetime_start=}")
print(f"{study.trials[0].datetime_start.tzinfo=}")

print(f"{study.trials[0].datetime_complete=}")
print(f"{study.trials[0].datetime_complete.tzinfo=}")

Before

$ python tmp/datetime/run_sqlite3.py
:
study.trials[0].datetime_start=datetime.datetime(2026, 7, 28, 19, 0, 33, 580824)
study.trials[0].datetime_start.tzinfo=None
study.trials[0].datetime_complete=datetime.datetime(2026, 7, 28, 19, 0, 33, 608338)
study.trials[0].datetime_complete.tzinfo=None
sqlite> .header on
sqlite> .mode column
sqlite> select trial_id, datetime_start, datetime_complete from trials;
trial_id  datetime_start              datetime_complete
--------  --------------------------  --------------------------
1         2026-07-28 19:00:33.580824  2026-07-28 19:00:33.608338
2         2026-07-28 19:00:33.614280  2026-07-28 19:00:33.620419
3         2026-07-28 19:00:33.624365  2026-07-28 19:00:33.629819
4         2026-07-28 19:00:33.634528  2026-07-28 19:00:33.639668
5         2026-07-28 19:00:33.643005  2026-07-28 19:00:33.648662

With this PR

$ python tmp/datetime/run_sqlite3.py
:
study.trials[0].datetime_start=datetime.datetime(2026, 7, 28, 19, 32, 26, 746021)
study.trials[0].datetime_start.tzinfo=None
study.trials[0].datetime_complete=datetime.datetime(2026, 7, 28, 19, 32, 26, 774664)
study.trials[0].datetime_complete.tzinfo=None
sqlite> .header on
sqlite> .mode column
sqlite> select trial_id, datetime_start, datetime_complete from trials;
trial_id  datetime_start              datetime_complete
--------  --------------------------  --------------------------
1         2026-07-28 10:32:26.746021  2026-07-28 10:32:26.774664
2         2026-07-28 10:32:26.780504  2026-07-28 10:32:26.786492
3         2026-07-28 10:32:26.790253  2026-07-28 10:32:26.795479
4         2026-07-28 10:32:26.799087  2026-07-28 10:32:26.805581
5         2026-07-28 10:32:26.809424  2026-07-28 10:32:26.815281

JournalStorage (JournalFileBackend)

Script

import os
import optuna
from optuna.storages.journal import JournalStorage
from optuna.storages.journal import JournalFileBackend

branch_name = "master"
base_dir = os.path.dirname(__file__)
storage = JournalStorage(JournalFileBackend(f"{base_dir}/{branch_name}.journal"))

study = optuna.create_study(storage=storage)
study.optimize(lambda t: t.suggest_float("x", -10, 10), n_trials=5)

print(f"{study.trials[0].datetime_start=}")
print(f"{study.trials[0].datetime_start.tzinfo=}")

print(f"{study.trials[0].datetime_complete=}")
print(f"{study.trials[0].datetime_complete.tzinfo=}")

Before

$ python tmp/datetime/run_journal.py
:
study.trials[0].datetime_start=datetime.datetime(2026, 7, 28, 19, 35, 0, 974514)
study.trials[0].datetime_start.tzinfo=None
study.trials[0].datetime_complete=datetime.datetime(2026, 7, 28, 19, 35, 0, 987160)
study.trials[0].datetime_complete.tzinfo=None
$ cat ./tmp/datetime/master.journal | grep datetime
{"op_code":4,"worker_id":"2db8a837-c323-49d1-9d83-dae4f7614f6b-8686522176","study_id":0,"datetime_start":"2026-07-28T19:35:00.974514"}
{"op_code":6,"worker_id":"2db8a837-c323-49d1-9d83-dae4f7614f6b-8686522176","trial_id":0,"state":1,"values":[9.517414743983306],"datetime_complete":"2026-07-28T19:35:00.987160"}
:

With this PR

$ python tmp/datetime/run_journal.py
:
study.trials[0].datetime_start=datetime.datetime(2026, 7, 28, 20, 16, 13, 354151)
study.trials[0].datetime_start.tzinfo=None
study.trials[0].datetime_complete=datetime.datetime(2026, 7, 28, 20, 16, 13, 367062)
study.trials[0].datetime_complete.tzinfo=None
$ cat ./tmp/datetime/master.journal | grep datetime
{"op_code":4,"worker_id":"cc7d3824-ebf8-4be8-955d-90974d7ea97d-8686522176","study_id":0,"datetime_start":"2026-07-28T11:16:13.354151+00:00"}
{"op_code":6,"worker_id":"cc7d3824-ebf8-4be8-955d-90974d7ea97d-8686522176","trial_id":0,"state":1,"values":[0.07257957740274046],"datetime_complete":"2026-07-28T11:16:13.367062+00:00"}
:

@y0z y0z left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@not522 not522 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compatibility Change that breaks compatibility.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants