unsigned long long 到 std::chrono::time_point 并返回

发布于 2025-01-12 05:41:15 字数 334 浏览 0 评论 0原文

我对某件事很困惑。给定:

std::time_t tt = std::time(0);
std::chrono::system_clock::time_point tp{seconds{tt} + millseconds{298}};
std::cout << tp.time_since_epoch().count();

打印类似:16466745672980000(例如)

如何将该数字重新水合回 time_point 对象?我发现自己做了一些奇怪的事情(我不想在这里展示),我想问一下补液的正确方法是什么。

I am very confused about something. Given:

std::time_t tt = std::time(0);
std::chrono::system_clock::time_point tp{seconds{tt} + millseconds{298}};
std::cout << tp.time_since_epoch().count();

prints something like: 16466745672980000 (for example)

How do I rehydrate that number back into a time_point object? I find myself doing kooky things (that I'd rather not show here) and I'd like to ask what the correct way to do the rehydration is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女猫性小仙女 2025-01-19 05:41:15
system_clock::time_point tp{system_clock::duration{16466745672980000}};

system_clock::duration 的单位因平台而异。在您的平台上,我猜它是 1/10 一微秒。

出于序列化目的,您可以记录这是 1/10 微秒的计数。这可以通过以下方式可移植地读回:

long long i;
in >> i;
using D = std::chrono::duration<long long, std::ratio<1, 10'000'000>>;
std::chrono::time_point<std::chrono::system_clock, D> tp{D{i}};

如果本机 system_clock::time_point 的精度与 D 一样好或更精细,您可以隐式转换为它:

system_clock::time_point tp2 = tp;

否则您可以选择通过选择向上舍入、向下舍入、到最接近的舍入或向零舍入来截断它。例如:

system_clock::time_point tp2 = round<system_clock::duration>(tp);
system_clock::time_point tp{system_clock::duration{16466745672980000}};

The units of system_clock::duration vary from platform to platform. On your platform I'm guessing it is 1/10 of a microsecond.

For serialization purposes you could document that this is a count of 1/10 microseconds. This could be portably read back in with:

long long i;
in >> i;
using D = std::chrono::duration<long long, std::ratio<1, 10'000'000>>;
std::chrono::time_point<std::chrono::system_clock, D> tp{D{i}};

If the native system_clock::time_point has precision as fine as D or finer, you can implicitly convert to it:

system_clock::time_point tp2 = tp;

Otherwise you can opt to truncate to it with a choice of rounding up, down, to nearest, or towards zero. For example:

system_clock::time_point tp2 = round<system_clock::duration>(tp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文