c++ - Replace hour of chrono::time_point? - TagMerge
3Replace hour of chrono::time_point?Replace hour of chrono::time_point?

Replace hour of chrono::time_point?

Asked 5 months ago
5
3 answers

The answer depends on exactly what you mean. The simplest interpretation is that you want to take whatever date tp points to (say yyyy-mm-dd hh:MM:ss.fff...), and create: yyyy-mm-dd hour:00:00.000....

Another possible interpretation is that yyyy-mm-dd hh:MM:ss.fff... is transformed into yyyy-mm-dd hour:MM:ss.fff....

In either event C++20 makes this easy, and if you don't yet have access to C++20 <chrono>, then there exists a free, open-source header-only library that emulates C++20 <chrono> and works with C++11/14/17.

If you want to zero the minute, second and subsecond fields as described in the first interpretation that is:

void
set_hour(std::chrono::system_clock::time_point& tp, int hour)
{
    using namespace std::chrono;

    auto day = floor<days>(tp);
    tp = day + hours{hour};
}

I.e. you simply floor the time_point to days-precision and then add the desired hours.

The second interpretation is slightly more complicated:

void
set_hour(std::chrono::system_clock::time_point& tp, int hour)
{
    using namespace std::chrono;

    auto day = floor<days>(tp);
    hh_mm_ss hms{tp - day};
    tp = day + hours{hour} + hms.minutes() + hms.seconds() + hms.subseconds();
}

Here you have to discover and recover the {minutes, seconds, subseconds} fields to re-apply them to the desired date (along with the desired hour). hh_mm_ss is a C++20 {hours, minutes, seconds, subseconds} data structure that automates the conversion from a duration into a field structure so that you can more easily replace the hours field.

Both of these solutions will give the same answer for your example input:

2022-01-01 11:00:00.000000

since the input has zeroed minutes, seconds and subseconds fields already.

Source: link

0

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
 
void slow_motion()
{
    static int a[] {1,2,3,4,5,6,7,8,9,10,11,12};
    while (std::ranges::next_permutation(a).found)
    { } // generates 12! permutations
}
 
int main()
{
    using namespace std::literals; // enables the usage of 24h, 1ms, 1s instead of
                                   // e.g. std::chrono::hours(24), accordingly
 
    const std::chrono::time_point<std::chrono::system_clock> now =
        std::chrono::system_clock::now();
 
    const std::time_t t_c = std::chrono::system_clock::to_time_t(now - 24h);
    std::cout << "24 hours ago, the time was "
              << std::put_time(std::localtime(&t_c), "%F %T.\n") << std::flush;
 
    const std::chrono::time_point<std::chrono::steady_clock> start =
        std::chrono::steady_clock::now();
 
    slow_motion();
 
    const auto end = std::chrono::steady_clock::now();
 
    std::cout
      << "Slow calculations took "
      << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << "µs ≈ "
      << (end - start) / 1ms << "ms ≈ " // almost equivalent form of the above, but
      << (end - start) / 1s << "s.\n";  // using milliseconds and seconds accordingly
}
24 hours ago, the time was 2021-02-15 18:28:52.
Slow calculations took 2090448µs ≈ 2090ms ≈ 2s.

Source: link

0

Syntax
template <class Rep, class Period>
constexpr duration<Rep, Period> abs(duration<Rep, Period> d ); // C++17
Example: abs(duration)
// compile using: /std:c++latest
#include <chrono>
#include <iostream>

int main()
{
    std::cout << abs(-24h);
    return 0;
}
// compile using: /std:c++latest #include <chrono> #include <iostream> int main() { std::cout << abs(-24h); return 0; }
24h
Syntax
template<class ToDuration, class Rep, class Period>
constexpr ToDuration
ceil(const duration<Rep, Period>& d);  // C++17
Syntax
template<class ToDuration, class Clock, class Duration>
constexpr time_point<Clock, ToDuration>
ceil(const time_point<Clock, Duration>& t);  // C++17

Source: link

Recent Questions on c++

    Programming Languages