Better behavior of DstTzInfo.__repr__ method

Asked by Hyun-goo Kang

I suggest DstTzInfo instance should display its UTC offset based on current datetime. We could reduce some confusion on inspecting timezone instances by changing behavior of DstTzInfo.__repr__ method. This would make the code more explorable. Many newcomers run REPL and check how the example code work. I guess we should consider them as the majority of use case.

I am expecting your various opinions.

Thank you.

Question information

Language:
English Edit question
Status:
Solved
For:
pytz Edit question
Assignee:
No assignee Edit question
Solved by:
Hyun-goo Kang
Solved:
Last query:
Last reply:
Revision history for this message
Stuart Bishop (stub) said :
#1

What is happening is that there is not a single DstTzInfo for a time zone, but several; one for each distinct period. The __repr__ method is what we have to tell them apart.

>>> from pytz import timezone
>>> from datetime import datetime
>>> timezone('Europe/Paris')
<DstTzInfo 'Europe/Paris' PMT+0:09:00 STD>
>>> timezone('Europe/Paris').localize(datetime.now()).tzinfo
<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>

For newcomers, an alternative is for the DstTzInfo returned from pytz.timezone(...) to be the DstTzInfo for the current time. At the moment, it returns the DstTzInfo for the first period in history. This is almost always not what you want, and forces correct usage to get correct answers. I worry that returning the DstTzInfo for the current time will mean people don't notice their usage error and subtle bugs will end up in their code.

Revision history for this message
Hyun-goo Kang (cyluss) said :
#2

I got the points.