Allow to set different 'zoneinfo' directory

Bug #1373960 reported by mm on 2014-09-25
20
This bug affects 2 people
Affects Status Importance Assigned to Milestone
pytz
Undecided
Unassigned

Bug Description

pytz should provide a simple way to set a different 'zoneinfo' directory for the timezone files.

This could be used to switch to the installed OS files or in my case to easier switch zoneinfo versions.

One solution could be to change in the open_resource(name) function in __init__.py :

    filename = os.path.join(os.path.dirname(__file__), 'zoneinfo', *name_parts)

to

    filename = os.path.join(zoneinfo_dir, *name_parts)

and insert on module level

    zoneinfo_dir = os.path.join(os.path.dirname(__file__), 'zoneinfo')

In this way, the calling program is able to set the directory to a different one:

    import pytz
    pytz.zoneinfo_dir = '/usr/share/zoneinfo'

Another option could be to evaluate an environment variable e.g. TZDATADIR and use its string as zoneinfo directory

    zoneinfo_dir = os.environ.get('TZDATADIR', os.path.join(os.path.dirname(__file__), 'zoneinfo') )

A change would support the question discussed in
https://answers.launchpad.net/pytz/+question/236068

mm (mm-softwareag) on 2014-09-25
description: updated
Stuart Bishop (stub) wrote :

An environment variable seems appropriate here, to avoid the issue where a module (eg. a third party dependency) has already been imported and retrieved, causing timezones to appear from data in the unwanted path.

Changed in pytz:
status: New → Triaged
Andriy Pylypenko (bambyster) wrote :

Hello,

I want to confirm the importance of this feature. Because of maintenance costs, it is very useful to have single copy of the zoneinfo for a project written in several programming languages. Luckily the most of the Unix-like systems already have /usr/share/zoneinfo by default and it would be great if the pytz could use it too.

I took the liberty of creating a patch for pytz-2016.7 which implements the ideas from the initial post regarding the environment variable approach. However I used PYTZ_TZDATADIR for variable name.

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

Duplicates of this bug

Other bug subscribers