how to efficiently find the difference of two times in C++?

M

Michael

Dear all,

I have the time data of the following formats:

How do I take their difference accurately in C++?

2009-03-09 04:07:39.017404079
2009-03-09 04:07:55.587754011

Thanks!
 
N

nelstaar

Dear all,

I have the time data of the following formats:

How do I take their difference  accurately in C++?

2009-03-09 04:07:39.017404079
2009-03-09 04:07:55.587754011

Thanks!

Hi!

What do you expect as result, ms, seconds, days ... or "grater than" /
"smaller than" ?

and can you use libs such boost:time ?
 
J

James Kanze

I have the time data of the following formats:
How do I take their difference accurately in C++?
2009-03-09 04:07:39.017404079
2009-03-09 04:07:55.587754011

It depends on the implementation. Standard C++ doesn't support
time resolution of less than a second (and even then, you'd have
to parse the times to create a tm, then use mktime on it, then
use difftime to determine the difference). Most actual
implementations do provide more, although you might still have
to do some parsing yourself.

If I were you, I'd start by looking for some sort of portable
library. (Boost has something, which I think was the basis of
what will be added in the next release of the standard.)
 
M

Michael

It depends on the implementation.  Standard C++ doesn't support
time resolution of less than a second (and even then, you'd have
to parse the times to create a tm, then use mktime on it, then
use difftime to determine the difference).  Most actual
implementations do provide more, although you might still have
to do some parsing yourself.

If I were you, I'd start by looking for some sort of portable
library.  (Boost has something, which I think was the basis of
what will be added in the next release of the standard.)

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34



Hi all,

I just need the result, the difference between two times to be a real
number. That's all.

Let's say using millisecond or microsecond as one unit.

The result should be in terms of "millisecond" or "microsecond", so
the precision will not be lost.

Need it to be fast. What's the most efficient way of doing this?
 
J

James Kanze

I just need the result, the difference between two times to be
a real number. That's all.
Let's say using millisecond or microsecond as one unit.
The result should be in terms of "millisecond" or
"microsecond", so the precision will not be lost.
Need it to be fast. What's the most efficient way of doing
this?

Did you read the response you are responding to? It answered
your question. C++ does not have time with resolution superior
to a second. (Actually, C++ doesn't say anything about the
representation or the resolution of time_t. So potentially,
some implementations could use a resolution of a millisecond or
more. To my knowledge, none do.)
 
M

Michael

Hi,

Well. I am talking about not treating them as times, but just plain
real numbers. What's the most efficient way to translate the string
into real numbers, and then take their difference?

Thanks!
 
J

James Kanze

Well. I am talking about not treating them as times, but just
plain real numbers. What's the most efficient way to translate
the string into real numbers, and then take their difference?

It depends on the implementation, but I suspect that in most
implementations, you'll have to parse the strings manually.
(What you might find is something that will parse up to the
decimal point in the time, giving a time_t in seconds from some
specified point. Something like strptime under Unix. But you'd
still have to handle the fractional seconds manually.)
 
B

Bart van Ingen Schenau

Michael said:
Hi,

Well. I am talking about not treating them as times, but just plain
real numbers. What's the most efficient way to translate the string
into real numbers, and then take their difference?

How about this to convert the string representation to numeric values:
int year, mon, day, hour, min;
double sec;
sscanf(dateString, "%d-%d-%d %d:%d:%f", &year, &mon, &day, &hour,
&min, &sec);

Converting this to a single number is left as an exercise.

Bart v Ingen Schenau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top