std::chrono...ss::hh_mm_ss(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

std::chrono::hh_mm_ss::hh_mm_ss β€” bestie, it's the constructor of std::chrono::hh_mm_ss, the literal glow-up that turns a duration into hours/minutes/seconds/subseconds πŸ’…

(oh and hey, this page lives in the C++ Standard Libary manual β€” spelling's theirs, not mine 🫢)


✨ Synopsis ✨

Two flavors, both constexpr, okay? Here's the whole thing, verbatim, no cap:

constexpr hh_mm_ss() noexcept : hh_mm_ss{Duration::zero()} {} (1)
constexpr explicit hh_mm_ss( Duration d );                    (2)

Both of them construct a hh_mm_ss object. Iconic. Obsessed.

  1. This one constructs a hh_mm_ss object corresponding to Duration::zero(). Zero. Nothing. The ✨clean slate✨ era. Also it's noexcept, so she will not be throwing tantrums.
  2. This one constructs a hh_mm_ss object corresponding to d, and here's the breakdown that ate, fr fr:
    • is_negative() returns d < Duration::zero().
    • hours() returns std::chrono::duration_cast<std::chrono::hours>(abs(d)).
    • minutes() returns std::chrono::duration_cast<std::chrono::minutes>(abs(d) - hours()).
    • seconds() returns std::chrono::duration_cast<std::chrono::seconds>(abs(d) - hours() - minutes()).
    • subseconds() returns abs(d) - hours() - minutes() - seconds() if std::chrono::treat_as_floating_point_v<precision::rep> is true; otherwise it returns std::chrono::duration_cast<precision>(abs(d) - hours() - minutes() - seconds()).
🫢 little heads-up: constructor (2) is explicit, so no sneaky implicit conversions happening on your watch. She said boundaries.

πŸŽ€ Parameters πŸŽ€

  • d β€” the duration to be broken down πŸ’– (she's getting dismantled, respectfully)

πŸ¦‹ Example πŸ¦‹

// Run this code

#include <chrono>
#include <print>

int main()
{
    std::println("Default constructor: {}",
                 std::chrono::hh_mm_ss<std::chrono::minutes>{});

    std::chrono::time_point now = std::chrono::system_clock::now();
    std::chrono::hh_mm_ss time_of_day{now -
        std::chrono::floor<std::chrono::days>(now)};
    std::println("The time of day is: {}", time_of_day);
}

🌈 Possible output:

Default constructor: 00:00:00
The time of day is: 12:13:14.151617189

(your nanosecond tail will absolutely vary, that's just the universe being quirky πŸ’€)


okay that's the whole page, main character energy complete 🌟 β€” sourced from http://cppreference.com, version 2024.06.10, the std::chrono...ss::hh_mm_ss(3) page. bye bestie, go format some durations πŸ’œπŸŽ€βœ¨

πŸ“¦ where this page comes from