Tag: c++-chrono
-
Why can adding a month to a valid date result in an invalid date instead of adjusting the result to be valid?
16 The <chrono> library allows dates to silently fall into a state of !ok(). For example: #include <chrono> #include <iostream> int main() { using namespace std; using namespace chrono; auto date = 2023y/October/31; cout << date.ok() << ‘n’; date += months{1}; cout << date.ok() << ‘n’; } Output: 1 0 Demo. I get that Oct…
-
When is it ok to be !ok() with C++20 chrono dates?
10 The <chrono> library allows dates to silently fall into a state of !ok(). For example: #include <chrono> #include <iostream> int main() { using namespace std; using namespace chrono; auto date = 2023y/October/31; cout << date.ok() << ‘n’; date += months{1}; cout << date.ok() << ‘n’; } Output: 1 0 Demo. I get that Oct…
-
fmt format %H:%M:%S without decimals
7 I am trying to format a std::chrono::duration object to the format HH:MM::SS, e.g. 16:42:02 being the hours (16), the minutes (42), and the seconds (2). the library fmt offers useful formatting specifiers for this: using namespace std::chrono; auto start = high_resolution_clock::now(); auto end = start + 4s; fmt::print("{:%H:%M:%S} n", end); which unfortuantely prints the…
-
why use template in year_month class?
17 In MSVC chrono implementation I see the following code _EXPORT_STD template <int = 0> _NODISCARD constexpr year_month operator+(const year_month& _Left, const months& _Right) noexcept { const auto _Mo = static_cast<long long>(static_cast<unsigned int>(_Left.month())) + (_Right.count() – 1); const auto _Div = (_Mo >= 0 ? _Mo : _Mo – 11) / 12; return year_month{_Left.year() +…