MCPcopy
hub / github.com/agent-infra/sandbox / serialize_datetime

Function serialize_datetime

sdk/python/agent_sandbox/core/datetime_utils.py:6–28  ·  view source on GitHub ↗

Serialize a datetime including timezone info. Uses the timezone info provided if present, otherwise uses the current runtime's timezone info. UTC datetimes end in "Z" while all other timezones are represented as offset from UTC, e.g. +05:00.

(v: dt.datetime)

Source from the content-addressed store, hash-verified

4
5
6def serialize_datetime(v: dt.datetime) -> str:
7 """
8 Serialize a datetime including timezone info.
9
10 Uses the timezone info provided if present, otherwise uses the current runtime's timezone info.
11
12 UTC datetimes end in "Z" while all other timezones are represented as offset from UTC, e.g. +05:00.
13 """
14
15 def _serialize_zoned_datetime(v: dt.datetime) -> str:
16 if v.tzinfo is not None and v.tzinfo.tzname(None) == dt.timezone.utc.tzname(None):
17 # UTC is a special case where we use "Z" at the end instead of "+00:00"
18 return v.isoformat().replace("+00:00", "Z")
19 else:
20 # Delegate to the typical +/- offset format
21 return v.isoformat()
22
23 if v.tzinfo is not None:
24 return _serialize_zoned_datetime(v)
25 else:
26 local_tz = dt.datetime.now().astimezone().tzinfo
27 localized_dt = v.replace(tzinfo=local_tz)
28 return _serialize_zoned_datetime(localized_dt)

Callers 2

serialize_modelMethod · 0.85
jsonable_encoderFunction · 0.85

Calls 1

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…