如何在使用 chrono 库将数字转换为日期的 cpp 的 main() 中正确调用此模板函数?

发布于 2025-01-09 21:33:06 字数 1954 浏览 0 评论 0原文

如何在使用 chrono 库将数字转换为日期的 cpp 的 main() 中正确调用此模板函数?

 #include <iostream>
 #include <chrono>
 #include <tuple>
 
 //using namespace std;
 // Returns year/month/day triple in civil calendar
 // Preconditions:  z is number of days since 1970-01-01 and is in the range:
 //                   [numeric_limits<Int>::min(), numeric_limits<Int>::max()-719468].
 template <class Int>
 constexpr
 std::tuple<Int, unsigned, unsigned>
 civil_from_days(Int z) noexcept
 {
     static_assert(std::numeric_limits<unsigned>::digits >= 18,
              "This algorithm has not been ported to a 16 bit unsigned integer");
     static_assert(std::numeric_limits<Int>::digits >= 20,
              "This algorithm has not been ported to a 16 bit signed integer");
     z += 719468;
     const Int era = (z >= 0 ? z : z - 146096) / 146097;
     const unsigned doe = static_cast<unsigned>(z - era * 146097);          // [0, 146096]
     const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365;  // [0, 399]
     const Int y = static_cast<Int>(yoe) + era * 400;
     const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100);                // [0, 365]
     const unsigned mp = (5*doy + 2)/153;                                   // [0, 11]
     const unsigned d = doy - (153*mp+2)/5 + 1;                             // [1, 31]
     const unsigned m = mp < 10 ? mp+3 : mp-9;                            // [1, 12]
     return std::tuple<Int, unsigned, unsigned>(y + (m <= 2), m, d);
 }
 
 
 int main(){
    std::cout<< civil_from_days(15432)<<'\n';
 }

给出 this这个(相同)编译器错误。

代码来自 Howard Hinnant

How can I call this template function correctly in main() in cpp which uses chrono library to convert a number to a date?

 #include <iostream>
 #include <chrono>
 #include <tuple>
 
 //using namespace std;
 // Returns year/month/day triple in civil calendar
 // Preconditions:  z is number of days since 1970-01-01 and is in the range:
 //                   [numeric_limits<Int>::min(), numeric_limits<Int>::max()-719468].
 template <class Int>
 constexpr
 std::tuple<Int, unsigned, unsigned>
 civil_from_days(Int z) noexcept
 {
     static_assert(std::numeric_limits<unsigned>::digits >= 18,
              "This algorithm has not been ported to a 16 bit unsigned integer");
     static_assert(std::numeric_limits<Int>::digits >= 20,
              "This algorithm has not been ported to a 16 bit signed integer");
     z += 719468;
     const Int era = (z >= 0 ? z : z - 146096) / 146097;
     const unsigned doe = static_cast<unsigned>(z - era * 146097);          // [0, 146096]
     const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365;  // [0, 399]
     const Int y = static_cast<Int>(yoe) + era * 400;
     const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100);                // [0, 365]
     const unsigned mp = (5*doy + 2)/153;                                   // [0, 11]
     const unsigned d = doy - (153*mp+2)/5 + 1;                             // [1, 31]
     const unsigned m = mp < 10 ? mp+3 : mp-9;                            // [1, 12]
     return std::tuple<Int, unsigned, unsigned>(y + (m <= 2), m, d);
 }
 
 
 int main(){
    std::cout<< civil_from_days(15432)<<'\n';
 }

which gives this or this (the same) compiler errors.

The code is from Howard Hinnant

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2025-01-16 21:33:06

对函数的调用不是问题。您得到的错误(您应该将其包含在问题中)是因为元组没有预定义的输出运算符。不过,您可以打印各个成员:

int main(){
    auto res = civil_from_days(15432);
    std::cout<< std::get<0>(res)<<'\n';
    std::cout<< std::get<1>(res)<<'\n';
    std::cout<< std::get<2>(res)<<'\n';
 }

The call to the function is not the problem. The error you get (which you should have included in the question) is because there is no predefined output operator for tuples. Though, you can print the individual members:

int main(){
    auto res = civil_from_days(15432);
    std::cout<< std::get<0>(res)<<'\n';
    std::cout<< std::get<1>(res)<<'\n';
    std::cout<< std::get<2>(res)<<'\n';
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文