Mailing List Archive

MacOS python3.9+ IANA Time Zone Support
Hi,
I kindly ask about the status of macoOs python 3.9+ and the support for
IANA time zones:
see https://docs.python.org/3/library/zoneinfo.html

My question is about recognizing the local time zone if given as
'localtime' parameter.
What does the following sequence show:

$ python3
>>> from zoneinfo import ZoneInfo
>>> from datetime import datetime
>>> localtz = ZoneInfo('localtime')
>>> now = datetime.now(localtz)
>>> now.tzname()
'CET'
>>> now.tzinfo
zoneinfo.ZoneInfo(key='localtime')

This should report the name of the the current time zone (in my case 'CET')
and the current
usage of pythons 'tzinfo' parameter.

My plan is to update the Mythtv's python bindings in that direction.

Roland
Re: MacOS python3.9+ IANA Time Zone Support [ In reply to ]
On Wed, Dec 1, 2021 at 3:25 PM Roland Ernst <rcrernst@gmail.com> wrote:

> Hi,
> I kindly ask about the status of macoOs python 3.9+ and the support for
> IANA time zones:
> see https://docs.python.org/3/library/zoneinfo.html
>
> My question is about recognizing the local time zone if given as
> 'localtime' parameter.
> What does the following sequence show:
>
> $ python3
> >>> from zoneinfo import ZoneInfo
> >>> from datetime import datetime
> >>> localtz = ZoneInfo('localtime')
> >>> now = datetime.now(localtz)
> >>> now.tzname()
> 'CET'
> >>> now.tzinfo
> zoneinfo.ZoneInfo(key='localtime')
>
> This should report the name of the the current time zone (in my case
> 'CET') and the current
> usage of pythons 'tzinfo' parameter.
>
> My plan is to update the Mythtv's python bindings in that direction.
>

macOS by default installs older versions of python (on Monterey it installs
3.8).

To get python39 support, you'll need to install from somewhere else (e.g.
macports, homebrew).

When I do my builds, I use macports for a number of reasons that I won't go
into on this thread and I have recently upgraded the build process locally
to move to python3.9.

I just tried running the functions you suggest on my system with macports
python39 and had to add received the following error:

>>> localtz = ZoneInfo('localtime')

Traceback (most recent call last):

File
"/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/zoneinfo/_common.py",
line 12, in load_tzdata

return importlib.resources.open_binary(package_name, resource_name)

File
"/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/resources.py",
line 88, in open_binary

package = _get_package(package)

File
"/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/resources.py",
line 49, in _get_package

module = _resolve(package)

File
"/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/resources.py",
line 40, in _resolve

return import_module(name)

File
"/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py",
line 127, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked

File "<frozen importlib._bootstrap>", line 228, in
_call_with_frames_removed

File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'tzdata'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File
"/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/zoneinfo/_common.py",
line 24, in load_tzdata

raise ZoneInfoNotFoundError(f"No time zone found with key {key}")

zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key
localtime'

Can you point me to the missing python module(s)? I have installed tzdata
as it seemed like it would have been the correct solution to no avail.
Re: MacOS python3.9+ IANA Time Zone Support [ In reply to ]
On Fri, Dec 3, 2021 at 11:56 AM John Hoyt <john.hoyt@gmail.com> wrote:

>
>
> On Wed, Dec 1, 2021 at 3:25 PM Roland Ernst <rcrernst@gmail.com> wrote:
>
>> Hi,
>> I kindly ask about the status of macoOs python 3.9+ and the support for
>> IANA time zones:
>> see https://docs.python.org/3/library/zoneinfo.html
>>
>> My question is about recognizing the local time zone if given as
>> 'localtime' parameter.
>> What does the following sequence show:
>>
>> $ python3
>> >>> from zoneinfo import ZoneInfo
>> >>> from datetime import datetime
>> >>> localtz = ZoneInfo('localtime')
>> >>> now = datetime.now(localtz)
>> >>> now.tzname()
>> 'CET'
>> >>> now.tzinfo
>> zoneinfo.ZoneInfo(key='localtime')
>>
>> This should report the name of the the current time zone (in my case
>> 'CET') and the current
>> usage of pythons 'tzinfo' parameter.
>>
>> My plan is to update the Mythtv's python bindings in that direction.
>>
>
> macOS by default installs older versions of python (on Monterey it
> installs 3.8).
>
> To get python39 support, you'll need to install from somewhere else (e.g.
> macports, homebrew).
>
> When I do my builds, I use macports for a number of reasons that I won't
> go into on this thread and I have recently upgraded the build process
> locally to move to python3.9.
>
> I just tried running the functions you suggest on my system with macports
> python39 and had to add received the following error:
>
> >>> localtz = ZoneInfo('localtime')
>
> Traceback (most recent call last):
>
> File
> "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/zoneinfo/_common.py",
> line 12, in load_tzdata
>
> return importlib.resources.open_binary(package_name, resource_name)
>
> File
> "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/resources.py",
> line 88, in open_binary
>
> package = _get_package(package)
>
> File
> "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/resources.py",
> line 49, in _get_package
>
> module = _resolve(package)
>
> File
> "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/resources.py",
> line 40, in _resolve
>
> return import_module(name)
>
> File
> "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py",
> line 127, in import_module
>
> return _bootstrap._gcd_import(name[level:], package, level)
>
> File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
>
> File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
>
> File "<frozen importlib._bootstrap>", line 972, in
> _find_and_load_unlocked
>
> File "<frozen importlib._bootstrap>", line 228, in
> _call_with_frames_removed
>
> File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
>
> File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
>
> File "<frozen importlib._bootstrap>", line 984, in
> _find_and_load_unlocked
>
> ModuleNotFoundError: No module named 'tzdata'
>
>
> During handling of the above exception, another exception occurred:
>
>
> Traceback (most recent call last):
>
> File "<stdin>", line 1, in <module>
>
> File
> "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/zoneinfo/_common.py",
> line 24, in load_tzdata
>
> raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
>
> zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key
> localtime'
>
> Can you point me to the missing python module(s)? I have installed tzdata
> as it seemed like it would have been the correct solution to no avail.
>
>
That's bad. What does the following command give?
$ python3.9 -m pip list | grep tzdata

I do not know how OS X itself organize the zoneinfo data which one can find
on linux under
/usr/share/zoneinfo

Roland
Re: MacOS python3.9+ IANA Time Zone Support [ In reply to ]
> On 4 Dec 2021, at 3:21 am, Roland Ernst <rcrernst@gmail.com> wrote:
>
> I do not know how OS X itself organize the zoneinfo data which one can find on linux under
> /usr/share/zoneinfo


A bunch of files and directories, similar to most Linuxes,
EXCEPT that there is no localtime symlink:

% ls -p /usr/share/zoneinfo
+VERSION Cuba GMT-0 MET ROK
Africa/ EET GMT0 MST Singapore
America/ EST Greenwich MST7MDT Turkey
Antarctica/ EST5EDT HST Mexico/ UCT
Arctic/ Egypt Hongkong NZ US/
Asia/ Eire Iceland NZ-CHAT UTC
Atlantic/ Etc/ Indian/ Navajo Universal
Australia/ Europe/ Iran PRC W-SU
Brazil/ Factory Israel PST8PDT WET
CET GB Jamaica Pacific/ Zulu
CST6CDT GB-Eire Japan Poland iso3166.tab
Canada/ GMT Kwajalein Portugal posixrules
Chile/ GMT+0 Libya ROC zone.tab




There is usually /etc/localtime:

% ls -l /etc/*time*
lrwxr-xr-x 1 root wheel 36 17 Feb 2017 /etc/localtime -> /usr/share/zoneinfo/Australia/Sydney



but OS X has so many databases, and changes between releases,
that I wouldn’t trust it.

Python developers shouldn’t rely on it.


--
Nigel Pearson, 02 9792 6998, 0408 66 44 35
nigel.pearson.au@gmail.com
Re: MacOS python3.9+ IANA Time Zone Support [ In reply to ]
On Fri, Dec 3, 2021 at 11:22 AM Roland Ernst <rcrernst@gmail.com> wrote:

>
>> That's bad. What does the following command give?
> $ python3.9 -m pip list | grep tzdata
>
> % python3.9 -m pip list | grep tzdata
>
>
> pytzdata 2020.1
>
Re: MacOS python3.9+ IANA Time Zone Support [ In reply to ]
Apologies, my response this morning had some missing info - that's what I
get for trying to reply before my morning coffee soaks in.

macports has a package for pytzdata and I've installed that. Is there a
necessary import command in the python prompt to make sure it gets called
correctly?

>
Re: MacOS python3.9+ IANA Time Zone Support [ In reply to ]
On Sat, Dec 4, 2021 at 1:04 AM John Hoyt <john.hoyt@gmail.com> wrote:

> Apologies, my response this morning had some missing info - that's what I
> get for trying to reply before my morning coffee soaks in.
>
> macports has a package for pytzdata and I've installed that. Is there a
> necessary import command in the python prompt to make sure it gets called
> correctly?
>
>>
>
There are two different python packages 'pytzdata' and 'tzdata'.
The traceback asked for 'tzdata' ( https://pypi.org/project/tzdata/ ).
According this page, you should not need it :
"It is intended to be a fallback for systems that do not have system time
zone data installed (or don’t have it installed in a standard location)."

Anyway, your feedback pointed me to the right direction:
I have to check if the 'zoneinfo' package is available *and* the local time
zone can be resolved.
Otherwise, fall back to the time zone handling provided by the bindings in
'MythTV/utility/dt.py'.

Regards,
Roland