Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit b76f0f1

Browse files
committed
fix: correct TIMESTAMP formatting for snippets after migrating to aware datetimes
The switch to timezone-aware datetimes caused .isoformat() to include the +00:00 offset. Appending 'Z' manually resulted in an invalid string (...+00:00Z). Switched to .strftime() to produce the correct RFC 3339 format expected by Spanner.
1 parent 868d18d commit b76f0f1

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

samples/samples/pg_snippets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,13 +1321,13 @@ def query_data_with_timestamp_parameter(instance_id, database_id):
13211321
instance = spanner_client.instance(instance_id)
13221322
database = instance.database(database_id)
13231323

1324-
example_timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z"
1324+
example_timestamp = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
13251325
# [END spanner_postgresql_query_with_timestamp_parameter]
13261326
# Avoid time drift on the local machine.
13271327
# https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4197.
13281328
example_timestamp = (
13291329
datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=1)
1330-
).isoformat() + "Z"
1330+
).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
13311331
# [START spanner_postgresql_query_with_timestamp_parameter]
13321332
param = {"p1": example_timestamp}
13331333
param_type = {"p1": param_types.TIMESTAMP}

samples/samples/snippets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,13 +2401,13 @@ def query_data_with_timestamp_parameter(instance_id, database_id):
24012401
instance = spanner_client.instance(instance_id)
24022402
database = instance.database(database_id)
24032403

2404-
example_timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z"
2404+
example_timestamp = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
24052405
# [END spanner_query_with_timestamp_parameter]
24062406
# Avoid time drift on the local machine.
24072407
# https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4197.
24082408
example_timestamp = (
24092409
datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=1)
2410-
).isoformat() + "Z"
2410+
).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
24112411
# [START spanner_query_with_timestamp_parameter]
24122412
param = {"last_update_time": example_timestamp}
24132413
param_type = {"last_update_time": param_types.TIMESTAMP}

0 commit comments

Comments
 (0)