# 第29章示例:日期倒计时 + timedelta

from datetime import datetime, timedelta

# 1. 当前时间
now = datetime.now()
print("现在:", now)
print("格式化:", now.strftime("%Y年%m月%d日 %H:%M"))

# 2. timedelta 加减
tomorrow = now + timedelta(days=1)
print("明天:", tomorrow.strftime("%Y-%m-%d"))
in_3_hours = now + timedelta(hours=3)
print("3小时后:", in_3_hours.strftime("%H:%M"))

# 3. 倒计时
def countdown(target_date, name):
    today = datetime.now()
    target = datetime.strptime(target_date, "%Y-%m-%d")
    diff = target - today
    days = diff.days
    if days >= 0:
        print(f"距离{name}还有 {days} 天")
    else:
        print(f"{name}已经过去 {-days} 天了")

countdown("2027-01-01", "新年")
countdown("2026-08-01", "开学")
