MCPcopy Create free account
hub / github.com/NVIDIA/NeMo-Relay / opt_py_to_timestamp

Function opt_py_to_timestamp

crates/python/src/convert.rs:46–68  ·  view source on GitHub ↗

Convert an optional timezone-aware Python datetime to a UTC timestamp.

(value: Option<&Bound<'_, PyAny>>)

Source from the content-addressed store, hash-verified

44
45/// Convert an optional timezone-aware Python datetime to a UTC timestamp.
46pub fn opt_py_to_timestamp(value: Option<&Bound<'_, PyAny>>) -> PyResult<Option<DateTime<Utc>>> {
47 let Some(timestamp) = value.filter(|timestamp| !timestamp.is_none()) else {
48 return Ok(None);
49 };
50
51 let py = timestamp.py();
52 let datetime_type = py.import("datetime")?.getattr("datetime")?;
53 if !timestamp.is_instance(&datetime_type)? {
54 return Err(pyo3::exceptions::PyTypeError::new_err(
55 "timestamp must be a datetime.datetime object",
56 ));
57 }
58 if timestamp.getattr("tzinfo")?.is_none() || timestamp.call_method0("utcoffset")?.is_none() {
59 return Err(pyo3::exceptions::PyValueError::new_err(
60 "timestamp datetime must be timezone-aware",
61 ));
62 }
63
64 let iso_timestamp: String = timestamp.call_method0("isoformat")?.extract()?;
65 DateTime::parse_from_rfc3339(&iso_timestamp)
66 .map(|timestamp| Some(timestamp.with_timezone(&Utc)))
67 .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("invalid timestamp: {e}")))
68}
69
70#[cfg(test)]
71#[path = "../tests/unit/convert_tests.rs"]

Callers 7

push_scopeFunction · 0.85
pop_scopeFunction · 0.85
eventFunction · 0.85
tool_callFunction · 0.85
tool_call_endFunction · 0.85
llm_callFunction · 0.85
llm_call_endFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected