0

The code most like

  uint64_t nanoseconds = 0;
  auto nano = std::chrono::nanoseconds(nanoseconds_);
  system_clock::time_point tp(nano);

It report error(QNX):

time.cpp:86:35: error: no matching function for call to 'std::__1::chrono::time_point<std::__1::chrono::system_clock>::time_point(std::__1::chrono::duration<long long int, std::__1::ratio<1l, 1000000000l> >&) 
qnx700/target/qnx7/usr/include/c++/v1/chrono:769:5: note: candidate: 
template<class _Duration2> std::__1::chrono::time_point<_Clock, _Duration>::time_point(const std::__1::chrono::time_point<_Clock, _Duration2>&,
typename std::__1::enable_if<std::__1::is_convertible<_Duration2, 
_Duration>::value>::type*) time_point(const time_point<clock, _Duration2>& t,
^ qnx700/target/qnx7/usr/include/c++/v1/chrono:769:5: note: template argument 
deduction/substitution failed: time.cpp:86:35: note: 
'std::__1::chrono::duration<long long int, std::__1::ratio<1l, 1000000000l> >'
 is not derived from 'const 
 std::__1::chrono::time_point<std::__1::chrono::system_clock, _Duration2>''

How can I combine std::chrono::nanoseconds with time_point

Thanks for all.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
darbe
  • 21
  • 1
  • 8
  • By adding them. e.g. `std::chrono::system_clock::now() + std::chrono::seconds(1);` – felix Nov 19 '18 at 13:56
  • First of all please try to create a [mcve] to show us (and read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/)). With slight modifications your code should build, [as it did in this test](https://ideone.com/Mn2rU3). And please tell us what compiler you're using, what version of it, and what flags you provide when you're compiling. – Some programmer dude Nov 19 '18 at 13:59

1 Answers1

4

std::chrono::time_point does not have a constructor that takes an arbitrary duration type as a template parameter. The constructor that takes system_clock::time_point::duration constructs a time point with its time_since_epoch() property set to the specified argument, i.e., the constructed time point represents the time which is the specified duration after its "epoch."

If this is your intent, you can use duration_cast to cast your nanoseconds value to system_clock::time_point::duration:

using namespace std::chrono;

nanoseconds nano(1234567);

system_clock::time_point time(
    duration_cast<system_clock::duration>(nano));

Or, you can construct a system_clock::time_point with a duration of zero (i.e., a time point representing the epoch) and then add your nanoseconds value to this; the result is a new time point:

system_clock::time_point epoch;
system_clock::time_point time = epoch + nano;

As alluded to by Howard Hinnant, the standard does not guarantee any particular precision for std::chrono::system_clock::duration, i.e., the system clock may not be capable of measuring intervals as small as a nanosecond. On my system (Linux x86_64, gcc 7.3 / glibc 2.20) the duration precision is 1 nanosecond, but on other platforms it may be 1 microsecond or less. On such a platform, the result of duration_cast<system_clock::duration>(nanoseconds(1)) is zero, since duration_cast rounds toward zero.

(Note also that even if the precision is 1 nanosecond, the accuracy probably isn't! This is another topic though.)

TypeIA
  • 16,916
  • 1
  • 38
  • 52