5

我正在编写一些必须处理 NTP 时间的代码。我决定用 来表示它们std::chrono::duration,我希望这能让我的时间数学更容易做。

NTP 时间由一个无符号的 64 位整数表示,高 32 位为纪元以来的秒数,低 32 位为小数秒。纪元日期是 1900 年 1 月 1 日,但这是与我在这里处理的问题不同的问题,我已经知道如何处理它。对于我正在使用的示例,假设 1970 年 1 月 1 日为一个纪元,以使数学更简单。

持续时间表示很简单:std::chrono::duration<std::uint64_t, std::ratio<1, INTMAX_C(0x100000000)>>. 当我试图在 NTP 时间和我的系统时钟之间进行转换时,问题就出现了。

在我的系统上,std::chrono::system_clock::durationstd::chrono::duration<int64_t, std::nano>. 当我尝试std::chrono::duration_cast在这两个持续时间之间进行施法时,我在任一方向都被大量截断。在调试器中跟踪它,我发现它与duration_cast实现有关,它在 libstdc++、libc++ 和 boost::chrono 中以同样的方式失败。简而言之,要从 system 转换为 ntp 表示,它会乘以 8388608/1953125 的比率,并在另一个方向上乘以相反的比率。它首先乘以分子,然后除以分母。数学是正确的,但初始乘法会溢出 64 位表示并被截断,尽管实际转换(除法后)仍然很容易用这些位表示。

我可以手动进行此转换,但我的问题如下:

  1. 这是实现中的错误,还是只是一个限制?
  2. 如果有限制,我是否应该意识到这将是一个问题?我没有看到任何可能导致能够猜测这不起作用的文档。
  3. 是否有一种通用的方法来实现这一点,而不是同一个问题?
  4. 是否应该报告,向谁报告?
  5. 最后,如果当 C++20 出现时,我使用具有手动策划to_sysfrom_sys功能的 NTP 时钟,我是否能够简单地使用std::chrono::clock_cast而不必担心其他微妙的问题?

这是我用来测试的代码和一些示例输出:

// #define BOOST
#ifdef BOOST
#  include <boost/chrono.hpp>
#  define PREFIX boost
#else
#  include <chrono>
#  define PREFIX std
#endif
#include <cstdint>
#include <iostream>

namespace chrono = PREFIX::chrono;
using PREFIX::ratio;
using PREFIX::nano;

using ntp_dur = chrono::duration<uint64_t, ratio<1, INTMAX_C(0x100000000)>>;
using std_dur = chrono::duration<int64_t, nano>;

int main() {
  auto write = [](auto & label, auto & dur) {
    std::cout << label << ": "
              << std::dec << dur.count()
              << std::hex << " (" << dur.count() << ")" << std::endl;
  };

  auto now = chrono::system_clock::now().time_since_epoch();
  write("now", now);
  std::cout << '\n';

  std::cout << "Naive conversion to ntp and back\n";
  auto a = chrono::duration_cast<std_dur>(now);
  write("std", a);
  auto b = chrono::duration_cast<ntp_dur>(a);
  write("std -> ntp", b);
  auto c = chrono::duration_cast<std_dur>(b);
  write("ntp -> std", c);
  std::cout << '\n';

  std::cout << "Broken down conversion to ntp, naive back\n";
  write("std", a);
  auto d = chrono::duration_cast<chrono::seconds>(a);
  write("std -> sec", d);
  auto e = chrono::duration_cast<ntp_dur>(d);
  write("sec -> ntp sec", e);
  auto f = a - d;
  write("std -> std frac", f);
  auto g = chrono::duration_cast<ntp_dur>(f);
  write("std frac -> ntp frac", f);
  auto h = e + g;
  write("ntp sec + ntp frac-> ntp", h);
  auto i = chrono::duration_cast<std_dur>(h);
  write("ntp -> std", i);
  std::cout << '\n';

  std::cout << "Broken down conversion to std from ntp\n";
  write("ntp", h);
  auto j = chrono::duration_cast<chrono::seconds>(h);
  write("ntp -> sec", j);
  auto k = chrono::duration_cast<std_dur>(j);
  write("src -> std sec", j);
  auto l = h - j;
  write("ntp -> ntp frac", l);
  auto m = chrono::duration_cast<std_dur>(l);
  write("ntp frac -> std frac", m);
  auto n = k + m;
  write("std sec + std frac-> std", n);
}

样本输出:

now: 1530980834103467738 (153f22f506ab1eda)

Naive conversion to ntp and back
std: 1530980834103467738 (153f22f506ab1eda)
std -> ntp: 4519932809765 (41c60fd5225)
ntp -> std: 1052378865369 (f506ab1ed9)

Broken down conversion to ntp, naive back
std: 1530980834103467738 (153f22f506ab1eda)
std -> sec: 1530980834 (5b40e9e2)
sec -> ntp sec: 6575512612832804864 (5b40e9e200000000)
std -> std frac: 103467738 (62acada)
std frac -> ntp frac: 103467738 (62acada)
ntp sec + ntp frac-> ntp: 6575512613277195414 (5b40e9e21a7cdc96)
ntp -> std: 1052378865369 (f506ab1ed9)

Broken down conversion to std from ntp
ntp: 6575512613277195414 (5b40e9e21a7cdc96)
ntp -> sec: 1530980834 (5b40e9e2)
src -> std sec: 1530980834 (5b40e9e2)
ntp -> ntp frac: 444390550 (1a7cdc96)
ntp frac -> std frac: 103467737 (62acad9)
std sec + std frac-> std: 1530980834103467737 (153f22f506ab1ed9)
4

1 回答 1

6
  1. 这是实现中的错误,还是只是一个限制?

只是一个限制。实现的行为符合规范。

  1. 如果有限制,我是否应该意识到这将是一个问题?我没有看到任何可能导致能够猜测这不起作用的文档。

该规范在C++ 标准的23.17.5.7 [time.duration.cast]中。它记录了转换算法的行为,如您在问题中描述的那样。

  1. 是否有一种通用的方法来实现这一点,而不是同一个问题?

在处理非常精细的单位或非常大的范围时,您需要注意溢出错误的可能性。 chrono::duration_cast旨在成为尽可能高效地处理最常见转换的最低级别工具。 chrono::duration_cast尽可能准确,尽可能完全消除分歧。

然而,任何转换算法都不能总是使用有限的存储量来获得任意转换的正确答案。C++17 引入了三种新的转换算法,这些算法建立在duration_cast截断存在的基础上并旨在指导截断的方向:

floor  // truncate towards negative infinity
ceil   // truncate towards positive infinity
round  // truncate towards nearest, to even on tie

您可以编写自己的通用转换函数来处理困难的情况,例如您描述的情况。这种客户提供的转换不太可能适合一般用途。例如:

template <class DOut, class Rep, class Period>
DOut
via_double(std::chrono::duration<Rep, Period> d)
{
    using namespace std::chrono;
    using dsec = duration<long double>;
    return duration_cast<DOut>(dsec{d});
}

上面的示例客户提供的转换从duration<long double>. 这不太容易溢出(尽管不能免疫),通常计算成本更高,并且会遇到精度问题(并且取决于numeric_limits<long double>::digits)。

对我来说(numeric_limits<long double>::digits == 64),1530996658751420125ns往返输入1530996658751420124ns(关闭 1ns)。这个算法可以通过使用roundover来改进duration_cast(同样以更多的计算为代价):

template <class DOut, class Rep, class Period>
DOut
via_double(std::chrono::duration<Rep, Period> d)
{
    using namespace std::chrono;
    using dsec = duration<long double>;
    return round<DOut>(dsec{d});
}

现在我的往返行程非常适合输入1530996658751420125ns. 但是,如果您long double只有 53 位精度,则甚至无法round提供完美的往返。

  1. 是否应该报告,向谁报告?

任何人都可以按照此链接中的说明针对 C++ 标准的库部分提交缺陷报告。此类报告将由 C++ 标准委员会的 LWG 小组委员会审议。可以对其采取行动,也可以将其声明为 NAD(不是缺陷)。如果问题包含提议的措辞(关于您希望如何更改规范的详细说明),它将有更高的成功机会。

即你认为标准应该说什么?

  1. 最后,如果当 C++20 出现时,我使用具有手动策划to_sysfrom_sys功能的 NTP 时钟,我是否能够简单地使用std::chrono::clock_cast而不必担心其他微妙的问题?

找出答案的一种方法是使用现有<chrono>的 C++20扩展规范草案原型进行试验。

于 2018-07-07T21:03:44.520 回答