如何创建用于std :: chrono函数的自定义时钟? [英] How to create a custom clock for use in std::chrono functions?
问题描述
我有一些随意的纪元,例如1988年7月13日。本质上,我想测量相对于此的时间。我当时正在考虑编写一个自定义时钟类,以便可以这样编写代码:
I have some arbitrary epoch, like July 13, 1988. Essentially I want to measure the time relative to this. I was thinking of writing a custom clock class, so that I could write code like this:
using std::chrono;
time_point<My_Clock> tp;
std::cout << duration_cast<seconds>(tp.time_since_epoch()).count() << std::endl;
这可能吗?如果不是,最干净的方法是什么?
Is this possible? If not, what's the cleanest way to accomplish this?
推荐答案
编写此自定义时钟的困难之处在于弄清楚如何编写其 now()
函数。在下面的示例中,我将 now()
基于 system_clock
的 now()
。首先,我做了一些侦探性工作,发现我的 system_clock
出现了1970年元旦,忽略了 le秒。这就是 unix时间。事实证明,我知道的每个实现(并且我都已经检查了所有实现)都具有相同的纪元(但这在C ++ 11标准中未指定)。
The hard part of writing this custom clock is figuring out how to write its now()
function. In the example below I base the now()
off of system_clock
's now()
. First I do some detective work to discover that my system_clock
has an epoch of New Years 1970, neglecting leap seconds. This is known as unix time. As it turns out, every implementation I'm aware of (and I think I've checked them all) have this very same epoch (but this is unspecified by the C++11 standard).
接下来,我计算1988-07-13是1970-01-01之后的6768天。使用这两个事实,其余的操作很容易:
Next I compute that 1988-07-13 is 6768 days after 1970-01-01. Using these two facts, the rest is easy:
#include <chrono>
struct My_Clock
{
typedef std::chrono::seconds duration;
typedef duration::rep rep;
typedef duration::period period;
typedef std::chrono::time_point<My_Clock> time_point;
static const bool is_steady = false;
static time_point now() noexcept
{
using namespace std::chrono;
return time_point
(
duration_cast<duration>(system_clock::now().time_since_epoch()) -
hours(6768*24)
);
}
};
MyClock
需要嵌套的typedef来描述其持续时间
, rep
,期间
和 time_point
。根据您的问题,我选择了 seconds
作为持续时间
,但是您可以选择任何内容。
MyClock
needs nested typedefs to describe its duration
, rep
, period
, and time_point
. Based on your question, I've chosen seconds
as the duration
, but you can choose anything you want.
对于 now()
函数,我只调用 system_clock :: now()
并减去以秒为单位的纪元。通过使用 MyClock :: duration
编写所有内容,我对计算有了一点点聪明,以便我可以更轻松地更改 duration
。请注意,我能够从小时
减去时代,这隐式转换为 duration
(即 seconds
)。或者,我可以为自己建立自定义的持续时间
天:
For the now()
function I just call the system_clock::now()
and subtract off the epoch in units of seconds. I got just a little clever with this computation by writing everything in terms of MyClock::duration
so that I can more easily change duration
. Note that I was able to subtract off the epoch in terms of hours
, which implicitly converts to duration
(which is seconds
). Alternatively I could have built myself a custom duration
of days:
typedef std::chrono::duration<int, std::ratio_multiply<std::chrono::hours::period,
std::ratio<24>>> days;
然后返回 now()
可能是这样写的:
And then the return of now()
could have been written:
return time_point
(
duration_cast<duration>(system_clock::now().time_since_epoch()) -
days(6768)
);
无论如何,现在您都可以这样使用:
At any rate, now you can use this like:
#include <iostream>
int
main()
{
using namespace std::chrono;
time_point<My_Clock> tp = My_Clock::now();
std::cout << tp.time_since_epoch().count() << '\n';
}
对我来说这只是打印出来:
Which for me just printed out:
786664963
今天证明了这一点( 2013-06-16)大约是1988-07-13年之后的24.9年。
Which demonstrates that today (2013-06-16) is approximately 24.9 years after 1988-07-13.
这篇关于如何创建用于std :: chrono函数的自定义时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!