Files
log-alert-service/utils/time.py

26 lines
678 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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) -> str:
return datetime.fromtimestamp(timestamp / 1_000_000).strftime("%Y-%m-%d %H:%M:%S")