wrong timezone offset on localize

Bug #1809979 reported by Kalyan Bhetwal
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
pytz
New
Undecided
Unassigned

Bug Description

While trying to convert date from local time i.e. Asia/Kathmandu to UTC time, the offset for Kathmandu is taken as +05:41 which is wrong. Below code demonstrates it.

import pytz
from datetime import datetime

date_time = datetime(18,12,28,10,30,19)
utc = pytz.utc
tz = pytz.timezone('Asia/Kathmandu')
dt = tz.localize(date_time)
print dt

The output offset obtained is +05:41 which is wrong since 'Asia/Kathmandu' is +05:45 from UTC

Kalyan Bhetwal (kbwal)
description: updated
tags: added: timezone
summary: - wrong timezone offset for datetime(18,12,28,10,12,22)
+ wrong timezone offset
tags: added: offset wrong
Kalyan Bhetwal (kbwal)
summary: - wrong timezone offset
+ wrong timezone offset on localize
Revision history for this message
Kalyan Bhetwal (kbwal) wrote :

This happens particularly for timezone format of datetime(18,12,28,10,30,19). Is there any solutions or fix to the problem

Revision history for this message
Brian Park (xparks) wrote :

I suspect the problem is your use of "18" to mean "2018". Your print output probably says "0018-12-28 10:30:19+05:41", which is a good hint. I get the following with the corrected year:

>>> date_time = datetime(2018,12,28,10,30,19)
>>> tz = pytz.timezone('Asia/Kathmandu')
>>> dt = tz.localize(date_time)
>>> print(dt)
2018-12-28 10:30:19+05:45

Revision history for this message
Kalyan Bhetwal (kbwal) wrote :

Hi Brian,

Thank you for your reply. I did as you explained above got the correct offset. There are few logs generating devices that generate the logs using such year format. They were creating lots of trouble due to the wrong conversion of log timestamps which are crucial for us to meet our daily compliance needs.

The problem happens only when we use %y(Year without century as a zero-padded decimal number.) format for the year as mentioned in the doc https://docs.python.org/2/library/datetime.html.

Happy New Year 2020.

Kind Regards,
Kalyan

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.