Time in C++

G

gabitoju

I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
std:string, and I wan't to compare them. I looked at ctime but I didn't
found, first, somethig to convert from string to time and then some
function to compare them.
How can I do this?

Thanks
 
D

Daniel T.

I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
std:string, and I wan't to compare them. I looked at ctime but I didn't
found, first, somethig to convert from string to time and then some
function to compare them.
How can I do this?

There are a couple of ways you can do this. One would be to wrap the
string in a "time" class that understands the format of times. If this
type of data was used extensively in a program, I would probably go this
rout. Another would be to implement a times_equal and times_less that
takes two strings as parameters. I'm going to assume the latter in this
case.

Take the code below and add to it where it says "insert code here" until
it will compile without errors or warnings and when run, will display
"Works so far!" on the screen.

If you have any problems, post what you have so far and I'll help you
through them. Once you get it working, post what you did and I'll help
you with the next step.

----- begin code -----
#include <cassert>
#include <iostream>
#include <string>

using namespace std;

bool times_equal( const string& lhs, const string& rhs ) {
// insert code here
}

bool times_less( const string& lhs, const string& rhs ) {
// insert code here
}

int main() {
string a = "20:30:15";
assert( times_equal( a, a ) );
cout << "Works so far!\n";
};
 
D

David Harmon

On 21 Oct 2006 14:58:41 -0700 in comp.lang.c++, (e-mail address removed)
wrote,
I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
std:string, and I wan't to compare them. I looked at ctime but I didn't
found, first, somethig to convert from string to time and then some
function to compare them.

You may have to do your own parsing from string to three integers,
with the usual istringstream, strtol(), sscanf(), your choice.

The rest you can do with mktime(), difftime() etc. from the old C
library. The usual answer to that is the same in C++ as it is in C,
and is covered in Steve Summit's C FAQ. It is always good to check
the FAQ before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html
 
B

Bo Yang

(e-mail address removed) :
I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
std:string, and I wan't to compare them. I looked at ctime but I didn't
found, first, somethig to convert from string to time and then some
function to compare them.
How can I do this?

Thanks
Oh , I think boost date time library
will help you !

You can take some example in the following url:
http://www.boost.org/boost/boost_1_33_1/doc/html/date_time/examples.html
 
J

Jacek Dziedzic

I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
std:string, and I wan't to compare them. I looked at ctime but I didn't
found, first, somethig to convert from string to time and then some
function to compare them.
How can I do this?

Wait a second, why not just compare strings? If they are
in hh:mm:ss format, than a lexical comparison works just
as you want it to, doesn't it?

string time1="20:30:15";
string time2="21:15:20";

cout << (time1 > time2) << " " << (time1 < time2) << endl;

yields:

false true

HTH,
- J.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Jacek said:
Wait a second, why not just compare strings? If they are
in hh:mm:ss format, than a lexical comparison works just
as you want it to, doesn't it?

string time1="20:30:15";
string time2="21:15:20";

cout << (time1 > time2) << " " << (time1 < time2) << endl;

yields:

false true

Just what I was about to say. Also works for ISO formatted dates
(YYYY-MM-DD).

You do need to make sure that times are zero padded though. 1:23:45
won't work, but 01:23:45 will. You should also assert that the
seperators are the same.


K
 
G

gabitoju

I'm analyzing web server logs, so the formate is hh:mm:ss. (time1 >
time2) works fine. But I having a class to wrap time is something that
I will do because I have to do some calculations like if (time1 - time
2 = 15(min) ). I also check out boost library and It's great, but to
big for what I'm doing.

Thank you all guys.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

I'm analyzing web server logs, so the formate is hh:mm:ss. (time1 >
time2) works fine. But I having a class to wrap time is something that
I will do because I have to do some calculations like if (time1 - time
2 = 15(min) ). I also check out boost library and It's great, but to
big for what I'm doing.

Thank you all guys.

Top posting is generally frowned upon around here. I've moved your
response to the end.

You can only do that calculation if you use the date together with the
time. If you're not too worried about the precision (i.e. you don't
have to be accurate to less than a couple of seconds per year) then the
calculations aren't too difficult to write by hand given a lookup table
of the days per month and a leap year formula. Things get trickier if
you want to deal with old dates or time differences that are very
accurate over long periods.


K
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top