https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87493

            Bug ID: 87493
           Summary: chrono::system_clock unusable with std::tm due to
                    misaligned precisions
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vini.ipsmaker at gmail dot com
  Target Milestone: ---

This code doesn't work:

  std::tm cal_time;
  cal_time.tm_year = year;
  cal_time.tm_mon = month;
  cal_time.tm_mday = day;
  cal_time.tm_hour = hour;
  cal_time.tm_min = min;
  cal_time.tm_sec = sec;
  std::time_t t1, t2;
  t1 = timegm(&cal_time);
  auto mydt = std::chrono::system_clock::from_time_t(t1);
  t2 = std::chrono::system_clock::to_time_t(mydt);
  assert(t1 == t2);

I tracked the problem down to libstdc++ sources codes.

system_clock::duration is nanoseconds:
https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/std/chrono;h=871c896144a23f37de858668a16388a4bc884d56;hb=HEAD#l830

and nanoseconds is backed by int64_t:
https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/std/chrono;h=871c896144a23f37de858668a16388a4bc884d56;hb=HEAD#l605

However, seconds is used to convert between time_t and
chrono::system_clock::time_point:
https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/std/chrono;h=871c896144a23f37de858668a16388a4bc884d56;hb=HEAD#l855

And seconds is backed by a int64_t too:
https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/std/chrono;h=871c896144a23f37de858668a16388a4bc884d56;hb=HEAD#l614

There is no way to preserve time_t value between conversions involving
chrono::system_clock::time_point and time_t.

Reply via email to