Skip to content
Merged
23 changes: 19 additions & 4 deletions src/firebase_functions/scheduler_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,25 @@ def on_schedule_wrapped(request: _Request) -> _Response:
if schedule_time_str is None:
schedule_time = _dt.datetime.utcnow()
else:
schedule_time = _dt.datetime.strptime(
schedule_time_str,
"%Y-%m-%dT%H:%M:%S%z",
)
try:
# Try to parse with the stdlib which supports fractional
# seconds and offsets in Python 3.11+ via fromisoformat.
# Normalize RFC3339 'Z' to '+00:00' for fromisoformat.
iso_str = schedule_time_str
if iso_str.endswith("Z"):
iso_str = iso_str[:-1] + "+00:00"
schedule_time = _dt.datetime.fromisoformat(iso_str)
except Exception:
# Fallback to strict parsing without fractional seconds
try:
schedule_time = _dt.datetime.strptime(
schedule_time_str,
"%Y-%m-%dT%H:%M:%S%z",
)
except Exception as e:
# If all parsing fails, log and use current UTC time
_logging.exception(e)
schedule_time = _dt.datetime.utcnow()
event = ScheduledEvent(
job_name=request.headers.get("X-CloudScheduler-JobName"),
schedule_time=schedule_time,
Expand Down