Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Time in C++

Reply
Thread Tools

Time in C++

 
 
gabitoju@gmail.com
Guest
Posts: n/a
 
      10-21-2006
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

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      10-21-2006
http://www.velocityreviews.com/forums/(E-Mail 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.
> 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.
 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      10-21-2006
On 21 Oct 2006 14:58:41 -0700 in comp.lang.c++, (E-Mail 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

 
Reply With Quote
 
Bo Yang
Guest
Posts: n/a
 
      10-22-2006
(E-Mail 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_3.../examples.html
 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      10-22-2006
(E-Mail 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.
> 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.
 
Reply With Quote
 
=?iso-8859-1?q?Kirit_S=E6lensminde?=
Guest
Posts: n/a
 
      10-23-2006

Jacek Dziedzic wrote:
> (E-Mail 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.
> > 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

 
Reply With Quote
 
gabitoju@gmail.com
Guest
Posts: n/a
 
      10-23-2006
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:
> Jacek Dziedzic wrote:
> > (E-Mail 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.
> > > 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


 
Reply With Quote
 
=?iso-8859-1?q?Kirit_S=E6lensminde?=
Guest
Posts: n/a
 
      10-24-2006


> Kirit Sælensminde wrote:
> > Jacek Dziedzic wrote:
> > > (E-Mail 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.
> > > > 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


(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Re: interpreting the fractional portion of time.clock() vs time.time(0measurements Peter Hansen Python 0 02-22-2006 02:02 PM
Re: interpreting the fractional portion of time.clock() vs time.time()measurements Peter Hansen Python 0 02-22-2006 12:03 AM
time.clock() or time.time() peterbe@gmail.com Python 8 08-05-2005 01:51 PM
delta time = time stop - time start engsol Python 2 01-26-2004 12:06 PM



Advertisments