regebro / tzlocal

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

'local' zone cannot be copied #18

Closed tbabej closed 9 years ago

tbabej commented 9 years ago

If you try to copy the 'local' zone object, or any object that contains it, you get the following failure:

>>> tzlocal.get_localzone()
<DstTzInfo 'local' PMT+0:58:00 STD>
>>> zone = tzlocal.get_localzone()
>>> import copy
>>> copy.deepcopy(zone)                                                                            
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib64/python2.7/copy.py", line 329, in _reconstruct
    y = callable(*args)
  File "/usr/lib/python2.7/site-packages/pytz/__init__.py", line 286, in _p
    return unpickler(*args)
  File "/usr/lib/python2.7/site-packages/pytz/tzinfo.py", line 524, in unpickler
    tz = pytz.timezone(zone)
  File "/usr/lib/python2.7/site-packages/pytz/__init__.py", line 180, in timezone
    raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'local'
regebro commented 9 years ago

I guess that should be fixable. But quick question: Why do you need to copy it?

tbabej commented 9 years ago

Oh, I hinted that in the description. Localized datetime is a attribute of a object which is being copied. The above is just a minimal example.

regebro commented 9 years ago

In fact, this is not trivially fixable, because of how pytz works.

However, systemd distributions will now no longer return the "local" zone, so it should be less of a problem.

tbabej commented 9 years ago

I feared as much - could not find a trivial solution myself before reporting. I agree it shouldn't make much trouble as 'local' timezone should be encountered much less frequently.

Thanks for taking the time to investigate!