https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122567
Bug ID: 122567
Summary: std::chrono::current_zone() fails to resolve timezone
from /etc/localtime if it has three components
Product: gcc
Version: 15.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: ljbousfield at gmail dot com
Target Milestone: ---
Created attachment 62713
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62713&action=edit
A small C++ program to print out std::chrono::current_zone()->name()
For a minimal reproduction of this issue, download the attached
current-zone.cpp and follow the below shell commands. This reproduction was
tested against tzdata 2025b, though similar reproductions should exist if these
exact zones don't exist on your system. These commands overwrite
`/etc/localtime` so you may want to make a backup of that symlink to restore
afterwards.
$ g++ -std=gnu++20 current-zone.cpp -o current-zone
$ sudo ln -sf /usr/share/zoneinfo/America/Indiana/Indianapolis /etc/localtime
$ date +%Z
EST
$ ./current-zone
std::chrono::current_zone()->name() = Etc/UTC
$ sudo ln -sf /usr/share/zoneinfo/America/Indianapolis /etc/localtime
$ ./current-zone
std::chrono::current_zone()->name() = America/Indiana/Indianapolis
Notice how in the first ./current-zone invocation, the /etc/localtime path has
three components after zoneinfo and std::chrono::current_zone() fails to
resolve it. In the second ./current-zone invocation, it succeeds and picks up
the timezone, because even though it's an alias of the same zone name (note how
its output uses the three component name) the symlink only has two zone
components. I believe this is due to the libstdc++ `tzdb::current_zone`
implementation only looking two directories up from the resolved /etc/localtime
path.
As far as I'm aware, this affects all gcc versions, including latest master.
This can be reproduced in the `archlinux:latest` or `debian:trixie` docker
images for instance. Some distros vary where exactly the zoneinfo is or what
zones it has, but I've been able to replicate this issue on every Linux distro
I've tried. I've also verified that this bug reproduces the same way with no
warnings when compiling with `-D_GLIBCXX_ASSERTIONS` and `-D_GLIBCXX_DEBUG`.
This is similar to bug 108211 but with three components instead of one. As far
as I'm aware, there isn't an existing zoneinfo with four or more components.
There's e.g. `/usr/share/zoneinfo/right/America/Indiana/Indianapolis` but
libstdc++ doesn't seem to recognize `right/` timezones in the first place
anyways (separate bug or out of scope for std::chrono?).