Files
SmartVoyage/utils/format.py
liangfangxing 0b087df55e feat: mcp
2026-03-20 11:26:44 +08:00

41 lines
2.0 KiB
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.
import json
from datetime import date, datetime, timedelta
from decimal import Decimal
def default_encoder(obj): # 定义编码器方法,用于格式化单个对象
if isinstance(obj, datetime): # 检查是否为datetime返回带时间的格式化字符串
return obj.strftime('%Y-%m-%d %H:%M:%S')
if isinstance(obj, date): # 检查是否为date返回日期格式化字符串
return obj.strftime('%Y-%m-%d')
if isinstance(obj, timedelta): # 检查是否为timedelta转换为字符串
return str(obj)
if isinstance(obj, Decimal): # 检查是否为Decimal转换为浮点数
return float(obj)
return obj # 否则返回原对象
# 定义自定义JSON编码器类继承自json.JSONEncoder用于处理非标准类型序列化
class DateEncoder(json.JSONEncoder):
def default(self, obj): # 重写default方法处理序列化时的默认对象转换
if isinstance(obj, (date, datetime)): # 检查对象是否为date或datetime类型对于datetime返回带时间的字符串对于date返回日期字符串
return obj.strftime('%Y-%m-%d %H:%M:%S') if isinstance(obj, datetime) else obj.strftime('%Y-%m-%d')
if isinstance(obj, timedelta): # 检查对象是否为timedelta类型将时间差转换为字符串
return str(obj)
if isinstance(obj, Decimal): # 检查对象是否为Decimal类型将Decimal转换为浮点数以兼容JSON
return float(obj)
return super().default(obj) # 对于其他类型,调用父类默认方法
if __name__ == '__main__':
print(default_encoder(datetime(2025, 8, 11, 8, 0)))
print(default_encoder(date(2025, 8, 11)))
print(default_encoder(timedelta(days=1)))
print(default_encoder(Decimal('123.45')))
print('*'*80)
encoder = DateEncoder()
print(encoder.default(datetime(2025, 8, 11, 8, 0)))
print(encoder.default(date(2025, 8, 11)))
print(encoder.default(timedelta(days=1)))
print(encoder.default(Decimal('123.45')))