首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何打印C++11 time_point?

如何打印C++11 time_point?
EN

Stack Overflow用户
提问于 2013-04-03 00:54:03
回答 8查看 99K关注 0票数 80

我创造了一个时间点,但我一直在努力把它打印到终端。

代码语言:javascript
运行
复制
#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;
}

我能找到的打印time_point的唯一文档是在这里:

但是,我甚至无法基于我的time_t (如示例)创建一个time_point。

代码语言:javascript
运行
复制
std::time_t now_c = std::chrono::system_clock::to_time_t(time_point); //does not compile

错误:

代码语言:javascript
运行
复制
/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, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >::duration)’
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:540:32: note: candidates are:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:247:14: note: template<class _Rep2, class _Period2, class> constexpr std::chrono::duration::duration(const std::chrono::duration<_Rep2, _Period2>&)
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:247:14: note:   template argument deduction/substitution failed:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:243:46: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:240:23: note: template<class _Rep2, class> constexpr std::chrono::duration::duration(const _Rep2&)
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:240:23: note:   template argument deduction/substitution failed:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:236:27: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:234:12: note: constexpr std::chrono::duration<_Rep, _Period>::duration(const std::chrono::duration<_Rep, _Period>&) [with _Rep = long int; _Period = std::ratio<1l, 1000000l>]
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:234:12: note:   no known conversion for argument 1 from ‘std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >::duration {aka std::chrono::duration<long int, std::ratio<1l, 1000000000l> >}’ to ‘const std::chrono::duration<long int, std::ratio<1l, 1000000l> >&’
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:232:12: note: constexpr std::chrono::duration<_Rep, _Period>::duration() [with _Rep = long int; _Period = std::ratio<1l, 1000000l>]
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:232:12: note:   candidate expects 0 arguments, 1 provided
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2013-04-03 02:52:38

(在这篇文章中,为了清晰起见,我将省略std::chrono::的资格。我相信你知道他们去哪儿了。)

您的代码示例无法编译的原因是system_clock::now()的返回类型与试图将其分配给(time_point<system_clock, nanoseconds>)的变量类型不匹配。

system_clock::now()的文档中的返回值是system_clock::time_point,它是time_point<system_clock, system_clock::duration>的typedef。system_clock::duration是实现定义的,microsecondsnanoseconds是常用的。您的实现似乎使用microseconds,所以system_clock::now()的返回类型是time_point<system_clock, microseconds>

具有不同持续时间的time_point不能隐式地相互转换,因此您将得到一个编译器错误。

您可以使用time_point_cast显式地转换具有不同持续时间的时间点,以便在您的系统上编译以下内容:

代码语言:javascript
运行
复制
time_point<system_clock, nanoseconds> time_point;
time_point = time_point_cast<nanoseconds>(system_clock::now());

注意,time_point_cast的显式模板参数是目标工期类型,而不是目标time_point类型。时钟类型必须在time_point_cast中匹配,因此指定整个time_point类型(时钟类型和持续时间类型都是模板)是多余的。

当然,在您的情况下,因为您只是想打印时间点,所以不需要它达到任何特定的分辨率,所以您可以只声明time_pointsystem_clock::now()返回的内容相同的类型。要做到这一点,一个简单的方法是使用system_clock::time_point typedef:

代码语言:javascript
运行
复制
system_clock::time_point time_point;
time_point = system_clock::now();  // no time_point_cast needed

因为这是C++11,所以您也可以使用auto

代码语言:javascript
运行
复制
auto time_point = system_clock::now(); 

在解决了这个编译器错误之后,向time_t的转换工作得很好:

代码语言:javascript
运行
复制
std::time_t now_c = std::chrono::system_clock::to_time_t(time_point);

现在您可以使用标准方法来显示time_t值,比如std::ctimestd::strftime。(正如Cassio Neri在对你的问题的评论中指出的那样,GCC还没有支持更多的C++-y std::put_time函数)。

票数 57
EN

Stack Overflow用户

发布于 2016-07-19 03:25:51

一个旧问题的最新答案:

对于一个std::chrono::time_point<std::chrono::system_clock, some-duration>,现在有一个第三方库可以给您更好的控制。对于基于其他时钟的time_points,仍然没有比获取内部表示并打印出来更好的解决方案了。

但是对于system_clock,使用这个图书馆,这是非常容易的:

代码语言:javascript
运行
复制
#include "date.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std::chrono;
    std::cout << system_clock::now() << " UTC\n";
}

刚输出给我的:

代码语言:javascript
运行
复制
2016-07-19 03:21:01.910626 UTC

这是当前UTC的日期和时间到微秒的精度。如果在您的平台上,system_clock::time_point具有纳秒精度,它将为您打印纳秒精度。

2021最新情况

下面是上面程序的C++20版本:

代码语言:javascript
运行
复制
#include <chrono>
#include <iostream>

int
main()
{
    std::cout << std::chrono::system_clock::now() << " UTC\n";
}
票数 25
EN

Stack Overflow用户

发布于 2013-05-22 13:02:50

这段代码片段可能会帮助您:

代码语言:javascript
运行
复制
#include <iomanip>
#include <iostream>
#include <chrono>
#include <ctime>

template<typename Clock, typename Duration>
std::ostream &operator<<(std::ostream &stream,
  const std::chrono::time_point<Clock, Duration> &time_point) {
  const time_t time = Clock::to_time_t(time_point);
#if __GNUC__ > 4 || \
    ((__GNUC__ == 4) && __GNUC_MINOR__ > 8 && __GNUC_REVISION__ > 1)
  // Maybe the put_time will be implemented later?
  struct tm tm;
  localtime_r(&time, &tm);
  return stream << std::put_time(&tm, "%c"); // Print standard date&time
#else
  char buffer[26];
  ctime_r(&time, buffer);
  buffer[24] = '\0';  // Removes the newline that is added
  return stream << buffer;
#endif
}

int main() {
  std::cout << std::chrono::system_clock::now() << std::endl;
  // Wed May 22 14:17:03 2013
}
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15777073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档