你如何打印C ++ 11 time_point?

时间:2013-04-03 00:54:04

标签: c++ templates time c++11 chrono

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

#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

然而,我甚至无法根据我的time_point创建time_t(如示例所示)。

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

7 个答案:

答案 0 :(得分:48)

(在这篇文章中,为了清楚起见,我将省略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 显式转换具有不同持续时间的时间点,因此以下内容将在您的系统上进行编译:

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_point声明为与system_clock::now()返回开头。一种简单的方法是使用system_clock::time_point typedef:

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

由于这是C ++ 11,您也可以使用auto

auto time_point = system_clock::now(); 

解决了这个编译器错误后,转换为time_t的工作正常:

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函数。)

答案 1 :(得分:15)

此代码段可能会对您有所帮助:

#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
}

答案 2 :(得分:11)

nanoseconds似乎是问题的一部分,看一下文档,我能够让它工作:

#include <iostream>
#include <chrono>
#include <ctime>


int main(){

    //set time_point to current time
    std::chrono::time_point<std::chrono::system_clock> time_point;
    time_point = std::chrono::system_clock::now();

    std::time_t ttp = std::chrono::system_clock::to_time_t(time_point);
    std::cout << "time: " << std::ctime(&ttp);

    return 0;
}

虽然看起来std::chrono::microseconds可以正常工作:

std::chrono::time_point<std::chrono::system_clock,std::chrono::microseconds> time_point;

答案 3 :(得分:10)

更新旧问题的答案:

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

但对于system_clock,使用this library,这很简单:

#include "date.h"
#include <iostream>

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

只为我输出:

2016-07-19 03:21:01.910626 UTC

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

答案 4 :(得分:2)

对于使用time_point<steady_clock>(不是time_point<system_clock>)的任何人:

#include <chrono>
#include <iostream>

template<std::intmax_t resolution>
std::ostream &operator<<(
    std::ostream &stream,
    const std::chrono::duration<
        std::intmax_t,
        std::ratio<std::intmax_t(1), resolution>
    > &duration)
{
    const std::intmax_t ticks = duration.count();
    stream << (ticks / resolution) << '.';
    std::intmax_t div = resolution;
    std::intmax_t frac = ticks;
    for (;;) {
        frac %= div;
        if (frac == 0) break;
        div /= 10;
        stream << frac / div;
    }
    return stream;
}

template<typename Clock, typename Duration>
std::ostream &operator<<(
    std::ostream &stream,
    const std::chrono::time_point<Clock, Duration> &timepoint)
{
    typename Duration::duration ago = timepoint.time_since_epoch();
    return stream << ago;
}

int main(){
    // print time_point
    std::chrono::time_point<std::chrono::steady_clock> now =
        std::chrono::steady_clock::now();
    std::cout << now << "\n";

    // print duration (such as the difference between 2 time_points)
    std::chrono::steady_clock::duration age = now - now;
    std::cout << age << "\n";
}

十进制数格式化程序不是最有效的,但不需要预先知道小数位数,如果你想要resolution进行模板化,这是不可知的,除非你能想出一个常量表达式ceil(log10(resolution))

答案 5 :(得分:1)

ctime()不适用于Visual C ++。我使用MS Visual Studio 2013.我根据MSVC编译器的提示更改了上面的代码以使用ctime_s(...)。它奏效了。

//set time_point to current time
std::chrono::time_point<std::chrono::system_clock> time_point;
time_point = std::chrono::system_clock::now();

std::time_t ttp = std::chrono::system_clock::to_time_t(time_point);
char chr[50];
errno_t e = ctime_s(chr, 50, &ttp);
if (e) std::cout << "Error." << std::endl;
else std::cout << chr << std::endl;

答案 6 :(得分:0)

还有另一段代码。 另外,它是相当独立的,并且支持微秒文本表示。

std::ostream& operator<<(std::ostream& stream, const std::chrono::system_clock::time_point& point)
{
    auto time = std::chrono::system_clock::to_time_t(point);
    std::tm* tm = std::localtime(&time);
    char buffer[26];
    std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S.", tm);
    stream << buffer;
    auto duration = point.time_since_epoch();
    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
    auto remaining = std::chrono::duration_cast<std::chrono::nanoseconds>(duration - seconds);
    // remove microsecond cast line if you would like a higher resolution sub second time representation, then just stream remaining.count()
    auto micro = std::chrono::duration_cast<std::chrono::microseconds>(remaining);
    return stream << micro.count();
}