As others have noted, you can call the count()
member function to get the internal count.
I wanted to add that I am attempting to add a new header: <chrono_io>
to this library. It is documented here. The main advantage of <chrono_io>
over just using count()
is that the compile-time units are printed out for you. This information is of course obtainable manually, but it is much easier to just have the library to it for you.
For me, your example:
#include <iostream>
#include <chrono_io>
int main()
{
auto t = std::chrono::high_resolution_clock::now();
std::cout << t.time_since_epoch() << '\n';
}
Outputs:
147901305796958 nanoseconds
The source code to do this is open source and available at the link above. It consists of two headers: <ratio_io>
and <chrono_io>
, and 1 source: chrono_io.cpp
.
This code should be considered experimental. It is not standard, and almost certainly will not be standardized as is. Indeed preliminary comments from the LWG indicate that they would prefer the default output to be what this software calls the "short form". This alternative output can be obtained with:
std::cout << std::chrono::duration_fmt(std::chrono::symbol)
<< t.time_since_epoch() << '\n';
And outputs:
147901305796958 ns
Update
It only took a decade, but C++20 now does:
#include <chrono>
#include <iostream>
int main()
{
auto t = std::chrono::high_resolution_clock::now();
std::cout << t.time_since_epoch() << '\n';
}
Output:
147901305796958ns
(In this post I will omit std::chrono::
qualifications for clarity. I trust you know where they go.)
The reason your code example fails to compile is that there is a mismatch between the return type of system_clock::now()
and the type of variable you are trying to assign this to (time_point<system_clock, nanoseconds>
).
The documented return value of system_clock::now()
is system_clock::time_point
, which is a typedef for time_point<system_clock, system_clock::duration>
. system_clock::duration
is implementation-defined, with microseconds
and nanoseconds
being commonly used. It seems that your implementation uses microseconds
, so the return type of system_clock::now()
is time_point<system_clock, microseconds>
.
time_point
s with different durations are not implicitly convertible to one another, so you get a compiler error.
You can explicitly convert time points with different durations using time_point_cast
, so the following would compile on your system:
time_point<system_clock, nanoseconds> time_point;
time_point = time_point_cast<nanoseconds>(system_clock::now());
Notice the explicit template parameter to time_point_cast
is the target duration type, not the target time_point type. The clock types must match in a time_point_cast
, so specifying the entire time_point type (which is templated on both the clock type and the duration type) would be redundant.
Of course in your case, since you are just looking to print the time point, there is no need for it to be at any specific resolution, so you can just declare time_point
to be the same type as what system_clock::now()
returns to begin with. A simple way to do that is to use the system_clock::time_point
typedef:
system_clock::time_point time_point;
time_point = system_clock::now(); // no time_point_cast needed
Since this is C++11, you can also just use auto
:
auto time_point = system_clock::now();
Having solved this compiler error, the conversion to time_t
works just fine:
std::time_t now_c = std::chrono::system_clock::to_time_t(time_point);
and you can now use standard methods for displaying time_t
values, like std::ctime
or std::strftime
. (As Cassio Neri points out in a comment to your question, the more C++-y std::put_time
function is not yet supported by GCC).
Best Answer
The
<chrono>
library only deals with time and not dates, except for thesystem_clock
which has the ability to convert its timepoints totime_t
. So using<chrono>
for dates will not improve things much. Hopefully we get something likechrono::date
in the not too distant future.That said, you can use
<chrono>
in the following way:Note that
std::localtime
may cause data races.localtime_r
or similar functions may be available on your platforms.Update:
Using a new version of Howard Hinnant's date library you can write:
This will print out something like "2015-07-24 05:15:34.043473124 UTC".
On an unrelated note, returning
const
objects has become undesirable with C++11; const return values cannot be moved from. I also removed the trailing const because trailing const is only valid for member functions and this function has no need to be a member.