Skip to content

feat: convert datetime values to RFC 3339 in WebDAV search literals #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion nc_py_api/files/_files.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Helper functions for **FilesAPI** and **AsyncFilesAPI** classes."""

import enum
from datetime import datetime, timezone
from io import BytesIO
from json import dumps, loads
from typing import Any
from urllib.parse import unquote
from xml.etree import ElementTree

Expand Down Expand Up @@ -69,6 +71,16 @@ def get_propfind_properties(capabilities: dict) -> list:
return r


def _dav_literal(val: Any) -> str:
"""Return a string suitable for <d:literal>."""
if isinstance(val, datetime):
# make timezone-aware, force UTC, second precision
dt = val if val.tzinfo else val.replace(tzinfo=timezone.utc)
dt = dt.astimezone(timezone.utc).replace(microsecond=0)
return dt.isoformat().replace("+00:00", "Z") # 2025-03-10T12:34:56Z
return str(val)


def build_find_request(req: list, path: str | FsNode, user: str, capabilities: dict) -> ElementTree.Element:
path = path.user_path if isinstance(path, FsNode) else path
root = ElementTree.Element(
Expand Down Expand Up @@ -126,7 +138,7 @@ def _add_value(xml_element, val=None) -> None:
ElementTree.SubElement(_, SEARCH_PROPERTIES_MAP[req.pop(0)])
_ = ElementTree.SubElement(_root, "d:literal")
value = req.pop(0)
_.text = value if isinstance(value, str) else str(value)
_.text = _dav_literal(value)

while len(req):
where_part = req.pop(0)
Expand Down
11 changes: 10 additions & 1 deletion tests/actual_tests/files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import math
import os
import zipfile
from datetime import datetime
from datetime import datetime, timedelta
from io import BytesIO
from pathlib import Path
from random import choice, randbytes
Expand Down Expand Up @@ -753,6 +753,15 @@ def test_find_files_listdir_depth(nc_any):
assert len(result) == 4


def test_find_files_datetime(nc_any):
time_in_past = datetime.now() - timedelta(days=1)
time_in_future = datetime.now() + timedelta(days=1)
assert len(nc_any.files.find(["gt", "last_modified", time_in_past], "/test_dir/subdir/"))
assert not len(nc_any.files.find(["gt", "last_modified", time_in_future], "/test_dir/subdir/"))
assert not len(nc_any.files.find(["lt", "last_modified", time_in_past], "/test_dir/subdir/"))
assert len(nc_any.files.find(["lt", "last_modified", time_in_future], "/test_dir/subdir/"))


def test_listdir_depth(nc_any):
result = nc_any.files.listdir("test_dir/", depth=1)
result2 = nc_any.files.listdir("test_dir")
Expand Down