Skip to content

Commit b36ed72

Browse files
committed
feat: convert datetime values to RFC 3339 in WebDAV search literals
Signed-off-by: bigcat88 <[email protected]>
1 parent ebce575 commit b36ed72

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

nc_py_api/files/_files.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Helper functions for **FilesAPI** and **AsyncFilesAPI** classes."""
22

33
import enum
4+
from datetime import datetime, timezone
45
from io import BytesIO
56
from json import dumps, loads
7+
from typing import Any
68
from urllib.parse import unquote
79
from xml.etree import ElementTree
810

@@ -69,6 +71,16 @@ def get_propfind_properties(capabilities: dict) -> list:
6971
return r
7072

7173

74+
def _dav_literal(val: Any) -> str:
75+
"""Return a string suitable for <d:literal>."""
76+
if isinstance(val, datetime):
77+
# make timezone-aware, force UTC, second precision
78+
dt = val if val.tzinfo else val.replace(tzinfo=timezone.utc)
79+
dt = dt.astimezone(timezone.utc).replace(microsecond=0)
80+
return dt.isoformat().replace("+00:00", "Z") # 2025-03-10T12:34:56Z
81+
return str(val)
82+
83+
7284
def build_find_request(req: list, path: str | FsNode, user: str, capabilities: dict) -> ElementTree.Element:
7385
path = path.user_path if isinstance(path, FsNode) else path
7486
root = ElementTree.Element(
@@ -126,7 +138,7 @@ def _add_value(xml_element, val=None) -> None:
126138
ElementTree.SubElement(_, SEARCH_PROPERTIES_MAP[req.pop(0)])
127139
_ = ElementTree.SubElement(_root, "d:literal")
128140
value = req.pop(0)
129-
_.text = value if isinstance(value, str) else str(value)
141+
_.text = _dav_literal(value)
130142

131143
while len(req):
132144
where_part = req.pop(0)

0 commit comments

Comments
 (0)