Skip to content

[cpp,c++,std::chrono] time 04: addition, subtraction, multiplication and division of std::chrono

addition, subtraction, multiplication and division of std::chrono::time_point and std::chrono::time_point

  • + : no
  • - : yes, the result type is a chrono::duration type.
  • * : no
  • / : no
Example
#include <chrono>
#include <iostream>
#include <thread>

namespace chrono = std::chrono;

int main() {
    std::cout << "Please wait ...\n";

    auto time = chrono::system_clock::now();

    std::this_thread::sleep_for(std::chrono::milliseconds(2789));

    auto time2 = chrono::system_clock::now();

    auto result1 = time - time2; // result1 type is std::chrono::duration type.
    auto result2 = time2 - time;

    // Visual CPP Only
    std::cout << time << '\n' << time2 << '\n';

    // Visual CPP Only
    std::cout << result1 << '\n' << result2 << '\n';
}

addition, subtraction, multiplication and division of std::chrono::duration and std::chrono::duration

  • + : yes, the result type is a chrono::duration type.
  • - : yes, the result type is a chrono::duration type.
  • * : no
  • / : yes, the result type is a std::int64_t type.
Example
#include <chrono>
#include <iostream>
#include <thread>

namespace chrono = std::chrono;

int main() {
    std::cout << "Please wait ...\n";

    auto time = chrono::system_clock::now();
    std::this_thread::sleep_for(std::chrono::milliseconds(2789));

    auto time2 = chrono::system_clock::now();
    std::this_thread::sleep_for(std::chrono::milliseconds(2789));

    auto time3 = chrono::system_clock::now();

    auto duration1 = time2 - time;

    auto duration2 = time3 - time2;

    auto result1 = duration1 + duration2; // result1 type is std::chrono::duration type.
    auto result2 = duration1 - duration2; // result2 type is std::chrono::duration type.
    auto result3 = duration2 - duration1;
    auto result4 = result1/result2; // result4 type is std::int64_t type.

    // Microsoft Visual CPP Only (operator<< for std::chrono)
    std::cout << result1 << "    /    " << result2 << "    =    "
        << result4 << '\n';

    using T = chrono::duration<chrono::system_clock::rep, chrono::system_clock::period>;

    static_assert(std::same_as<decltype(result1), T>);
    static_assert(std::same_as<decltype(result2), T>);
    static_assert(std::same_as<decltype(result3), T>);
    static_assert(std::same_as<decltype(result4), std::int64_t>);
}

addition, subtraction, multiplication and division of std::chrono::time_point and std::chrono::duration

  • + : time_point + duration : yes, result is time_point
  • + : duration + time_point : yes, result is time_point
  • - : time_point - duration : yes, result is time_point
  • * : no
  • / : no

Example

#include <chrono>
#include <iostream>

namespace chrono = std::chrono;

int main() {
    std::cout << "Please wait ...\n";

    auto start = chrono::system_clock::now();
    auto end = chrono::system_clock::now();
    auto duration = end - start;

    auto time = start + duration;
    auto time2 = duration + start;

    auto time3 = start - duration;

    // Microsoft Visual CPP Only.
    std::cout << time << '\n' << time2 << '\n' << time3 << '\n';

    using cpp_clock = chrono::system_clock;
    using cpp_duration = chrono::duration<cpp_clock::rep, cpp_clock::period>;
    using cpp_time_point = chrono::time_point<cpp_clock, cpp_duration>;

    static_assert(std::same_as<decltype(time), cpp_time_point>);
    static_assert(std::same_as<decltype(time2), cpp_time_point>);
    static_assert(std::same_as<decltype(time3), cpp_time_point>);
}

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options

Submitted comments will be subject to moderation before being displayed.

@cppfx.xyz