如何将小数纪元时间戳(双精度)转换为 std::chrono::time_point?
我有一个小数纪元时间戳,表示为 double
,我想将其转换为适当的 std::chrono::time_point
。该纪元是自 1970 年 1 月 1 日以来常见的 UNIX 纪元。我知道存在 std::chrono::system_clock::from_time_t
,但 time_t
没有小数部分。使用 C++11 实现此目的的最佳方法是什么?
这个问题与 unix timestamp to boost::posix_time::ptime 相关,除了它要求 C++11 而不是 Boost 版本。
I have a fractional epoch timestamp, represented as double
, that I would like to convert to an appropriate std::chrono::time_point
. The epoch is the usual UNIX epoch since 1/1/1970. I know that there exists std::chrono::system_clock::from_time_t
, but a time_t
does not have a fractional part. What would be the best way to do this with C++11 means?
This question is related to unix timestamp to boost::posix_time::ptime, except that it's asking for the C++11 rather than Boost version of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设纪元与已知的时钟类型相同,您可以使用带有双精度表示的持续时间,并将其转换为该时钟使用的持续时间。
对于涉及该时钟的任何计算来说,这应该没问题,因为您使用与时钟相同的精度,但它可能会在转换中丢失一些精度。如果您不想失去这一点,则必须使用基于
double
的持续时间类型的time_point
专业化。然后在计算中使用它(当然,要考虑浮点数学的所有注意事项)。然而,这将使涉及同一时钟的任何计算变得复杂,因为它需要转换。为了使这更容易,您可以构建自己的时钟包装器来执行转换并使用它:
Assuming the epoch is the same as a known
clock
type you can use a duration with adouble
representation and convert to the duration used by that clock.This should be ok for any calculation involving that clock, since you're using the same precision as the clock, but it may lose some of the precision in the conversion. If you don't want to lose that you'll have to use a
time_point
specialization that uses thatdouble
-based duration type. And then use that in your calculations (of course, with all the caveats of floating-point math).However, this will complicate any calculations involving that same clock, as it will require conversions. To make this easier, you can build your own clock wrapper that does the conversions and use that: