0

Is there a way to get the ptime representation of the current UTC time using boost Posix Time Library with nanoseconds precision? I know how to get microseconds precision using microsec_clock. I know as well we can get this using chrono:

#include <chrono>
#include <ctime>

const std::chrono::time_point<std::chrono::system_clock> pt = std::chrono::system_clock::now();
const auto nanos = static_cast<std::time_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(pt.time_since_epoch()).count());

Looking for something like nanosec_clock maybe?

Note: I am using boost library so I would like to keep using it for this as well, if no solution I could use the chrono above explained here as well.

3
  • This question is similar to: C++ current system time in nanoseconds as long integer in order to send influxdb. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Aug 3 at 11:52
  • As explained in my question I would like to stay consistent and use same library instead of chrono. That is the difference with that question. I added a Note in the question. Thank you
    – SpeakX
    Commented Aug 3 at 11:56
  • 1
    Note that just because an API returns nanoseconds does not mean that is has nanoseconds precision, you will need to check this. Commented Aug 3 at 15:12

1 Answer 1

2
PTime - Introduction

Defines a non-adjusted time system with nano-second/micro-second resolution and stable calculation properties. The nano-second resolution option uses 96 bits of underlying storage for each ptime while the micro-second resolution uses 64 bits per ptime (see Build Options for details).

From Build Options:

Compilation Options

By default the posix_time system uses a single 64 bit integer internally to provide a microsecond level resolution. As an alternative, a combination of a 64 bit integer and a 32 bit integer (96 bit resolution) can be used to provide nano-second level resolutions. The default implementation may provide better performance and more compact memory usage for many applications that do not require nano-second resolutions.

To use the alternate resolution (96 bit nanosecond) the variable BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG must be defined in the library users project files (ie Makefile, Jamfile, etc).

2
  • Hi @Ted Lyngmo thanks so much for this. I already defined that flag using #define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG in one of my hpp. Do you think this will enable nanosec_clock and does it even exist in boost libarary? Because what I need is more precise than just using the nanoseconds. I want a way to get the current UTC time in nanoseconds
    – SpeakX
    Commented Aug 3 at 12:17
  • 2
    @SpeakX You're welcome! I would define it outside the source code (as an argument to the compiler, g++ -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG ...) to ensure that no boost headers are read before it is defined.
    – Ted Lyngmo
    Commented Aug 3 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.