How to look up historical time zones by date and location

Ian Kelly ian.g.kelly at gmail.com
Tue Aug 19 12:25:58 EDT 2014


On Mon, Aug 18, 2014 at 7:31 PM, luofeiyu <elearn2014 at gmail.com> wrote:
> My dear  friends here, all i want is get   ` LMT+8:06:00`  from the output
> of tz1  `<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>`
>
> Shall we get back to the main point?
>
> If you are interested in it ,please say yes or no ,and how to do that ?
>
>
> import pytz,datetime
> tz1 = pytz.timezone('Asia/Shanghai')
> tz1
> <DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>
>>>> str(tz1)
> 'Asia/Shanghai'

This is not a supported use case, so there's no good way to do it that
I can see. What gets printed when you evaluate tz1 is the result of
calling repr(), not str().

>>> repr(tz1)
"<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>"

If you just want the "LMT+8:06:00" as a string, you could call
repr(tz1) and then parse the result with a regex. But like I said,
this is not something that is supported by pytz. Somebody else noted
that the repr is simply showing the first historical entry for the
timezone in the database. Since the first entry is somewhat arbitrary,
not all time zones will show LMT like this, and there is no guarantee
that it won't change in some future update of pytz. In fact, there is
an open bug to fix this behavior:
https://bugs.launchpad.net/pytz/+bug/1319939

We might be able to be more helpful if you would be more clear about
what problem it is that you are trying to solve. Are you trying, for a
given point on the Earth, to determine what nautical time zone it
falls into, or some other "natural" time zone, or its LMT offset, or
something else? For any of those cases, I think the Olson database of
*legal* time zones is not going to be very useful. It would probably
be better to obtain the longitude and do the calculation yourself.



More information about the Python-list mailing list