26 lines
653 B
Python
26 lines
653 B
Python
from datetime import datetime
|
||
from typing import Tuple
|
||
|
||
|
||
def get_timestamp_range(ts: int, delta_seconds: int = 5, unit: str = "microseconds") -> Tuple[int, int]:
|
||
"""
|
||
返回时间戳前后delta_seconds秒的范围(单位与输入一致)
|
||
"""
|
||
if ts <= 0:
|
||
return 0, 0
|
||
|
||
scale = {
|
||
"seconds": 1,
|
||
"milliseconds": 1000,
|
||
"microseconds": 1_000_000,
|
||
"nanoseconds": 1_000_000_000
|
||
|
||
}.get(unit, 1_000_000) # 默认微秒
|
||
|
||
delta = delta_seconds * scale
|
||
return ts - delta, ts + delta
|
||
|
||
|
||
def format_timestamp(timestamp: int) -> datetime:
|
||
return datetime.fromtimestamp(timestamp / 1_000_000)
|