3

Here's what I'd need to do:

double now=getdoubletimestampsomehow();

Where getdoubletimestampsomehow() should be a straight-forward, easy to use function returning a double value representing the number of seconds elapsed from a given date. I'd need it to be quite precise, but I don't really need it to be more precise than a few milliseconds. Portability is quite important, if it isn't possible to directly port it anywhere could you please tell me both an unix and a windows way to do it?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
  • What have you looked at, and why are those possiblities inadequate? Have you looked at date/time facilities in [Boost](http://www.boost.org/doc/libs/1_49_0/doc/html/date_time.html) and [Qt](http://developer.qt.nokia.com/doc/qt-4.8/qdatetime.html)? Do you really need the timestamp to be a `double`? – André Caron Feb 26 '12 at 22:55
  • 2
    possible duplicate of [C++ Cross-Platform High-Resolution Timer](http://stackoverflow.com/questions/1487695/c-cross-platform-high-resolution-timer) – Greg Hewgill Feb 26 '12 at 22:55

4 Answers4

4

Have you looked at Boost and particularly its Date_Time library ? Here is the seconds since epoch example.

You will be hard-pressed to find something more portable, and of higher resolution.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
2

C++11 introduced the <chrono> header containing quite a few portable clocks. The highest resolution clock among them is the std::chrono::high_resolution_clock.

It provides the current time as a std::chrono::time_point object which has a time_since_epoch member. This might contain what you want.

Reference:

Prior to the release of the C++11 standard, there was no standard way in which one could accurately measure the execution time of a piece of code. The programmer was forced to use external libraries like Boost, or routines provided by each operating system.

The C++11 chrono header file provides three standard clocks that could be used for timing one’s code:

  • system_clock - this is the real-time clock used by the system;
  • high_resolution_clock - this is a clock with the shortest tick period possible on the current system;
  • steady_clock - this is a monotonic clock that is guaranteed to never be adjusted.

If you want to measure the time taken by a certain piece of code for execution, you should generally use the steady_clock, which is a monotonic clock that is never adjusted by the system. The other two clocks provided by the chrono header can be occasionally adjusted, so the difference between two consecutive time moments, t0 < t1, is not always positive.

Community
  • 1
  • 1
Chris
  • 6,914
  • 5
  • 54
  • 80
2

Portable good precision double timestamp in C++?

There is no portable way to get high-precision timestamp (milliseconds) without using 3rd party libraries. Maximum precision you'll get is 1 second, using time/localtime/gmtime.

If you're fine with 3rd party libraries, use either Boost or Qt 4.

both an unix and a windows way to do it?

GetSystemTime on Windows and gettimeofday on linux.

Please note that if you're planning to use timestamps to determine order of some events, then it might be a bad idea. System clock might have very limited precision (10 milliseconds on windows platform), in which case several operations performed consequently can produce same timestamp. So, to determine order of events you would need "logical timestamps" ("vector clock" is one of examples).

On windows platform, there are highly precise functions that can be used to determine how much time has passed since some point in the past (QueryPerformanceCounter), but they aren't connected to timestamps.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
-1

Doubles are not precise - therefore you idea for double now=getdoubletimestampsomehow(); falls down at the first hurdle.

Others have mentioned other possibilities. I would explore those.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127