"""工具函数"""
from typing import Any, Dict
import json
from datetime import datetime


def format_datetime(dt: datetime, fmt: str = "%Y-%m-%d %H:%M:%S") -> str:
    """格式化日期时间"""
    return dt.strftime(fmt)


def safe_json_loads(json_str: str, default: Any = None) -> Any:
    """安全的JSON解析"""
    try:
        return json.loads(json_str)
    except (json.JSONDecodeError, TypeError):
        return default


def dict_filter_none(data: Dict[str, Any]) -> Dict[str, Any]:
    """过滤字典中的None值"""
    return {k: v for k, v in data.items() if v is not None}
