regebro / tzlocal

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

Downgrade multiple timezones to warning instead of error? #165

Open hmaarrfk opened 2 hours ago

hmaarrfk commented 2 hours ago

I'm not sure if this is an ubuntu 24.04 bug or a tzlocal bug, but here are the steps to recreate

$ echo "/etc/timezone $(cat /etc/timezone)" && ls -lah /etc/localtime
/etc/timezone America/New_York
lrwxrwxrwx 1 root root 36 Sep 20 11:11 /etc/localtime -> /usr/share/zoneinfo/America/New_York

Then go to the Ubuntu date and time settings and uncheck automatic timezone and change the timezone to be something else

$ echo "/etc/timezone $(cat /etc/timezone)" && ls -lah /etc/localtime
/etc/timezone America/New_York
lrwxrwxrwx 1 root root 39 Sep 20 08:12 /etc/localtime -> /usr/share/zoneinfo/America/Los_Angeles

Then you can't import your code anymore because the /etc/localtime and /etc/timezone conflict.

Would it be OK to let /etc/localtime take precedence and make the error below a warning instead of an error? Setting environment variables is somewhat annoying.

Thank you for the consideration

https://github.com/user-attachments/assets/a233bc66-246a-4d04-8ddf-911f08c7a36a

  File "/home/mark/code.py", line 542, in add_timestamp
    local_timezone_name = tzlocal.get_localzone_name()
  File "/home/mark/miniforge3/envs/dev/lib/python3.10/site-packages/tzlocal/unix.py", line 209, in get_localzone_name
    _cache_tz_name = _get_localzone_name()
  File "/home/mark/miniforge3/envs/dev/lib/python3.10/site-packages/tzlocal/unix.py", line 159, in _get_localzone_name
    raise zoneinfo.ZoneInfoNotFoundError(message)
zoneinfo._common.ZoneInfoNotFoundError: 'Multiple conflicting time zone configurations found:\n/etc/timezone: America/New_York\n/etc/localtime is a symlink to: America/Los_Angeles\nFix the configuration, or set the time zone in a TZ environment variable.\n'

https://github.com/regebro/tzlocal/blob/52e53725f86fd74e551965de4e3a9f3aee826c8d/tzlocal/unix.py#L159

xref: https://github.com/regebro/tzlocal/issues/127

hmaarrfk commented 2 hours ago

I can confirm that if you run:

sudo dpkg-reconfigure tzdata

and press enter twice, it will fix things..... somewhat annoying....

mark@carbon $ sudo dpkg-reconfigure tzdata
[sudo] password for mark:

Current default time zone: 'America/Los_Angeles'
Local time is now:      Fri Sep 20 08:19:46 PDT 2024.
Universal Time is now:  Fri Sep 20 15:19:46 UTC 2024.

✔ ~
mark@carbon $ echo "/etc/timezone $(cat /etc/timezone)" && ls -lah /etc/localtime
/etc/timezone America/Los_Angeles
lrwxrwxrwx 1 root root 39 Sep 20 08:13 /etc/localtime -> /usr/share/zoneinfo/America/Los_Angeles