#!/usr/bin/env python3 """ 检查数据库时区设置 """ import asyncio import sys from pathlib import Path from datetime import datetime project_root = Path(__file__).parent sys.path.insert(0, str(project_root)) sys.path.insert(0, str(project_root / "src")) from sqlalchemy.ext.asyncio import create_async_engine from sqlalchemy import text from config.settings import settings async def check(): """检查数据库时区设置""" if not settings.DATABASE_URL: print("错误:未配置数据库连接") return engine = create_async_engine(settings.DATABASE_URL, echo=False) async with engine.begin() as conn: # 检查数据库时区设置 result = await conn.execute(text("SHOW timezone")) timezone = result.scalar() print(f"数据库时区: {timezone}") # 检查数据库当前时间 result = await conn.execute(text("SELECT now()")) db_now = result.scalar() print(f"数据库当前时间: {db_now}") # 检查 Python 当前时间 python_now = datetime.now() print(f"Python 当前时间: {python_now}") # 检查数据库当前时间(转换为本地时区) result = await conn.execute(text("SELECT now() AT TIME ZONE 'Asia/Shanghai'")) db_now_local = result.scalar() print(f"数据库当前时间 (Asia/Shanghai): {db_now_local}") await engine.dispose() if __name__ == "__main__": asyncio.run(check())