如何打印C ++ 11 time_point? [英] How do you print a C++11 time_point?

查看:244
本文介绍了如何打印C ++ 11 time_point?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个时间点,但是我一直在努力将其打印到终端上。

I've created a time point, but I have been struggling to print it to the terminal.

#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的文档:
http://en.cppreference.com/w/cpp/chrono/time_point

The only documentation I can find that prints a time_point is found here: http://en.cppreference.com/w/cpp/chrono/time_point

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

however, I'm not even able to create a time_t based on my time_point(like the example).

std::time_t now_c = std::chrono::system_clock::to_time_t(time_point); //does not compile

错误:

/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


推荐答案

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

(In this post I will omit std::chrono:: qualifications for clarity. I trust you know where they go.)

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

The reason your code example fails to compile is that there is a mismatch between the return type of system_clock::now() and the type of variable you are trying to assign this to (time_point<system_clock, nanoseconds>).

system_clock :: now()的返回值是 system_clock :: time_point ,它是<$ c $的typedef c> time_point< system_clock,system_clock :: duration> 。 system_clock :: duration 是实现定义的,具有 microseconds nanoseconds 常用。看来您的实现使用了 microseconds ,所以 system_clock :: now()的返回类型是 time_point< system_clock,microseconds>

The documented return value of system_clock::now() is system_clock::time_point, which is a typedef for time_point<system_clock, system_clock::duration>. system_clock::duration is implementation-defined, with microseconds and nanoseconds being commonly used. It seems that your implementation uses microseconds, so the return type of system_clock::now() is time_point<system_clock, microseconds>.

time_point s具有不同持续时间是不能相互隐式转换,因此会出现编译器错误。

time_points with different durations are not implicitly convertible to one another, so you get a compiler error.

您可以使用<$ c $ 显式转换不同持续时间的时间点c> time_point_cast ,因此以下内容将在您的系统上编译:

You can explicitly convert time points with different durations using time_point_cast, so the following would compile on your system:

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类型(这是在时钟类型和持续时间类型上的模板)将是多余的。

Notice the explicit template parameter to time_point_cast is the target duration type, not the target time_point type. The clock types must match in a time_point_cast, so specifying the entire time_point type (which is templated on both the clock type and the duration type) would be redundant.

当然,在您的情况下,由于您只是想打印时间点,因此不需要将其设置为任何特定的分辨率,因此您可以声明 time_point 的类型与 system_clock :: now()开头的类型相同。一种简单的方法是使用 system_clock :: time_point typedef:

Of course in your case, since you are just looking to print the time point, there is no need for it to be at any specific resolution, so you can just declare time_point to be the same type as what system_clock::now() returns to begin with. A simple way to do that is to use the system_clock::time_point typedef:

system_clock::time_point time_point;
time_point = system_clock::now();  // no time_point_cast needed

由于这是C ++ 11,因此也可以只使用自动

Since this is C++11, you can also just use auto:

auto time_point = system_clock::now(); 

解决了此编译器错误,转换为 time_t 正常运行:

Having solved this compiler error, the conversion to time_t works just fine:

std::time_t now_c = std::chrono::system_clock::to_time_t(time_point);

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

and you can now use standard methods for displaying time_t values, like std::ctime or std::strftime. (As Cassio Neri points out in a comment to your question, the more C++-y std::put_time function is not yet supported by GCC).

这篇关于如何打印C ++ 11 time_point?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆