Skip to content

Commit 3d8fade

Browse files
committed
feat(constants): add from_microseconds class method to DAVTime
1 parent 5dedc4a commit 3d8fade

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

asgi_webdav/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ def __init__(self, timestamp: float | None = None):
355355
self.timestamp = timestamp
356356
self.data = datetime.fromtimestamp(timestamp, tz=timezone.utc)
357357

358+
@classmethod
359+
def from_microseconds(cls, timestamp: float):
360+
return cls(timestamp / 1_000_000)
361+
358362
@cached_property
359363
def iso_8601(self) -> str:
360364
# - https://datatracker.ietf.org/doc/html/rfc3339#section-5.6

entrypoint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
echo "
44
------------------------
55
App user's UID:GID
6+
67
$UID:$GID
78
------------------------
89
"

tests/test_constants.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,35 @@ def test_DAVTime():
127127
assert dt.w3c == "1970-01-01 00:00:00+00:00"
128128
assert dt.http_date == "Thu, 01 Jan 1970 00:00:00 GMT"
129129
assert dt.display(timezone_shanghai) == "1970-01-01 08:00:00+08:00"
130+
131+
132+
def test_DAVTime_from_microseconds():
133+
"""测试从微秒时间戳创建 DAVTime 实例"""
134+
# 测试 0 微秒,对应 Unix 纪元
135+
dt = DAVTime.from_microseconds(0.0)
136+
assert isinstance(dt, DAVTime)
137+
assert dt.timestamp == 0.0
138+
assert dt.iso_8601 == "1970-01-01T00:00:00+00:00"
139+
140+
# 测试 1 秒(1,000,000 微秒)
141+
dt = DAVTime.from_microseconds(1_000_000.0)
142+
assert dt.timestamp == 1.0
143+
# 验证转换后的日期时间字符串
144+
assert dt.iso_8601 == "1970-01-01T00:00:01+00:00"
145+
146+
# 测试负微秒值(1970 年之前的时间)
147+
dt = DAVTime.from_microseconds(-1_000_000.0)
148+
assert dt.timestamp == -1.0
149+
assert dt.iso_8601 == "1969-12-31T23:59:59+00:00"
150+
151+
# 测试浮点数微秒值(带小数部分)
152+
dt = DAVTime.from_microseconds(1_500_000.5)
153+
assert dt.timestamp == 1.5000005 # 1.5 秒 + 0.5 微秒
154+
# 注意:isoformat() 可能只显示到微秒精度
155+
156+
# 验证通过 from_microseconds 创建的实例与直接使用秒级时间戳创建的实例行为一致
157+
microseconds = 2_123_456.789
158+
dt_from_micro = DAVTime.from_microseconds(microseconds)
159+
dt_from_seconds = DAVTime(microseconds / 1_000_000)
160+
assert dt_from_micro.timestamp == dt_from_seconds.timestamp
161+
assert dt_from_micro.iso_8601 == dt_from_seconds.iso_8601

0 commit comments

Comments
 (0)