Bug 114770 - std::chrono::locate_zone("Asia/Chungking") fails on Debian Sid
Summary: std::chrono::locate_zone("Asia/Chungking") fails on Debian Sid
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 13.2.1
: P3 normal
Target Milestone: 13.4
Assignee: Jonathan Wakely
URL: https://gcc.gnu.org/pipermail/gcc-pat...
Keywords: patch
Depends on:
Blocks:
 
Reported: 2024-04-18 14:55 UTC by Jonathan Wakely
Modified: 2024-05-21 09:20 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-04-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2024-04-18 14:55:08 UTC
#include <chrono>
int main()
{
  (void) std::chrono::locate_zone("Asia/Chungking");
}

With the latest tzdata (version 2024a-2) on Debian Sid this fails:

terminate called after throwing an instance of 'std::runtime_error'
  what():  tzdb: cannot locate zone: Asia/Chungking
Aborted (core dumped)

The problem is a Debian patch that enables link chaining, so that one link can have another link as its target:
https://sources.debian.org/patches/tzdata/2024a-2/ziguard.awk-Move-link-to-link-feature-from-vanguard-to-ma.patch/

This feature was added to tzdata in 2022, but isn't compatible with the expectations of the C++20 standard. When chrono::locate_zone finds a link, it expects its target to be a zone, not another link.
Comment 1 GCC Commits 2024-04-19 20:06:57 UTC
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:eed7fb1b2fe72150cd6af10dd3b8f7fc4f0a4da1

commit r14-10043-geed7fb1b2fe72150cd6af10dd3b8f7fc4f0a4da1
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Apr 18 12:14:41 2024 +0100

    libstdc++: Support link chains in std::chrono::tzdb::locate_zone [PR114770]
    
    Since 2022 the TZif format defined in the zic(8) man page has said that
    links can refer to other links, rather than only referring to a zone.
    This isn't supported by the C++20 spec, which assumes that the target()
    for a chrono::time_zone_link always names a chrono::time_zone, not
    another chrono::time_zone_link.
    
    This hasn't been a problem until now, because there are no entries in
    the tzdata file that chain links together. However, Debian Sid has
    changed the target of the Asia/Chungking link from the Asia/Shanghai
    zone to the Asia/Chongqing link, creating a link chain. The libstdc++
    code is unable to handle this, so chrono::locate_zone("Asia/Chungking")
    will fail with the tzdata.zi file from Debian Sid.
    
    It seems likely that the C++ spec will need a change to allow link
    chains, so that the original structure of the IANA database can be fully
    represented by chrono::tzdb. The alternative would be for chrono::tzdb
    to flatten all chains when loading the data, so that a link's target is
    always a zone, but this means throwing away information present in the
    tzdata.zi input file.
    
    In anticipation of a change to the spec, this commit adds support for
    chained links to libstdc++. When a name is found to be a link, we try to
    find its target in the list of zones as before, but now if the target
    isn't the name of a zone we don't fail. Instead we look for another link
    with that name, and keep doing that until we reach the end of the chain
    of links, and then look up the last target as a zone.
    
    This new logic would get stuck in a loop if the tzdata.zi file is buggy
    and defines a link chain that contains a cycle, e.g. two links that
    refer to each other. To deal with that unlikely case, we use the
    tortoise and hare algorithm to detect cycles in link chains, and throw
    an exception if we detect a cycle. Cycles in links should never happen,
    and it is expected that link chains will be short (if they occur at all)
    and so the code is optimized for short chains without cycles. Longer
    chains (four or more links) and cycles will do more work, but won't fail
    to resolve a chain or get stuck in a loop.
    
    The new test file checks various forms of broken links and cycles.
    
    Also add a new check in the testsuite that every element in the
    get_tzdb().zones and get_tzdb().links sequences can be successfully
    found using locate_zone.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/114770
            * src/c++20/tzdb.cc (do_locate_zone): Support links that have
            another link as their target.
            * testsuite/std/time/tzdb/1.cc: Check that all zones and links
            can be found by locate_zone.
            * testsuite/std/time/tzdb/links.cc: New test.
Comment 2 Jakub Jelinek 2024-05-21 09:20:01 UTC
GCC 13.3 is being released, retargeting bugs to GCC 13.4.