On Wed, 15 Apr 2026 at 14:39, David Malcolm <[email protected]> wrote:
>
> On Mon, 2026-04-13 at 15:23 +0100, Jonathan Wakely wrote:
> > On Sat, 11 Apr 2026 at 14:33, Alvaro Begue <[email protected]>
> > wrote:
> > >
> > > This series fixes five distinct correctness issues in libstdc++'s
> > > <chrono> tzdb implementation that cause time-zone conversions to
> > > disagree with zic.c / libc localtime_r for specific real-world
> > > zones.
> >
> > This is really great, thanks. Some of the smaller patches might be
> > possible to merge now for GCC 16 but the larger ones will have to
> > wait
> > for GCC 17 (and then maybe get backported later for GCC 16.2).
> >
> > For all significant patches to GCC we require either copyright
> > assignment to the FSF or a DCO sign-off, please see
> > https://gcc.gnu.org/contribute.html#legal and let us know which route
> > you want to take.
> >
> >
> > >
> > > The motivation was a brute-force comparison harness that walks
> > > every
> > > IANA zone hourly across 1850-2150 (~563k transition samples) and
> > > compares chrono::time_zone::to_local() against libc's
> > > localtime_r().
>
> Out of curiosity:
> * is this test harness something you wrote that you can share? (with
> some kind of FLOSS license)
> * how long does it take to run?

I have a similar program (attached to this mail) that compares the
output of std::chrono to the output of
https://github.com/HowardHinnant/date (which uses the same TZif files
as glibc uses).
You need to link it with Howard's library, which is libdate-tz.so in
the date-devel RPM on Fedora.

Sample output:

$ ./compare PRC 1899 55
# 1899-01-01 00:00:00 -37342080 1899-01-01 00:00:00 -37342080
sys_info::first:
end: 1900-12-31 15:54:17 != 1901-01-01 00:00:00
# 1901-01-02 00:00:00 -36289440 1901-01-02 00:00:00 -36289440
sys_info::first:
begin: 1900-12-31 15:54:17 != 1901-01-01 00:00:00
# 1949-05-01 00:00:00 -10872000 1949-05-01 00:00:00 -10872000
sys_info::second:
end: 1949-05-27 15:00:00 != 1949-05-28 00:00:00
# 1949-05-01 16:00:00 -10871040 1949-05-01 16:00:00 -10871040
sys_info::first:
end: 1949-05-27 15:00:00 != 1949-05-28 00:00:00
# 1949-05-29 00:00:00 -10831680 1949-05-29 00:00:00 -10831680
sys_info::first:
begin: 1949-05-27 15:00:00 != 1949-05-28 00:00:00

You can run it in a loop for every time zone like so:

timedatectl list-timezones | while read tz ; do echo $tz ; ./compare
$tz 1700 350 ; done

This takes 3.5 minutes on my laptop.
#include <date/date.h>
#include <date/tz.h>
#include <iostream>
#include <chrono>
#include <cassert>
#define VERIFY assert

using namespace date::literals;
using namespace std::literals::chrono_literals;
using std::chrono::hours;
using std::chrono::minutes;
namespace chrono = std::chrono;

std::string cmp(date::sys_info const& di, chrono::sys_info& ci)
{
  std::ostringstream out;

  if (di.abbrev != ci.abbrev)
      out << di.abbrev << " != " << ci.abbrev << '\n';

  if (di.offset != ci.offset)
    out << "offset: " << di.offset << " != " << ci.offset << '\n';

  if (di.begin != ci.begin)
    out << "begin: " << di.begin << " != " << ci.begin << '\n';

  if (di.end != ci.end)
    out << "end: " << di.end << " != " << ci.end << '\n';

  if ((di.save == 0min) != (ci.save == 0min))
    out << "save: " << di.save << " != " << ci.save << '\n';

  return std::move(out).str();
}

int main(int argc, char** argv)
{
  if (0)
  {
    using namespace std::chrono;
    local_days d(2010y/May/1);
    auto tz = locate_zone("Egypt");
    local_info info = tz->get_info(d);
    VERIFY( info.result == local_info::unique );
    VERIFY( info.first.begin == sys_days(2010y/April/29) + 22h );
    VERIFY( info.first.offset == 3h );
    VERIFY( info.first.save == 0min );
    VERIFY( info.first.abbrev == "EET" );
  }

  // Bugs found with args such as:
  // {"PRC", 1899, 55}
  // {"Egypt", 1850, 60}
  // {"Europe/London", 1900, 100} (but only because libstdc++ doesn't merge adjacent infos)

  std::string_view zone = "Egypt";
  int start_year = 2001;
  int num_years = 20;
  if (argc > 1)
    zone = argv[1];
  if (argc > 2)
    start_year = std::stoi(argv[2]);
  if (argc > 3)
    num_years = std::stoi(argv[3]);

  date::local_time<minutes> dt(date::local_days(date::year(start_year)/date::January/1));
  const date::time_zone* dtz = date::locate_zone(zone);

  chrono::local_time<minutes> ct(chrono::local_days(chrono::year(start_year)/chrono::January/1));
  const chrono::time_zone* ctz = chrono::locate_zone(zone);

  const auto end = ct + chrono::years(num_years);

  while (ct < end)
  {
    if (ct.time_since_epoch() == 18800520min - 150min)
      __builtin_puts("");

    date::local_info di = dtz->get_info(dt);
    chrono::local_info ci = ctz->get_info(ct);

    auto first = cmp(di.first, ci.first);
    auto second = cmp(di.second, ci.second);

    if (first.size() || second.size() || di.result != ci.result)
    {
      std::cout << "# " << dt << ' ' << dt.time_since_epoch().count()
        << ' ' << ct << ' ' << ct.time_since_epoch().count() << '\n';
      if (di.result != ci.result)
        std::cout << "result: " << di.result << " != " << ci.result << '\n';
      if (first.size() != 0)
        std::cout << "sys_info::first:\n" << first;
      if (second.size() != 0)
        std::cout << "sys_info::second:\n" << second;

      auto dd = di.first.end.time_since_epoch().count();
      auto cd = ci.first.end.time_since_epoch().count();
      auto max = std::max(dd, cd);
      dt = time_point_cast<minutes>(date::local_seconds(chrono::seconds(max)));
      ct = time_point_cast<minutes>(chrono::local_seconds(chrono::seconds(max)));
      dt += 24h;
      ct += 24h;
    }
    else
    {
      // std::cout << ci << '\n';
      dt += 30min;
      ct += 30min;
    }
  }
}

Reply via email to