Convert nanoseconds (from midnight) to print time?

I have uint64_t

one representing the number of nanoseconds since midnight. Would std :: chrono let me convert this to meaningful "time", relatively easy?

Also, how would I do it if I have time from the era?

For example, in this format:

14:03:27.812374923

      

And the same situation, but when are the nanoseconds from the epoch given? (in case the answer is significantly different)

+3


source to share


1 answer


You can use Howard Hinnant's free open source library to do this:

#include "date.h"
#include <cstdint>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    using namespace date;

    uint64_t since_midnight = 50607812374923;
    cout << make_time(nanoseconds{since_midnight}) << '\n';

    uint64_t since_epoch = 1499522607812374923;
    cout << sys_time<nanoseconds>{nanoseconds{since_epoch}} << '\n';
}

      

Output:

14:03:27.812374923
2017-07-08 14:03:27.812374923

      



Or did you have to count the jump seconds as since_epoch

?

    cout << utc_time<nanoseconds>{nanoseconds{since_epoch}} << '\n';

2017-07-08 14:03:00.812374923

      

For this last calculation, you will need the "tz.h"

one described here
, and this library is not just a header.

+2


source







All Articles