以特定方式获取时间
getting chrono time in specific way
我有以下C代码:
uint64_t combine(uint32_t const sec, uint32_t const usec){
return (uint64_t) sec << 32 | usec;
};
uint64_t now3(){
struct timeval tv;
gettimeofday(&tv, NULL);
return combine((uint32_t) tv.tv_sec, (uint32_t) tv.tv_usec);
}
它将32位时间戳和32位"某物"(可能是微/纳秒)组合成一个64位整数。
我真的很难用C++11 chrono重写它。
这就是我目前所做的,但我认为这是错误的做法
auto tse = std::chrono::system_clock::now().time_since_epoch();
auto dur = std::chrono::duration_cast<std::chrono::nanoseconds>( tse ).count();
uint64_t time = static_cast<uint64_t>( dur );
重要提示-我只关心第一个32位是"有效"时间戳。
第二个32位的"部分"可以是任何东西——纳米或微秒——只要这个函数的两个顺序调用给我不同的第二个"部分",一切都很好。
我想要一个int中的秒,另一个int的毫秒。
这里有这样做的代码:
#include <chrono>
#include <iostream>
int
main()
{
auto now = std::chrono::system_clock::now().time_since_epoch();
std::cout << now.count() << 'n';
auto s = std::chrono::duration_cast<std::chrono::seconds>(now);
now -= s;
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now);
int si = s.count();
int msi = ms.count();
std::cout << si << 'n';
std::cout << msi << 'n';
}
这只是我的输出:
1447109182307707
1447109182
307
C++11时间类型只使用一个数字来表示自给定Epoch以来的时间,而不像timeval
(或timespec
)结构那样使用两个数字来精确表示时间。因此,使用C++11 chrono,您不需要combine()
方法。
now()
返回的时间戳的内容取决于您使用的时钟;有树时钟,如中所述http://en.cppreference.com/w/cpp/chrono:
system_clock wall clock time from the system-wide realtime clock
steady_clock monotonic clock that will never be adjusted
high_resolution_clock the clock with the shortest tick period available
如果您希望连续的时间戳总是不同的,请使用稳定时钟:
auto t1 = std::chrono::steady_clock::now();
...
auto t2 = std::chrono::steady_clock::now();
assert (t2 > t1);
编辑:回答评论
#include <iostream>
#include <chrono>
#include <cstdint>
int main()
{
typedef std::chrono::duration< uint32_t, std::ratio<1> > s32_t;
typedef std::chrono::duration< uint32_t, std::milli > ms32_t;
s32_t first_part;
ms32_t second_part;
auto t1 = std::chrono::nanoseconds( 2500000000 ); // 2.5 secs
first_part = std::chrono::duration_cast<s32_t>(t1);
second_part = std::chrono::duration_cast<ms32_t>(t1-first_part);
std::cout << "first part = " << first_part.count() << " sn"
<< "seconds part = " << second_part.count() << " ms" << std::endl;
auto t2 = std::chrono::nanoseconds( 2800000000 ); // 2.8 secs
first_part = std::chrono::duration_cast<s32_t>(t2);
second_part = std::chrono::duration_cast<ms32_t>(t2-first_part);
std::cout << "first part = " << first_part.count() << " sn"
<< "seconds part = " << second_part.count() << " ms" << std::endl;
}
输出:
first part = 2 s
seconds part = 500 ms
first part = 2 s
seconds part = 800 ms
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 如何在c++中为模板函数实例创建快捷方式
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何以" Safe "的方式获取 C++ 中的当前时间
- 长度库,其实现方式类似于C++时间的计时
- 从getSystemtime preciseasfiletime()到本地时间可靠的快速方式
- 在编译时间之前抓取常数值
- 在 Linux 中获取时间戳的"最便宜"方式 (c++)
- emacs向下钻取快捷方式
- 我的代码在在线评判上获得了 TLE(超出时间限制),即使我已经根据社论进行了编码
- 跨平台获取一天中的时间的方式
- 在 x 时间后结束循环的性能最低占用方式
- 以编程方式设置唤醒时间
- 以特定方式获取时间
- 什么是最快的方式来获得日期,时间和解析成字符串以及一些其他信息
- 强制输入,或更简单的方式为有限的时间等待用户输入
- c++中静态数据初始化的时间和方式
- 以增量的方式使用z3,但会导致更多的时间
- Linux LibdvbV5 EIT抓取-没有得到足够的时间