Twitter API抓出來的時間 Created_at 會像這樣
Tue Jan 04 05:27:30 +0000 2011
所以要轉換成自己要的格式才能存進資料庫
from datetime import datetime, timedelta created_at = 'Tue Jan 04 05:27:30 +0000 2011' utc_offset = '28800' # +0800 60 * 60 * 8 created_at = datetime.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y') local_time = created_at + timedelta(seconds = int(utc_offset)) print created_at print local_time
Result
2011-01-04 05:27:30
2011-01-04 13:27:30
抓出來的時區是+0000, 所以要再加上UTC_Offset, 才會是本地時間
See Also
Python Documentation - 8.1. datetime — Basic date and time types
在 app engine 使用 pytz
Twitter API Documentation