假設時期與已知的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()
));
}
};
你讀過了[N2661](http://www.open-std.org/jtc1/sc22/ WG21 /文檔/文件/ 2008/n2661.htm)? – ildjarn 2012-02-10 00:10:34
「(...)分數時期時間戳,以」double「表示」什麼單位?你知道這個時代嗎? – 2012-02-10 00:11:40
@ R.MartinhoFernandes:它是自1970年1月1日以來的標準UNIX時代。 – mavam 2012-02-10 00:16:08