regebro / tzlocal

A Python module that tries to figure out what your local timezone is
MIT License
184 stars 58 forks source link

`/etc/timezone` comment handling doesn't work #65

Closed mcv21 closed 5 years ago

mcv21 commented 5 years ago

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 #)?

regebro commented 5 years ago

First time I've seen a comment in those files.

Informal standards... So yeah, we gotta discard those lines then, if Ansible start putting comments in them.

regebro commented 5 years ago

This is merged now, so that should hopefully work now.

mcv21 commented 5 years ago

Thanks :)