Currently, I tried to understand how timezone.localize
work.
naive datetime (without timezone information)
Now, I try to create a naive datetime (without timezone information). I assume everything will respect to UTC
>>> d = datetime.datetime.fromtimestamp(1535500800)
>>> d
datetime.datetime(2018, 8, 29, 0, 0)
>>> time.mktime(d.timetuple())
1535500800.0
>>> d.hour
0
(Screenshot from https://www.epochconverter.com/ , by using 1535500800 as timestamp. Red rectangles are for UTC timezone)
Everything seems fine. Now, I would like to experiment with timezone.localize
datetime with timezone using timezone.localize
>>> d = datetime.datetime.fromtimestamp(1535500800)
>>> d
datetime.datetime(2018, 8, 29, 0, 0)
>>> kl_timezone = timezone('Asia/Kuala_Lumpur')
>>> d = kl_timezone.localize(d)
>>> d
datetime.datetime(2018, 8, 29, 0, 0, tzinfo=<DstTzInfo 'Asia/Kuala_Lumpur' +08+8:00:00 STD>)
>>> time.mktime(d.timetuple())
1535500800.0
>>> d.hour
0
(Screenshot from https://www.epochconverter.com/ , by using 1535500800 as timestamp. Blue rectangles are for Kuala Lumpur timezone)
The time information highlight in blue rectangle is Kuala Lumpur timezone. Hence, I'm expecting after running d = kl_timezone.localize(d)
, d.hour
will return 8.
This is because
- Given 1535500800 timestamp, UTC residents treat it as 12:00 am
- Given 1535500800 timestamp, Kuala Lumpur residents treat it as 8:00 am
But, d.hour
is returning 0, even after I use kl_timezone.localize(d)
.
Isn't d.hour
should return 8, because at timestamp 1535500800, Kuala Lumpur is 8:00 am?
Am I having wrong expectation on timezone.localize
?