如何将分数历元时间戳(双)转换为std::chrono::time_point
How to convert a fractional epoch timestamp (double) to an std::chrono::time_point?
我有一个分数历元时间戳,表示为double
,我想将其转换为适当的std::chrono::time_point
。epoch是自1970年1月1日以来常见的UNIX epoch。我知道存在std::chrono::system_clock::from_time_t
,但time_t
没有分数部分。用C++11实现这一点的最佳方法是什么?
这个问题与到boost::posix_time::ptime的unix时间戳有关,只是它要求的是C++11而不是boost版本。
假设epoch与已知的clock
类型相同,则可以使用具有double
表示的持续时间,并将其转换为该时钟使用的持续时间。
// change period to appropriate units - I'm assuming seconds
typedef std::chrono::duration<double, std::ratio<1>> d_seconds;
d_seconds since_epoch_full(324324.342);
auto since_epoch = std::chrono::duration_cast<clock::duration>(since_epoch_full);
clock::time_point point(since_epoch);
这对于任何涉及该时钟的计算都应该是可以的,因为您使用的精度与时钟相同,但在转换过程中可能会失去一些精度。如果你不想失去这一点,你就必须使用time_point
专门化,它使用基于double
的持续时间类型。然后在计算中使用它(当然,还有浮点数学的所有注意事项)。
typedef std::chrono::time_point<clock, d_seconds> d_time_point;
然而,这将使涉及同一时钟的任何计算复杂化,因为这将需要转换。为了让这更容易,你可以构建自己的时钟包装器来进行转换并使用它:
template <typename Clock>
struct my_clock_with_doubles {
typedef double rep;
typedef std::ratio<1> period;
typedef std::chrono::duration<rep, period> duration;
typedef std::chrono::time_point<my_clock_with_doubles<Clock>> time_point;
static const bool is_steady = Clock::is_steady;
static time_point now() noexcept {
return time_point(std::chrono::duration_cast<duration>(
Clock::now().time_since_epoch()
));
}
static time_t to_time_t(const time_point& t) noexcept {
return Clock::to_time_t(typename Clock::time_point(
std::chrono::duration_cast<typename Clock::duration>(
t.time_since_epoch()
)
));
}
static time_point from_time_t(time_t t) noexcept {
return time_point(std::chrono::duration_cast<duration>(
Clock::from_time_t(t).time_since_epoch()
));
}
};
相关文章:
- 将无符号char*转换为std::istream*C++
- 将 int 数组转换为 std::vector<int*>
- 如何将这个std::字符串转换为std::基本字符串
- 哪些类型可以转换为std::any
- 如何在对<char>C++程序进行逆向工程的同时将 std::basic_string 转换为 Rust 可读值?
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- 将函数参数"const char*"转换为"std::string_view"是
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 字符转换功能 std::isupper() & std::islower() C++17
- 将函数包装器转换为 std::function
- 如何将 std::ifstream 转换为 std::basic_istream<CharT, Traits>&?
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- 如何将唯一指针的 std::vector 转换为原始指针的 std::span?
- std::转换move构造函数的模板专业化的变体
- STD ::转换如何用特定值初始化函子
- 对C++字符串使用 std 转换
- 等价于元组的std::转换
- std::转换为任意容器
- std::转换顺序保证
- std::转换产生奇怪的输出