วิธีง่ายที่สุดจัดการ Timezones ใน Python Date/Time คือใช้โมดูล pytz Library นี้ให้ Timezone Calculations ที่แม่นยำและ Cross-platform นำ Olson tz Database เข้า Python แก้ปัญหา Ambiguous Times ตอนจบ Daylight Saving Time
ติดตั้งก่อนใช้:
$ pip install pytz
ตัวอย่าง
from datetime import datetime
from pytz import timezone
format = "%Y-%m-%d %H:%M:%S %Z%z"
# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print(now_utc.strftime(format))
# Convert to Asia/Kolkata
now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))
print(now_asia.strftime(format))
ผลลัพธ์
2018-01-03 07:05:50 UTC+0000 2018-01-03 12:35:50 IST+0530