/etc/timezone parsing doesn't deal with comments properly

Bug #1797857 reported by Matthew Vernon
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-tzlocal (Ubuntu)
Fix Released
Undecided
Unassigned

Bug Description

On my system, /etc/timezone is 2 lines, one comment, then a timezone definition:

# Ansible managed: /nfs/users/nfs_p/pj2/Documents/Ansible/2.4.6/roles/core/sanger.localisation/templates/ubu_timezone_file.j2 modified on 2018-09-24 17:30:26 by pj2 on deskpro22189
Europe/London

pytz's _get_localzone code doesn't parse this correctly, resulting in pytz.exceptions.UnknownTimeZoneError: ''

this is because the code first splits on the first space, and then splits the result on the # character (resulting in an empty string); I demonstrate it here by copying the logic from unix.py ll56 et seq:

>>> with open("/etc/timezone", "rb") as tzfile:
... data = tzfile.read()
... etctz = data.strip().decode()
... print("'%s'" % etctz)
... if ' ' in etctz:
... a = etctz.split(' ', 1)
... print(a)
... if '#' in a[0]:
... b=a[0].split('#', 1)
... print(b)
...
'# Ansible managed: /nfs/users/nfs_p/pj2/Documents/Ansible/2.4.6/roles/core/sanger.localisation/templates/ubu_timezone_file.j2 modified on 2018-09-24 17:30:26 by pj2 on deskpro22189
Europe/London'
['#', 'Ansible managed: /nfs/users/nfs_p/pj2/Documents/Ansible/2.4.6/roles/core/sanger.localisation/templates/ubu_timezone_file.j2 modified on 2018-09-24 17:30:26 by pj2 on deskpro22189\nEurope/London']
['', '']

I think it would be better to do a line-by-line read of the file, and discard comment lines (i.e. where first character is #)?

Revision history for this message
Matthew Vernon (matthew-debian) wrote :

Sorry, this is actually a bug in python3-tzlocal.

Colin Watson (cjwatson)
affects: pytz → python-tzlocal (Ubuntu)
Revision history for this message
Edward Betts (edwardbetts) wrote :

I submitted a pull request to fix this bug: https://github.com/regebro/tzlocal/pull/68

Revision history for this message
Edward Betts (edwardbetts) wrote :

This is fixed in 2.0.0b2

Changed in python-tzlocal (Ubuntu):
status: New → Fix Committed
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

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