baoxiang/test_timezone_fixed.py
2025-12-16 18:06:50 +08:00

28 lines
1020 B
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.

from datetime import datetime
# 模拟数据库中的UTC时间naive datetime
db_created_time = datetime(2025, 12, 14, 11, 0, 0) # UTC时间 11:00
# 模拟当前UTC时间
now_utc = datetime.utcnow()
print(f"Database created time (UTC): {db_created_time}")
print(f"Current UTC time: {now_utc}")
# 计算时间差
elapsed = (now_utc - db_created_time).total_seconds()
print(f"Elapsed seconds: {elapsed}")
# 测试60秒倒计时
countdown_seconds = 60
time_remaining = max(0, int(countdown_seconds - elapsed))
print(f"Time remaining for 60s countdown: {time_remaining} seconds")
# 如果是新创建的宝箱1秒前创建
recent_created_time = datetime.utcnow() # 当前UTC时间
elapsed_recent = (now_utc - recent_created_time).total_seconds()
time_remaining_recent = max(0, int(countdown_seconds - elapsed_recent))
print(f"\nFor recently created chest:")
print(f"Created time: {recent_created_time}")
print(f"Elapsed seconds: {elapsed_recent}")
print(f"Time remaining: {time_remaining_recent} seconds")