12

I want to be able to get nanosecond accuracy with the chrono library but I can't figure out how to convert std::chrono::high_resolution_clock::now() into long int. I tried this:

#include <chrono>
#include <iostream>
using namespace std;

int main() {
    typedef std::chrono::high_resolution_clock Clock;

    long int val = Clock::now();

    cout << val << endl;

    cin.ignore();
    return 0;
}

But this gave me the error: error C2440: 'initializing' : cannot convert from 'std::chrono::system_clock::time_point' to 'long' How can I convert it to a 64 bit int? If I can't then I don't see how chrono is useful.

2
  • 2
    If you read the documentation you will see that the now function returns a time_point object (just like the error message says). See the time_point link for an example on how to print the time. If you want the time in seconds, use e.g. to_time_t. Aug 2, 2013 at 17:38
  • One of <chrono>s advantages is that it is type safe; two examples are that time points and time durations are distinct, non-interchangeable types and the types keep track of units for you, so you can't confuse seconds for milliseconds, etc.
    – bames53
    Aug 2, 2013 at 18:31

4 Answers 4

19

The following works with GCC 4.8 on Linux:

using namespace std::chrono;
auto now = high_resolution_clock::now();
auto nanos = duration_cast<nanoseconds>(now.time_since_epoch()).count();
std::cout << nanos << '\n';
5
  • 2
    This printed out a huge negative number. Aug 2, 2013 at 17:47
  • @SusanYanders I don't think count can return a negative
    – aaronman
    Aug 2, 2013 at 17:51
  • @aaronman Hmm thats strange my code was almost exactly the same yet it printed out what I expected when I tried your code. Anyways thanks Aug 2, 2013 at 17:55
  • 8
    @SusanYanders: Beware that long int isn't necessarily 64 bits; it's 32 on at least one popular platform. Use auto or std::uint64_t to be sure the type is large enough. Aug 2, 2013 at 18:29
  • 1
    One of <chrono>s advantages is that it is type safe; two examples are that time points and time durations are distinct, non-interchangeable types and the types keep track of units for you, so you can't confuse seconds for milliseconds, etc. Stick with <chrono> types as long as possible and only convert to generic types like long as late as possible. For example: auto nanos = duration_cast<nanoseconds>(now.time_since_epoch()); std::cout << nanos.count() << '\n'; instead of doing the .count() when initializing nanos.
    – bames53
    Aug 2, 2013 at 18:33
5

First, convert the time point returned by now() into the duration since a known timepoint. This can either be the clock's epoch:

auto since_epoch = Clock::now().time_since_epoch();

or some timepoint that you've chosen:

auto since_epoch = Clock::now() - my_epoch;

Then you can get the number of nanoseconds either by converting and extracting the count:

auto nanos = duration_cast<nanoseconds>(since_epoch).count();

or by dividing by whatever granularity you want:

auto nanos = since_epoch / nanoseconds(1);

As noted in the comments, only do this last conversion (which leaves the Chrono library's type system, losing valuable information about what the number means) if you really need a scalar quantity; perhaps because you're interacting with an API that doesn't use the standard types. For your own calcuations, the types should allow you to perform any meaningful arithmetic that you need.

4
  • 2
    auto nanos = since_epoch / nanoseconds(1); It should be pointed out that this results in a unitless scalar. Unless you actually need a unitless value then it's best to stick with chrono's unit-safe types.
    – bames53
    Aug 2, 2013 at 18:37
  • @bames53: Indeed; but in this case, a unitless scalar is exactly what the OP is asking for, and exactly what count() also gives you. Aug 3, 2013 at 3:19
  • Sure but new users ought to be informed about the benefits of strong typing in the context of <chrono> and shown idiomatic <chrono> usage. New users often ask for weaker types simply because that's what they're used to in other time libraries, not because they actually need weak types.
    – bames53
    Aug 3, 2013 at 5:11
  • @bames53: I suppose you're right. A lecture on type safety is rather off-topic for this question, but I've added a brief one in case anyone reads the answer and thinks that leaving the type system sounds like a good idea. Aug 3, 2013 at 13:27
2

A more succinct version of nosid's answer:

long int time = static_cast<long int>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
-2

You can use a std::chrono::duration_cast:

http://en.cppreference.com/w/cpp/chrono/duration/duration_cast

1
  • This is not a full explanation, the part that actually converts it to an int is the count call
    – aaronman
    Aug 2, 2013 at 17:42

Your Answer

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

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