Time in C++ 
October 21st, 2006, 09:45 PM
| | | Time in C++
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 | 
October 21st, 2006, 10:25 PM
| | | Re: Time in C++ gabitoju@gmail.com wrote: Quote:
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";
};
--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them. | 
October 21st, 2006, 11:25 PM
| | | Re: Time in C++
On 21 Oct 2006 14:58:41 -0700 in comp.lang.c++, gabitoju@gmail.com
wrote, Quote:
>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 | 
October 22nd, 2006, 12:45 PM
| | | Re: Time in C++ gabitoju@gmail.com : Quote:
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_3.../examples.html | 
October 22nd, 2006, 02:05 PM
| | | Re: Time in C++ gabitoju@gmail.com wrote: Quote:
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. | 
October 23rd, 2006, 05:55 AM
| | | Re: Time in C++
Jacek Dziedzic wrote: Quote: gabitoju@gmail.com wrote: Quote:
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
>
| 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 | 
October 23rd, 2006, 02:45 PM
| | | Re: Time in C++
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.
Kirit Sælensminde wrote: Quote:
Jacek Dziedzic wrote: Quote: gabitoju@gmail.com wrote: Quote:
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
| >
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
| | 
October 24th, 2006, 02:35 AM
| | | Re: Time in C++ Quote:
Kirit Sælensminde wrote: Quote:
Jacek Dziedzic wrote: Quote: gabitoju@gmail.com 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.
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
>
| 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
| | gabitoju@gmail.com wrote: Quote:
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 | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|