如何使用年、月、日、小时和分钟构建 std::chrono::time_point?

问题描述 投票:0回答:0

我需要使用年、月、日、小时和分钟构造一个 std::chrono::time_pointThis question 几乎与我的问题重复,但公认的答案并没有帮助我找到解决问题的有效方法。这几乎是从已接受的答案中逐字复制/粘贴的,但无法编译。

#include <chrono>

int main() {
    int y = 2023;
    int m = 2;
    int d = 18;
    int h = 12;
    int M = 50;

    using namespace std::chrono;
    time_point<system_clock, seconds> tp = sys_days{year{y}/month(m)/day(d)} + hours{h} + minutes{M};
}

编译器输出:

$ g++ -std=c++20 date_test.cpp -o date_test
date_test.cpp: In function ‘int main()’:
date_test.cpp:11:50: error: ‘year’ was not declared in this scope
   11 |  time_point<system_clock, seconds> tp = sys_days{year{y}/month(m)/day(d)} + hours{h} + minutes{M};
      |                                                  ^~~~
date_test.cpp:11:50: error: no matching function for call to ‘std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<86400> > >::time_point(<brace-enclosed initializer list>)’
In file included from date_test.cpp:1:
/usr/include/c++/10/chrono:782:14: note: candidate: ‘template<class _Dur2, class> constexpr std::chrono::time_point<_Clock, _Dur>::time_point(const std::chrono::time_point<_Clock, _Dur2>&) [with _Dur2 = _Dur2; <template-parameter-2-2> = <template-parameter-1-2>; _Clock = std::chrono::_V2::system_clock; _Dur = std::chrono::duration<long int, std::ratio<86400> >]’
  782 |    constexpr time_point(const time_point<clock, _Dur2>& __t)
      |              ^~~~~~~~~~
/usr/include/c++/10/chrono:782:14: note:   template argument deduction/substitution failed:
/usr/include/c++/10/chrono:775:21: note: candidate: ‘constexpr std::chrono::time_point<_Clock, _Dur>::time_point(const duration&) [with _Clock = std::chrono::_V2::system_clock; _Dur = std::chrono::duration<long int, std::ratio<86400> >; std::chrono::time_point<_Clock, _Dur>::duration = std::chrono::duration<long int, std::ratio<86400> >]’
  775 |  constexpr explicit time_point(const duration& __dur)
      |                     ^~~~~~~~~~
/usr/include/c++/10/chrono:775:21: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/10/chrono:772:12: note: candidate: ‘constexpr std::chrono::time_point<_Clock, _Dur>::time_point() [with _Clock = std::chrono::_V2::system_clock; _Dur = std::chrono::duration<long int, std::ratio<86400> >]’
  772 |  constexpr time_point() : __d(duration::zero())
      |            ^~~~~~~~~~
/usr/include/c++/10/chrono:772:12: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/10/chrono:762:14: note: candidate: ‘constexpr std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<86400> > >::time_point(const std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<86400> > >&)’
  762 |       struct time_point
      |              ^~~~~~~~~~
/usr/include/c++/10/chrono:762:14: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/10/chrono:762:14: note: candidate: ‘constexpr std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<86400> > >::time_point(std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<86400> > >&&)’
/usr/include/c++/10/chrono:762:14: note:   conversion of argument 1 would be ill-formed:
date_test.cpp:11:49: error: expected primary-expression before ‘{’ token
   11 |  time_point<system_clock, seconds> tp = sys_days{year{y}/month(m)/day(d)} + hours{h} + minutes{M};
      |                                                 ^
date_test.cpp:11:75: error: expected ‘,’ or ‘;’ before ‘+’ token
   11 |  time_point<system_clock, seconds> tp = sys_days{year{y}/month(m)/day(d)} + hours{h} + minutes{M};

date_test.cpp:11:50: error: ‘year’ was not declared in this scope

链接的答案特别指出使用year而不是years,编译器在这里找不到...

如果相关:

$g++ --version
g++ (Debian 10.2.1-6) 10.2.1 20210110

怎么了?我的编译器不够新吗?我也在使用 Howard Hinnant 的(链接问题中接受的答案的作者)tz 库——我可以用它来实现我想做的事情吗?

c++ g++ c++20 c++-chrono
© www.soinside.com 2019 - 2024. All rights reserved.