4th_fix time error
This commit is contained in:
81
test_timezone_fix.py
Normal file
81
test_timezone_fix.py
Normal file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
測試時區修正是否正確
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from datetime import datetime
|
||||
from app import create_app
|
||||
from app.models.job import TranslationJob
|
||||
from app.models.user import User
|
||||
from app.utils.timezone import format_taiwan_time, now_taiwan, now_utc
|
||||
|
||||
def test_timezone_conversion():
|
||||
"""測試時區轉換功能"""
|
||||
|
||||
print("=" * 60)
|
||||
print("時區轉換測試")
|
||||
print("=" * 60)
|
||||
|
||||
# 1. 測試當前時間
|
||||
print("\n1. 當前時間測試:")
|
||||
print(f" 系統本地時間: {datetime.now()}")
|
||||
print(f" UTC 時間 (舊): {datetime.utcnow()}")
|
||||
print(f" UTC 時間 (新): {now_utc()}")
|
||||
print(f" 台灣時間: {now_taiwan()}")
|
||||
|
||||
# 2. 測試時間格式化
|
||||
print("\n2. 時間格式化測試:")
|
||||
utc_time = datetime.utcnow()
|
||||
print(f" UTC 時間原始: {utc_time}")
|
||||
print(f" 轉換為台灣時間: {format_taiwan_time(utc_time)}")
|
||||
|
||||
# 3. 測試模型的 to_dict 方法
|
||||
print("\n3. 測試資料模型時間輸出:")
|
||||
|
||||
app = create_app()
|
||||
|
||||
with app.app_context():
|
||||
# 創建測試資料
|
||||
from app import db
|
||||
|
||||
# 查詢一筆任務記錄
|
||||
job = TranslationJob.query.first()
|
||||
if job:
|
||||
print(f"\n 任務 UUID: {job.job_uuid}")
|
||||
print(f" 資料庫中的 created_at (UTC): {job.created_at}")
|
||||
|
||||
job_dict = job.to_dict()
|
||||
print(f" to_dict 輸出的 created_at (台灣時間): {job_dict['created_at']}")
|
||||
|
||||
if job.completed_at:
|
||||
print(f" 資料庫中的 completed_at (UTC): {job.completed_at}")
|
||||
print(f" to_dict 輸出的 completed_at (台灣時間): {job_dict['completed_at']}")
|
||||
else:
|
||||
print(" 沒有找到任務記錄")
|
||||
|
||||
# 查詢使用者記錄
|
||||
user = User.query.first()
|
||||
if user:
|
||||
print(f"\n 使用者: {user.username}")
|
||||
print(f" 資料庫中的 created_at (UTC): {user.created_at}")
|
||||
|
||||
user_dict = user.to_dict()
|
||||
print(f" to_dict 輸出的 created_at (台灣時間): {user_dict['created_at']}")
|
||||
|
||||
if user.last_login:
|
||||
print(f" 資料庫中的 last_login (UTC): {user.last_login}")
|
||||
print(f" to_dict 輸出的 last_login (台灣時間): {user_dict['last_login']}")
|
||||
else:
|
||||
print(" 沒有找到使用者記錄")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("測試完成!")
|
||||
print("=" * 60)
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_timezone_conversion()
|
Reference in New Issue
Block a user