Skip to content

Commit 7123e88

Browse files
fix: improve RFC 3339 timestamp parsing in on_schedule function
1 parent 22c7638 commit 7123e88

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/firebase_functions/scheduler_fn.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import firebase_functions.options as _options
3333
import firebase_functions.private.util as _util
3434
from firebase_functions.core import _with_init
35-
import dateutil.parser as dateutil_parser
3635

3736
# Re-export Timezone from options module so users can import it directly from scheduler_fn
3837
# This provides a more convenient API: from firebase_functions.scheduler_fn import Timezone
@@ -105,11 +104,24 @@ def on_schedule_wrapped(request: _Request) -> _Response:
105104
schedule_time = _dt.datetime.utcnow()
106105
else:
107106
try:
108-
# Robust RFC 3339 parsing
109-
schedule_time = dateutil_parser.isoparse(schedule_time_str)
110-
except ValueError as e:
111-
print(f"Failed to parse RFC 3339 timestamp: {e}")
112-
schedule_time = _dt.utcnow()
107+
# Try to parse with the stdlib which supports fractional
108+
# seconds and offsets in Python 3.11+ via fromisoformat.
109+
# Normalize RFC3339 'Z' to '+00:00' for fromisoformat.
110+
iso_str = schedule_time_str
111+
if iso_str.endswith("Z"):
112+
iso_str = iso_str[:-1] + "+00:00"
113+
schedule_time = _dt.datetime.fromisoformat(iso_str)
114+
except Exception:
115+
# Fallback to strict parsing without fractional seconds
116+
try:
117+
schedule_time = _dt.datetime.strptime(
118+
schedule_time_str,
119+
"%Y-%m-%dT%H:%M:%S%z",
120+
)
121+
except Exception as e:
122+
# If all parsing fails, log and use current UTC time
123+
_logging.exception(e)
124+
schedule_time = _dt.datetime.utcnow()
113125
event = ScheduledEvent(
114126
job_name=request.headers.get("X-CloudScheduler-JobName"),
115127
schedule_time=schedule_time,

0 commit comments

Comments
 (0)