diff --git a/Dockerfile b/Dockerfile index 9ef3f71..00ec992 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,8 @@ # 使用Python 3.10作为基础镜像 FROM python:3.10-slim +# 设置时区 +RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 清空所有默认源 RUN rm -rf /etc/apt/sources.list.d/* && \ rm -f /etc/apt/sources.list diff --git a/app/core/response.py b/app/core/response.py index 8e80277..8b369d3 100644 --- a/app/core/response.py +++ b/app/core/response.py @@ -6,10 +6,12 @@ from typing import TypeVar, Generic from fastapi import status import json from decimal import Decimal +from app.core.utils import CommonUtils + class CustomJSONEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime): - return obj.strftime('%Y-%m-%d %H:%M:%S') + return CommonUtils.get_asia_datetime(obj) if isinstance(obj, time): return obj.strftime('%H:%M:%S') elif isinstance(obj, date): diff --git a/app/core/utils.py b/app/core/utils.py index 823ef40..584acbe 100644 --- a/app/core/utils.py +++ b/app/core/utils.py @@ -7,7 +7,15 @@ class CommonUtils: """工具类""" @staticmethod - def get_current_time(timezone='Asia/Shanghai'): + def get_asia_datetime(my_datetime: datetime, timezone='Asia/Shanghai') -> str: + """ + 将时间转换为UTF-8编码 + """ + tz = pytz.timezone(timezone) + return my_datetime.astimezone(tz).strftime('%Y-%m-%d %H:%M:%S') + + @staticmethod + def get_current_time(timezone='Asia/Shanghai') -> str: """ 获取指定时区的当前时间 :param timezone: 时区名称,默认为上海时区(东八区)