1

I'm working with a property c++11 library which had its own implementation of Datetime their implementation can be simplified as a struct that contains the fraction of time up to nanoseconds. a rough example will look like:

    struct custom_time_t {
       unsigned year;
       unsigned month;
       unsigned day;
       unsigned hour;
       .......
       long long nanoseconds;
    }

I want to get the epoch from this custom object but also in nanoseconds resolution - similar to gettimeofday.

Getting the epoch up to seconds is typical using std::chrono and std::tm but how to get the nanoseconds since epoch as well?

JeJo
  • 30,635
  • 6
  • 49
  • 88
eyadof
  • 318
  • 3
  • 12

2 Answers2

1

The easiest way is to use this preview of the C++20 <chrono> library.

#include "date/date.h"
#include <chrono>

std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>
to_time_point(custom_time_t const& t)
{
    using namespace date;
    using namespace std::chrono;
    return sys_days{year(t.year)/t.month/t.day} + hours(t.hour) + ...
             + nanoseconds{t.nanoseconds};
}

The return type has the correct value for a system_clock-based time_point. If you extract the value from it with .time_since_epoch().count(), you will get the number of nanoseconds since (or prior to) 1970-01-01 00:00:00.000000000 UTC.

One could also use auto as the return type in C++14, but I wanted to be clear what the return type was. There is also a type alias for this return type: sys_time<nanoseconds>.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
0

I would stick to std::chrono and convert the duration to std::chrono::nanoseconds.

#include <iostream>
#include <chrono>

int main() {
    const auto p0 = std::chrono::time_point<std::chrono::system_clock>{};
    const auto end = std::chrono::system_clock::now();
    const auto diff = end - p0;
    const auto diff_ns = std::chrono::duration_cast< std::chrono::nanoseconds >( diff );
    const auto diff_h = std::chrono::duration_cast< std::chrono::hours >( diff );
    std::cout  << diff_h.count() << "h "  << diff.count() << " s " << diff_ns.count() << "ns\n";
}

Ps. I don't really see the point in storing the values for year, month, ... on your own.

schorsch312
  • 5,553
  • 5
  • 28
  • 57
  • That doesn't really change the resolution to nanoseconds. It is still the resolution of the system clock but with some extra zeros at the end. – user6556709 Jul 17 '19 at 06:03
  • I don't think this is relevant because I'm not interested in the time at the current point `std::chrono::system_clock::now()` but at any point, if I can find a way to construct `std::time_point` at nanoseconds level from the given struct - not owned by me - my problem would be solved. – eyadof Jul 17 '19 at 06:23
  • @ user6556709 You can use `std::chrono::high_resolution_clock`. – schorsch312 Jul 17 '19 at 06:29
  • @eyadof I think this answers your question than. https://stackoverflow.com/questions/52238978/creating-a-stdchronotime-point-from-a-calendar-date-known-at-compile-time – schorsch312 Jul 17 '19 at 06:32
  • @schorsch312 high_resolution_clock:now() is not from epoch. On Linux for example it is from last boot. – user6556709 Jul 17 '19 at 07:01