是否可以在 time_point
之间进行隐式/显式转换s 的两个 C++11 时钟?
动机: chrono::duration
s 提供从 epoch 存储时间间隔的方法,概念上不等于 time_point
一个自定义时钟,它自己有一个纪元。
在时钟之间进行隐式转换可以简化 Howard Hinnant 的 date library 的使用。 <date/date.h>
它提供了操作和打印的方法 time_point
s 的系统时钟。
示例:
#include <date/date.h>
using namespace date;
namespace ch = std::chrono;
//
#define EPOCH_OFFSET 100
template<class Duration> using PosixTimePoint =
ch::time_point<ch::system_clock, Duration>;
typedef PosixTimePoint<ch::duration<long,std::micro>> PosixTimePointType;
struct SomeClock{
typedef ch::duration<long,std::micro> duration;
typedef ch::time_point<SomeClock> time_point;
...
static time_point now() noexcept {
using namespace std::chrono;
return time_point (
duration_cast<duration>(
system_clock::now().time_since_epoch()) + date::years(EPOCH_OFFSET) );
}
static PosixTimePoint<duration> to_posix( const time_point& tp ){...}
}
auto tp = SomeClock::now(); //<time_point<SomeClock,ch::duration<long,std::micro>>;
目标:转换
tp
所以std::stream
date.h
的转换工作并打印出当前时间,在我的情况下是:2017-12-24 17:02:56.000000// std::cout << tp; compile error
std::cout << SomeClock::to_posix( tp ); // OK
显式转换:这可以提高可读性,支持语言的转换功能并方便访问
date.h
例程。long time_value = static_cast<long>( tp );
auto st = static_cast<PosixTimePointType>( tp );
std::cout << static_cast<PosixTimePointType>( tp );
最佳答案
我建议模仿 date::utc_clock
的实现或 date::tai_clock
在 tz.h 中找到.例如 utc_clock
实现两个函数来转换 sys_time
:
template<typename Duration>
static
std::chrono::time_point<std::chrono::system_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys(const std::chrono::time_point<utc_clock, Duration>&);
template<typename Duration>
static
std::chrono::time_point<utc_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
from_sys(const std::chrono::time_point<std::chrono::system_clock, Duration>&);
所以你可以想到
std::chrono::system_clock
作为“枢纽”。任何实现这些转换的时钟都可以通过从 system_clock
反弹来转换为实现这些转换的任何其他时钟。在封面下。为了促进反弹, date::clock_cast
介绍。此外,
utc_time
可以用作集线器,如果这对您的类型更有效。例如 tai_clock
实现:template<typename Duration>
static
std::chrono::time_point<utc_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
to_utc(const std::chrono::time_point<tai_clock, Duration>&) NOEXCEPT;
template<typename Duration>
static
std::chrono::time_point<tai_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
from_utc(const std::chrono::time_point<utc_clock, Duration>&) NOEXCEPT;
clock_cast
足够聪明来处理这个“双集线器”系统,所以你可以转换一个时钟,来/从utc_time
, 到另一个使用 sys_time
的时钟作为它的枢纽。如果您还实现
to_stream
对于您的时钟,那么您可以直接使用 format
格式化您的 clock::time_point
.和 clock_cast
可能对您的 to_stream
的实现有用功能。还有
from_stream
可以用来勾你的clock::time_point
高达 date::parse
.搜索 https://howardhinnant.github.io/date/tz.html例如“
clock_cast
”的用法。对于您的用例,to_sys
/from_sys
API 似乎是最有用的。只要这两个功能就可以让你使用 clock_cast
在您的 SomeClock
之间以及 tz.h 中的任何其他时钟(以及满足这些要求的任何其他自定义时钟)。完整演示
#include "date/tz.h"
#include <iostream>
#include <sstream>
struct SomeClock
{
using duration = std::chrono::microseconds;
using rep = duration::rep;
using period = duration::period;
using time_point = std::chrono::time_point<SomeClock>;
static constexpr bool is_steady = false;
static time_point now() noexcept
{
return from_sys(date::floor<duration>(std::chrono::system_clock::now()));
}
static constexpr auto offset = date::sys_days{} - date::sys_days{date::year{1870}/1/1};
template<typename Duration>
static
date::sys_time<Duration>
to_sys(const std::chrono::time_point<SomeClock, Duration>& t)
{
return date::sys_time<Duration>{(t - offset).time_since_epoch()};
}
template<typename Duration>
static
std::chrono::time_point<SomeClock, Duration>
from_sys(const date::sys_time<Duration>& t)
{
return std::chrono::time_point<SomeClock, Duration>{(t + offset).time_since_epoch()};
}
};
template <class Duration>
using SomeTime = std::chrono::time_point<SomeClock, Duration>;
constexpr date::days SomeClock::offset;
template <class CharT, class Traits, class Duration>
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const SomeTime<Duration>& t)
{
return date::to_stream(os, fmt, date::clock_cast<std::chrono::system_clock>(t));
}
template <class CharT, class Traits, class Duration>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const SomeTime<Duration>& t)
{
const CharT fmt[] = {'%', 'F', ' ', '%', 'T', CharT{}};
return to_stream(os, fmt, t);
}
template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
SomeTime<Duration>& tp, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
{
using namespace date;
sys_time<Duration> st;
date::from_stream(is, fmt, st, abbrev, offset);
if (!is.fail())
tp = clock_cast<SomeClock>(st);
return is;
}
int
main()
{
std::cout << SomeClock::now() << '\n';
std::cout << date::format("%a, %b %d, %Y\n", SomeClock::now());
std::istringstream in{"2017-12-24 19:52:30"};
SomeClock::time_point t;
in >> date::parse("%F %T", t);
std::cout << t << '\n';
}
关于date - c++11时钟/时间点之间的隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47962806/