Skip to content
Merged
26 changes: 21 additions & 5 deletions src/firebase_functions/scheduler_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import dataclasses as _dataclasses
import datetime as _dt
from datetime import timezone as _timezone
import functools as _functools
import typing as _typing

Expand Down Expand Up @@ -101,12 +102,27 @@ def on_schedule_wrapped(request: _Request) -> _Response:
schedule_time: _dt.datetime
schedule_time_str = request.headers.get("X-CloudScheduler-ScheduleTime")
if schedule_time_str is None:
schedule_time = _dt.datetime.utcnow()
schedule_time = _dt.datetime.now(_timezone.utc)
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 ValueError:
# Fallback to strict parsing without fractional seconds
try:
schedule_time = _dt.datetime.strptime(
schedule_time_str,
"%Y-%m-%dT%H:%M:%S%z",
)
except ValueError as e:
# If all parsing fails, log and use current UTC time
_logging.exception(e)
schedule_time = _dt.datetime.now(_timezone.utc)
event = ScheduledEvent(
job_name=request.headers.get("X-CloudScheduler-JobName"),
schedule_time=schedule_time,
Expand Down
Loading