Allow to set different 'zoneinfo' directory

Bug #1373960 reported by mm
20
This bug affects 2 people
Affects Status Importance Assigned to Milestone
pytz
Fix Released
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)
description: updated
Revision history for this message
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
Revision history for this message
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.

Revision history for this message
Stuart Bishop (stub) wrote :

I will apply this patch as supplied. Thanks!

Changed in pytz:
status: Triaged → Fix Committed
Stuart Bishop (stub)
Changed in pytz:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

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