# 動態小時數功能說明
## 📋 功能概述
`viz [city]` 和 `--mode viz` 現在會**動態顯示從午夜到當前時刻**的天氣資料,而非固定的 12 小時。
## 🔄 修改前後對比
### ❌ 修改前
```python
# 固定查詢 12 小時
result = client.get_city_weather("Tokyo", hours=12)
```
- 無論何時執行,都只顯示 00:00 - 11:00 (12小時)
- 資料範圍固定,不反映當前時間
### ✅ 修改後
```python
from datetime import datetime
# 動態計算當前小時
current_hour = datetime.now().hour
hours_to_show = current_hour + 1 # 包含當前小時
result = client.get_city_weather("Tokyo", hours=hours_to_show)
```
- 動態顯示從 00:00 到當前小時的資料
- 隨著時間自動調整
## 📊 使用範例
### 互動模式
```bash
python main.py --mode interactive
```
```
🌤️ Weather query: viz Tokyo
# 如果現在是 13:30
# 將顯示: Weather in Tokyo (00:00 - 13:00)
# 資料點數: 14 小時
```
### 視覺化模式
```bash
python main.py --mode viz
```
## 🕐 時間情境示例
| 當前時間 | 顯示範圍 | 資料點數 | 說明 |
|---------|---------|---------|------|
| 00:30 | 00:00 - 00:00 | 1 小時 | 午夜時段 |
| 08:15 | 00:00 - 08:00 | 9 小時 | 早上 8 點 |
| 13:30 | 00:00 - 13:00 | 14 小時 | 下午 1 點半 |
| 18:45 | 00:00 - 18:00 | 19 小時 | 傍晚 6 點 |
| 23:50 | 00:00 - 23:00 | 24 小時 | 深夜 11 點 |
## 💡 設計邏輯
### 計算方式
```python
current_hour = datetime.now().hour # 取得當前小時 (0-23)
hours_to_show = current_hour + 1 # +1 以包含當前小時
```
### 範例計算
- **現在 13:30**
- `current_hour = 13`
- `hours_to_show = 14`
- 查詢 14 小時資料 (00:00, 01:00, ..., 13:00)
- **現在 00:15**
- `current_hour = 0`
- `hours_to_show = 1`
- 查詢 1 小時資料 (00:00)
- **現在 23:45**
- `current_hour = 23`
- `hours_to_show = 24`
- 查詢 24 小時資料 (00:00 - 23:00,最大值)
## 🎯 修改的檔案
### 1. `main.py` - 互動模式 viz 指令
**位置**: 第 171-192 行
```python
# Calculate hours from midnight to current time
from datetime import datetime
current_hour = datetime.now().hour
hours_to_show = current_hour + 1 # Include current hour
result = client.get_city_weather(city, hours=hours_to_show)
```
### 2. `main.py` - 視覺化測試模式
**位置**: 第 98-125 行
```python
def run_visualization_demo():
from datetime import datetime
current_hour = datetime.now().hour
hours_to_show = current_hour + 1
result = client.get_city_weather("Tokyo", hours=hours_to_show)
# ...
```
## 🧪 測試
### 快速測試
```bash
python test_dynamic_hours.py
```
輸出範例:
```
🕐 Testing Dynamic Hours Based on Current Time
============================================================
📅 Current time: 2025-10-02 13:30:45
🕐 Current hour: 13
📊 Hours to display: 14 (00:00 - 13:00)
✅ Retrieved weather data for Tokyo, Japan
📋 Data points: 14
First: 2025-10-02T00:00
Last: 2025-10-02T13:00
```
### 情境測試
```bash
python test_time_scenarios.py
```
## 📈 優勢
1. **即時性**: 總是顯示最新到當前時刻的資料
2. **自動調整**: 無需手動設定小時數
3. **資料完整**: 顯示從午夜開始的完整歷史
4. **使用者體驗**: 圖表標題明確顯示時間範圍
## ⚠️ 注意事項
### API 限制
- Open-Meteo API 最多支援 24 小時歷史資料
- 在深夜 23:00 後會達到最大值(24小時)
### 時區
- 使用系統本地時間
- 確保系統時間設定正確
### 資料可用性
- API 提供的資料點可能少於請求的小時數
- 建議檢查返回的資料點數量
## 🔧 自訂調整
如需改回固定小時數:
```python
# 固定 12 小時
result = client.get_city_weather(city, hours=12)
# 或使用參數
hours = args.hours # 從命令列參數取得
result = client.get_city_weather(city, hours=hours)
```
## 📝 相關文件
- [README.md](../README.md) - 專案說明
- [QUICKSTART.md](QUICKSTART.md) - 快速開始
- [main.py](../main.py) - 主程式
---
**更新日期**: 2024-10-02
**功能狀態**: ✅ 已實作並測試