เมื่อมี 2 Time Objects ที่มี Timezone ต่างกัน ต้องเข้าใจความแตกต่างระหว่าง Aware และ Naive Objects: Aware Datetime Object เก็บ Timezone Info Naive ไม่มี Timezone Info
วิธีง่ายที่สุดเปรียบเทียบ 2 Aware Objects ว่าบอกเวลาเดียวกันหรือไม่: เปรียบเทียบโดยตรง
ตัวอย่าง
import datetime, pytz
local_tz = pytz.timezone('CET')
utc = datetime.datetime.now(pytz.utc)
local = utc.astimezone(local_tz)
print("UTC:", utc)
print("Local:", local)
print(utc == local)
ผลลัพธ์
UTC: 2018-01-03 17:02:43.632805+00:00 Local: 2018-01-03 18:02:43.632805+01:00 True
แม้เวลาท้องถิ่นต่างกัน (17:02 vs 18:02) แต่แทนเวลา UTC เดียวกัน จึงเท่ากัน