設定時區
既然你已經能一次設定一個時區了,接下來來看看如何為車號 W20529 的前 10 段行程設定時區。
timezone 和 timedelta 已經匯入。請使用 .replace() 來完成變更。
本練習屬於課程
在 Python 處理日期與時間
練習說明
- 建立
edt,這是一個 UTC 位移為 -4 小時的timezone物件。 - 在
for迴圈中: - 設定
trip['start']的tzinfo。 - 設定
trip['end']的tzinfo。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a timezone object corresponding to UTC-4
edt = ____
# Loop over trips, updating the start and end datetimes to be in UTC-4
for trip in onebike_datetimes[:10]:
# Update trip['start'] and trip['end']
trip['start'] = trip['start'].____
trip['end'] = trip['end'].____