如何将std :: chrono :: high_resolution_clock :: now()转换为毫秒,微秒,...?

用户

我从如何获取持续时间中获取了这段代码,就像从<chrono>中获取int milli和float秒?

#include <chrono>
#include <iostream>

int main (int argc, char *argv[])
{
  auto t0 = std::chrono::high_resolution_clock::now();
  auto t1 = std::chrono::high_resolution_clock::now();

  std::chrono::duration< double > fs = t1 - t0;
  std::chrono::milliseconds d = std::chrono::duration_cast< std::chrono::milliseconds >( fs );

  std::cout << fs.count() << "s\n";
  std::cout << d.count() << "ms\n";
}

哪个工作完美,但是我如何使用以下方法创建时间戳:

hour:minute:second:millisecond:microsecond:nanosecond

使用auto t0 = std::chrono::high_resolution_clock::now()值?

我试图打印该auto t0 = std::chrono::high_resolution_clock::now();值,以查看它的内部内容,但是它只给了我一个大错误堆栈:

#include <chrono>
#include <iostream>

int main (int argc, char *argv[])
{
  auto t0 = std::chrono::high_resolution_clock::now();
  std::cout << t0 << "\n";
}

错误:

main2.cpp: In function 'int main(int, char**)':
main2.cpp:10:13: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long long int, std::ratio<1, 1000000000> > >')
   std::cout << t0 << "\n";
   ~~~~~~~~~~^~~~~
塞克普尔

您可以这样打印chrono :: timepoint:

    auto t0 = std::chrono::high_resolution_clock::now();        
    auto nanosec = t0.time_since_epoch();

    std::cout << nanosec.count() << " nanoseconds since epoch" "\n";
    std::cout << nanosec.count()/(1000000000.0 *60.0 *60.0) << " hours since epoch" "\n";

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

std :: chrono :: system_clock与std :: chrono :: high_resolution_clock行为

C++ std::chrono::high_resolution_clock time_since_epoch 返回的数字太小。如何以微秒为单位获得自 1970 年以来的正确时间?

std :: atomic <std :: chrono :: high_resolution_clock :: time_point>无法编译

使用std :: chrono :: high_resolution_clock每秒写入一帧30次

特定于平台的std :: chrono :: high_resolution_clock :: period :: num

使用std :: chrono :: high_resolution_clock时,“ operator =“不匹配

如何将“std::chrono::system_clock::now()”转换为双倍

如何将 std::chrono::high_resolution 类型转换为浮点类型?

在类内使用std :: chrono :: high_resolution_clock播种std :: mt19937的正确方法是什么?

我怎么知道我的`std :: chrono :: high_resolution_clock`对应于`steady_clock`或`system_clock`或其他别名?

C ++打印chrono :: time_point <chrono :: high_resolution_clock>易于阅读

如何将YYYY / MM / DD HH:MM:SS转换为std :: chrono :: system_clock :: time_point?

如何将std :: chrono :: duration转换为double(秒)?

chrono :: high_resolution_clock延迟不一致

如何将chrono :: steady_clock :: duration转换为ostream?

将boost :: chrono :: steady_clock :: time_point转换为std :: chrono :: steady_clock :: time_point

如何在 C++ 中将 Windows FILETIME 转换为 std::chrono::time_point<std::chrono::file_clock>

如何将 std::chrono::duration 转换为提升持续时间

C ++如何将std :: chrono :: time_point转换为long并返回

如何将std :: chrono :: time_point转换为字符串

如何将`std :: chrono :: milliseconds`转换为`boost :: posix_time :: milliseconds`

std :: chrono ::毫秒的向量

将std :: chrono :: system_clock :: time_point转换为struct timeval并返回

使用std :: chrono / date :: gps_clock将double gps时间戳转换为utc / tai

如何将'std :: chrono :: duration <int,std :: ratio <2629746l,1l>>'转换为'int'类型?

相当于std :: chrono :: steady_clock :: now();的python

如何将chrono :: format :: strftime静态转换为chrono :: format :: Item`?

std::chrono::from_stream 可以以微秒精度将字符串转换为 time_point 吗?

将std :: string转换为std :: chrono :: duration