C++20 计时类型/值month{7}和months{7}? 有两个如此相似的名字是不是很混乱?
std::system_clock和之间有什么区别std::steady_clock?(说明不同结果/行为的示例案例会很棒).
如果我的目标是精确测量的功能(如基准)的执行时间,这将是之间是最好的选择std::system_clock,std::steady_clock和std::high_resolution_clock?
我正在尝试使用计时库来定时器和持续时间.
我希望能够Duration frameStart;(从app开始)和a Duration frameDelta;(帧之间的时间)
我需要能够以frameDelta毫秒和浮点秒的形式获得持续时间.
你如何使用新的c ++ 11 <chrono>库做到这一点?我一直在努力和谷歌搜索(信息稀疏).代码是模板化的,需要特殊的演员和东西,我无法弄清楚如何正确使用这个库.
如何找出编译器在使用auto关键字时推断出的类型?
示例1:更简单
auto tickTime = 0.001;
Run Code Online (Sandbox Code Playgroud)
这被推断为a float或adouble?
例2:更复杂(以及我目前的头痛):
typedef std::ratio<1, 1> sec;
std::chrono::duration<double, sec > timePerTick2{0.001};
auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2;
Run Code Online (Sandbox Code Playgroud)
什么类型nextTickTime?
我遇到的问题是当我尝试发送nextTickTime时std::cout.我收到以下错误:
./main.cpp: In function ‘int main(int, char**)’:
./main.cpp:143:16: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
std::cout << std::setprecision(12) << nextTickTime << std::endl; // time in seconds
^
In file included from /usr/include/c++/4.8.2/iostream:39:0,
from ./main.cpp:10:
/usr/include/c++/4.8.2/ostream:602:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) …Run Code Online (Sandbox Code Playgroud) 我需要转换std::chrono::time_point为一个long类型(整数64位).我正在开始std::chrono......
这是我的代码:
int main ()
{
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
auto epoch = now.time_since_epoch();
auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
long duration = value.count();
std::chrono::duration<long> dur(duration);
std::chrono::time_point<std::chrono::system_clock> dt(dur);
if (dt != now)
std::cout << "Failure." << std::endl;
else
std::cout << "Success." << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
此代码编译,但不显示成功.
为什么dt不同于now最后?
该代码缺少什么?
我试图使用GCC(测试版本4.5.1,4.6.3,4.8.4)编译此示例程序:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
using std::chrono::system_clock;
int main()
{
system_clock::time_point now = system_clock::now();
std::time_t now_c = system_clock::to_time_t(
now - std::chrono::hours(24));
std::cout << "One day ago, the time was "
<< std::put_time(std::localtime(&now_c), "%F %T") << '\n';
}
Run Code Online (Sandbox Code Playgroud)
prog.cpp: In function 'int main()':
prog.cpp:14:18: error: 'put_time' is not a member of 'std'
Run Code Online (Sandbox Code Playgroud)
我想,可能还没有实现.所以我试着检查这个功能的实现状态.我只找到了这个页面:
但我找不到任何笔记put_time或chrono或相似.任何人都可以指向我提供有关此库的实现状态的信息的资源吗?
我一直在升级一些旧代码,并且尽可能地尝试更新到c ++ 11.以下代码是我用来在程序中显示时间和日期的方法
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
const std::string return_current_time_and_date() const
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
return buf;
}
Run Code Online (Sandbox Code Playgroud)
我想使用std :: chrono(或类似的)以类似的格式输出当前时间和日期,但我不确定如何这样做.任何帮助将不胜感激.谢谢
由于C++ 11的文档不足,我遇到了一个小问题.
我想获得一个自纪元以来的时间,以毫秒为单位,或纳秒或秒,然后我必须将此值"转换"为另一个分辨率.我可以使用gettimeofday()来做它,但它很容易,所以我尝试使用std :: chrono来实现它.
我试过了:
std::chrono::time_point<std::chrono::system_clock> now =
std::chrono::system_clock::now();
Run Code Online (Sandbox Code Playgroud)
但是我不知道以这种方式获得的分辨率是什么time_point,我不知道如何将这个时间作为一个简单的无符号long long,我没有任何概念如何将它转换为另一个分辨率.
我已经创建了一个时间点,但我一直在努力将它打印到终端.
#include <iostream>
#include <chrono>
int main(){
//set time_point to current time
std::chrono::time_point<std::chrono::system_clock,std::chrono::nanoseconds> time_point;
time_point = std::chrono::system_clock::now();
//print the time
//...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我可以在这里找到打印time_point的唯一文档:http://en.cppreference.com/w/cpp/chrono/time_point
但是,我甚至无法根据我的time_point创建time_t(如示例所示).
std::time_t now_c = std::chrono::system_clock::to_time_t(time_point); //does not compile
Run Code Online (Sandbox Code Playgroud)
错误:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono: In instantiation of ‘constexpr std::chrono::time_point<_Clock, _Dur>::time_point(const std::chrono::time_point<_Clock, _Dur2>&) [with _Dur2 = std::chrono::duration<long int, std::ratio<1l, 1000000000l> >; _Clock = std::chrono::system_clock; _Dur = std::chrono::duration<long int, std::ratio<1l, 1000000l> >]’:
time.cpp:13:69: required from here
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:540:32: error: no matching function for call to ‘std::chrono::duration<long int, std::ratio<1l, 1000000l> >::duration(std::chrono::time_point<std::chrono::system_clock, …Run Code Online (Sandbox Code Playgroud) 如何将std :: chrono :: time_point转换为带有小数秒的日历日期时间字符串?例如:"10-10-2012 12:38:40.123456".