<tfoot id='GaR3S'></tfoot>
  • <legend id='GaR3S'><style id='GaR3S'><dir id='GaR3S'><q id='GaR3S'></q></dir></style></legend>
      • <bdo id='GaR3S'></bdo><ul id='GaR3S'></ul>

        <i id='GaR3S'><tr id='GaR3S'><dt id='GaR3S'><q id='GaR3S'><span id='GaR3S'><b id='GaR3S'><form id='GaR3S'><ins id='GaR3S'></ins><ul id='GaR3S'></ul><sub id='GaR3S'></sub></form><legend id='GaR3S'></legend><bdo id='GaR3S'><pre id='GaR3S'><center id='GaR3S'></center></pre></bdo></b><th id='GaR3S'></th></span></q></dt></tr></i><div id='GaR3S'><tfoot id='GaR3S'></tfoot><dl id='GaR3S'><fieldset id='GaR3S'></fieldset></dl></div>

        <small id='GaR3S'></small><noframes id='GaR3S'>

        你如何打印 C++11 时间点?

        How do you print a C++11 time_point?(你如何打印 C++11 时间点?)
      1. <small id='7hkN4'></small><noframes id='7hkN4'>

        1. <legend id='7hkN4'><style id='7hkN4'><dir id='7hkN4'><q id='7hkN4'></q></dir></style></legend>
          <i id='7hkN4'><tr id='7hkN4'><dt id='7hkN4'><q id='7hkN4'><span id='7hkN4'><b id='7hkN4'><form id='7hkN4'><ins id='7hkN4'></ins><ul id='7hkN4'></ul><sub id='7hkN4'></sub></form><legend id='7hkN4'></legend><bdo id='7hkN4'><pre id='7hkN4'><center id='7hkN4'></center></pre></bdo></b><th id='7hkN4'></th></span></q></dt></tr></i><div id='7hkN4'><tfoot id='7hkN4'></tfoot><dl id='7hkN4'><fieldset id='7hkN4'></fieldset></dl></div>
          • <tfoot id='7hkN4'></tfoot>
                  <tbody id='7hkN4'></tbody>

                • <bdo id='7hkN4'></bdo><ul id='7hkN4'></ul>

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

                  问题描述

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

                  #include #include <chrono>int main(){//设置time_point为当前时间std::chrono::time_point<std::chrono::system_clock,std::chrono::nanoseconds>时间点;time_point = std::chrono::system_clock::now();//打印时间//...返回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);//不编译

                  错误:

                  /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono: 在 'constexpr 的实例化std::chrono::time_point<_Clock, _Dur>::time_point(const std::chrono::time_point<_Clock, _Dur2>&) [与 _Dur2 = std::chrono::duration<long int, std::比率<1升,1000000000升>>;_Clock = std::chrono::system_clock;_Dur = std::chrono::duration>]’:time.cpp:13:69: 从这里需要/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:540:32: 错误:没有匹配的调用函数'std::chrono::duration>::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: 注意:候选对象是:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:247:14: 注意:模板<class _Rep2,类_Period2,类>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: 注意:模板参数推导/替换失败的:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:243:46: 错误:没有名为type"的类型' 在 'struct std::enable_if<false, void>' 中/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:240:23: 注意:模板<class _Rep2,班级>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: 注意:模板参数推导/替换失败的:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:236:27: 错误:没有名为type"的类型' 在 'struct std::enable_if<false, void>' 中/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:234:12: 注意:constexpr std::chrono::duration<_Rep, _Period>::duration(const std::chrono::duration<_Rep, _Period>&) [与 _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: 注意:没有已知的参数转换1 来自 'std::chrono::time_point>>::duration {aka std::chrono::duration>}' 到 'const std::chrono::duration>&’/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:232:12: 注意: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: 注意:候选人需要 0 个参数,1 提供

                  解决方案

                  (在这篇文章中,为了清楚起见,我将省略 std::chrono:: 限定条件.我相信你知道它们的去向.)

                  您的代码示例无法编译的原因是 system_clock::now() 的返回类型与您尝试将其分配给 (time_point).

                  system_clock::now() 的文档返回值是 system_clock::time_point,这是 time_point 的 typedef;.system_clock::duration 是实现定义的,通常使用 microsecondsnanoseconds.看来你的实现使用了microseconds,所以system_clock::now()的返回类型是time_point.>具有不同持续时间的

                  time_point 不能相互隐式转换,因此会出现编译器错误.

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

                  time_point时间点;time_point = time_point_cast(system_clock::now());

                  注意 time_point_cast 的显式模板参数是目标 duration 类型,而不是目标 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();//不需要 time_point_cast

                  由于这是 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 函数).

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

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

                  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
                  

                  Error:

                  /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
                  

                  解决方案

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

                  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>).

                  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_points with different durations are not implicitly convertible to one another, so you get a compiler error.

                  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());
                  

                  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.

                  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
                  

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

                  auto time_point = system_clock::now(); 
                  

                  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);
                  

                  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 时间点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                  <tfoot id='avFen'></tfoot>
                • <i id='avFen'><tr id='avFen'><dt id='avFen'><q id='avFen'><span id='avFen'><b id='avFen'><form id='avFen'><ins id='avFen'></ins><ul id='avFen'></ul><sub id='avFen'></sub></form><legend id='avFen'></legend><bdo id='avFen'><pre id='avFen'><center id='avFen'></center></pre></bdo></b><th id='avFen'></th></span></q></dt></tr></i><div id='avFen'><tfoot id='avFen'></tfoot><dl id='avFen'><fieldset id='avFen'></fieldset></dl></div>

                      • <bdo id='avFen'></bdo><ul id='avFen'></ul>
                            <tbody id='avFen'></tbody>

                            <small id='avFen'></small><noframes id='avFen'>

                            <legend id='avFen'><style id='avFen'><dir id='avFen'><q id='avFen'></q></dir></style></legend>