Skip to main content
Became Hot Network Question
added 11 characters in body
Source Link
wohlstad
  • 37k
  • 18
  • 79
  • 114

After running this program   
(compiled with MSVC compiler 19.50.35723 with option /std:c++23preview/std:c++23preview)

#include <print>
#include <chrono>
#include <string>
int main() {
    using namespace std::chrono;
    system_clock::time_point tp1;
    system_clock::time_point tp2;

    std::string y1 = "01.01.2024";
    std::string y2 = "01.01.2025";
    
    std::istringstream is1{ y1.data() };
    is1 >> parse("%d.%m.%Y", tp1);
    std::istringstream is2{ y2.data() };
    is2 >> parse("%d.%m.%Y", tp2);

    std::println("{}", tp1);
    std::println("{}", tp2);

    auto duration = duration_cast<years>(tp2 - tp1);
    std::println("duration: {} year(s)", duration);

    return 0;
}
#include <print>
#include <chrono>
#include <string>
int main() {
    using namespace std::chrono;
    system_clock::time_point tp1;
    system_clock::time_point tp2;

    std::string y1 = "01.01.2024";
    std::string y2 = "01.01.2025";
    
    std::istringstream is1{ y1.data() };
    is1 >> parse("%d.%m.%Y", tp1);
    std::istringstream is2{ y2.data() };
    is2 >> parse("%d.%m.%Y", tp2);

    std::println("{}", tp1);
    std::println("{}", tp2);

    auto duration = duration_cast<years>(tp2 - tp1);
    std::println("duration: {} year(s)", duration);

    return 0;
}

I got the following result:

2024-01-01 00:00:00.0000000
2025-01-01 00:00:00.0000000
duration: 1[31556952]s year(s)
2024-01-01 00:00:00.0000000
2025-01-01 00:00:00.0000000
duration: 1[31556952]s year(s)

I was expecting a duration of exactly 1 year without fractional seconds, can someone help me out here?

After running this program  (compiled with MSVC compiler 19.50.35723 with option /std:c++23preview)

#include <print>
#include <chrono>
#include <string>
int main() {
    using namespace std::chrono;
    system_clock::time_point tp1;
    system_clock::time_point tp2;

    std::string y1 = "01.01.2024";
    std::string y2 = "01.01.2025";
    
    std::istringstream is1{ y1.data() };
    is1 >> parse("%d.%m.%Y", tp1);
    std::istringstream is2{ y2.data() };
    is2 >> parse("%d.%m.%Y", tp2);

    std::println("{}", tp1);
    std::println("{}", tp2);

    auto duration = duration_cast<years>(tp2 - tp1);
    std::println("duration: {} year(s)", duration);

    return 0;
}

I got the following result

2024-01-01 00:00:00.0000000
2025-01-01 00:00:00.0000000
duration: 1[31556952]s year(s)

I was expecting a duration of exactly 1 year without fractional seconds, can someone help me out here?

After running this program 
(compiled with MSVC compiler 19.50.35723 with option /std:c++23preview)

#include <print>
#include <chrono>
#include <string>
int main() {
    using namespace std::chrono;
    system_clock::time_point tp1;
    system_clock::time_point tp2;

    std::string y1 = "01.01.2024";
    std::string y2 = "01.01.2025";
    
    std::istringstream is1{ y1.data() };
    is1 >> parse("%d.%m.%Y", tp1);
    std::istringstream is2{ y2.data() };
    is2 >> parse("%d.%m.%Y", tp2);

    std::println("{}", tp1);
    std::println("{}", tp2);

    auto duration = duration_cast<years>(tp2 - tp1);
    std::println("duration: {} year(s)", duration);

    return 0;
}

I got the following result:

2024-01-01 00:00:00.0000000
2025-01-01 00:00:00.0000000
duration: 1[31556952]s year(s)

I was expecting a duration of exactly 1 year without fractional seconds, can someone help me out here?

Source Link
Angle.Bracket
  • 1.8k
  • 1
  • 16
  • 38

How can I format a duration with std::println?

After running this program (compiled with MSVC compiler 19.50.35723 with option /std:c++23preview)

#include <print>
#include <chrono>
#include <string>
int main() {
    using namespace std::chrono;
    system_clock::time_point tp1;
    system_clock::time_point tp2;

    std::string y1 = "01.01.2024";
    std::string y2 = "01.01.2025";
    
    std::istringstream is1{ y1.data() };
    is1 >> parse("%d.%m.%Y", tp1);
    std::istringstream is2{ y2.data() };
    is2 >> parse("%d.%m.%Y", tp2);

    std::println("{}", tp1);
    std::println("{}", tp2);

    auto duration = duration_cast<years>(tp2 - tp1);
    std::println("duration: {} year(s)", duration);

    return 0;
}

I got the following result

2024-01-01 00:00:00.0000000
2025-01-01 00:00:00.0000000
duration: 1[31556952]s year(s)

I was expecting a duration of exactly 1 year without fractional seconds, can someone help me out here?