exa icon indicating copy to clipboard operation
exa copied to clipboard

Handle `TZ` environment variable correctly and add support for `TZDIR`

Open ariasuni opened this issue 3 years ago • 2 comments

I started investigating the handling of timezones and the TZ environment variable for #712.

Turns out documentation on this matter is a sad joke and I had to read glibc (tzset.c and tzfile.c) and musl source code. Here’s what I discovered:

  • An empty TZ indicates a UTC timezone
  • When TZ starts with :, the character is removed from the start of the string
  • When TZ is an absolute path (starts with /), it uses the file directly
  • Otherwise it’s a relative path.
    • glibc searches at the directory described by TZDIR, or /usr/share/zoneinfo if it doesn’t exist
    • musl searches in /usr/share/zoneinfo/, then /share/zoneinfo/, then /etc/zoneinfo/.

Note 1: TZDIR is used by NixOS (it’s not documented anywhere but there are uses in their codebase…). Note 2: At least one Linux distribution is using /etc/zoneinfo, apparently without TZDIR because it uses musl.

ariasuni avatar Apr 25 '21 19:04 ariasuni

In case it's useful, it looks like exa doesn't handle TZ correctly on Fedora 35. It falls under the case "When TZ is an absolute path (starts with /)" + "When TZ starts with :, the character is removed from the start of the string" since I have:

❯ echo "TZ: '$TZ', TZDIR: '$TZDIR'" 
TZ: ':/etc/localtime', TZDIR: ''

Running ls -la ~ outputs "Unable to determine time zone: No such file or directory (os error 2)", while running TZ="Europe/Paris" ls -la ~ does not have any error in the output since:

❯ file /etc/localtime
/etc/localtime: symbolic link to ../usr/share/zoneinfo/Europe/Paris

mx-psi avatar Apr 30 '22 15:04 mx-psi

I already knew all that, but mostly the chrono crate will handle that stuff for me (by relying on the libc of the system) instead of trying to emulate the actual behavior… I just need some time to rereview my code and finally merge it.

ariasuni avatar May 02 '22 16:05 ariasuni

Please open an issue against chrono if there is something we can do to help on our side.

pitdicker avatar Sep 07 '23 10:09 pitdicker

Since exa is unmaintained (see https://github.com/ogham/exa/issues/1243), I’m working to merge my «use-chrono» PR in the active fork eza, see https://github.com/eza-community/eza/pull/227.

ariasuni avatar Sep 08 '23 14:09 ariasuni

It’s been merged in eza!

ariasuni avatar Sep 11 '23 22:09 ariasuni