473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Oct 21 '06 #1
7 1656
ga******@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?
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.
Oct 21 '06 #2
On 21 Oct 2006 14:58:41 -0700 in comp.lang.c++, ga******@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.
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

Oct 21 '06 #3
ga******@gmail.com :
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
Oct 22 '06 #4
ga******@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

HTH,
- J.
Oct 22 '06 #5

Jacek Dziedzic wrote:
ga******@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

Oct 23 '06 #6
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:
ga******@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
Oct 23 '06 #7

Kirit Sælensminde wrote:
Jacek Dziedzic wrote:
ga******@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
ga******@gmail.com 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

Oct 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
6
by: Rebecca Smith | last post by:
Today’s question involves two time text boxes each set to a different time zone. Initially txtCurrentTime will be set to Pacific Time or system time. This will change with system time as we travel...
3
by: luscus | last post by:
Thanks for all the responses on my first question. Unfortunately the answers I was given were too complicated for my small brain , and neophite condition to understand. So if you could talk down to...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
1
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.