By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

comparing date time

P: n/a

Hi

Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20
In some moment Ill need to check if there are in vector a date like
current date with a time >= current time and <= (current time + n minutes)

Im new to C++ but, nevertheless, I know how to search in a vector, but
how could I make date time comparisons ?

Thanks in advance

J
PD: Im working with VC++ 6
Jul 23 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
"Javier" writes:
Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20


Not to change the subject but where on this planet is such a time
representation used and valid?
Jul 23 '05 #2

P: n/a
osmium wrote:
"Javier" writes:

Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20

Not to change the subject but where on this planet is such a time
representation used and valid?

I've seen that ordering, but not with that punctuation in the Military
and frequently in Europe.
Jul 23 '05 #3

P: n/a
"Ron Natalie" writes:
osmium wrote:
"Javier" writes:

Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20

Not to change the subject but where on this planet is such a time
representation used and valid?

I've seen that ordering, but not with that punctuation in the Military
and frequently in Europe.


It sure looks like February 30th to me.
Jul 23 '05 #4

P: n/a
osmium wrote:
30/02/2005 19:20

Not to change the subject but where on this planet is such a time
representation used and valid?

What exactly do you think is invalid? In Europe dates are in the form
dd/mm/yyyy or dd/mm/yy.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5

P: n/a
Ioannis Vranos wrote:
30/02/2005 19:20


Not to change the subject but where on this planet is such a time
representation used and valid?


What exactly do you think is invalid? In Europe dates are in the form
dd/mm/yyyy or dd/mm/yy.


.... and time is used in both 12h/24h formats.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #6

P: n/a
osmium wrote:

"Ron Natalie" writes:
osmium wrote:
"Javier" writes:
Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20
Not to change the subject but where on this planet is such a time
representation used and valid?

I've seen that ordering, but not with that punctuation in the Military
and frequently in Europe.


It sure looks like February 30th to me.


C'mon. The OP was simply typing an example without thinking and made a
mistake in the values. That doesn't change his problem.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7

P: n/a
Ioannis Vranos wrote:
What exactly do you think is invalid? In Europe dates are in the form
dd/mm/yyyy or dd/mm/yy.

... and time is used in both 12h/24h formats.

I just realised the OP's 30th of February...


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #8

P: n/a
osmium wrote:
"Javier" writes:

Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20

Not to change the subject but where on this planet is such a time
representation used and valid?

How much of this planet do you know ?

In Europe and all South America, people use to represent dates in that
way: dd/mm/yyyy. May be you need to make a trip through the 99,99% of
the world you unknown... :)

In this case, this data comes from a Visual Fox application and is
stored in that way in a MSDE.

J

Jul 23 '05 #9

P: n/a
On Mon, 21 Feb 2005 09:06:35 -0500, Ron Natalie
<ro*@sensor.com> wrote:
osmium wrote:
"Javier" writes:
Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20


Not to change the subject but where on this planet is such a time
representation used and valid?

I've seen that ordering, but not with that punctuation in the Military
and frequently in Europe.


Military time is normally ISO 8601 now (yyyy.mm.ddThh:mm:ss, with optional
separators). Normal UK date notation is dd/mm/yyyy (continental Europe
tends to use dd.mm.yyyy, using - as separator is also common; prior to
2000 the year was often 2 digits instead of 4 (and sometimes still is
that way). Only the UK and dependencies now generally use the 12-hour
clock, a lot of European languages don't even have a concept of AM and
PM, and most public bodies in the UK now use 24-hour (buses, trains,
etc.).

(Of course, none of them actually use the 30th of February in any year,
but it was the representation which was being queried not the value...)

Chris C
Jul 23 '05 #10

P: n/a
"Javier" writes:
osmium wrote:
"Javier" writes:

Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20

Not to change the subject but where on this planet is such a time
representation used and valid?

How much of this planet do you know ?


Does the word "valid" have any meaning at all to you?
Jul 23 '05 #11

P: n/a
On Mon, 21 Feb 2005 13:29:19 -0800, osmium
<r1********@comcast.net> wrote:
"Javier" writes:
osmium wrote:
"Javier" writes:

Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20

Not to change the subject but where on this planet is such a time
representation used and valid?


How much of this planet do you know ?


Does the word "valid" have any meaning at all to you?


You said "time representation", that representation is valid. The date
itself is not valid, but that's not what you asked...

Chris C
Jul 23 '05 #12

P: n/a
Javier wrote:

Hi

Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20

In some moment Ill need to check if there are in vector a date like
current date with a time >= current time and <= (current time + n minutes)

Im new to C++ but, nevertheless, I know how to search in a vector, but
how could I make date time comparisons ?


To get back to the question asked:
In order to compare such dates, you will need to split the complete string
into pieces and compare those pieces. This gets simple, if the format of
the time string is constant and always the same.

Search out for string manipulation functions, especially on how to get
a substring from a string. The other thing that will be handy, is a function
which converts a string representation of a number into a number.

Once you have all those pieces (day, month, year, hour, minute) as numbers,
comparing them should not be a problem. When you do your tests, make sure you
also test the cases, where the time intervall crosses midnight :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #13

P: n/a
Javier wrote:

Hi

Ive a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20
In some moment Ill need to check if there are in vector a date like
current date with a time >= current time and <= (current time + n minutes)

Im new to C++ but, nevertheless, I know how to search in a vector, but
how could I make date time comparisons ?

Thanks in advance

J
PD: Im working with VC++ 6


Now, I bet you didn't expect to kick off a flame war about time
formatting with this topic did you? :D

As to your problem:
I think it would be a better idea to have your date/time being
represented by some class than by plain strings, so you can compare
using operator==. I haven't worked with it, but I suggest you have a
look at boost::date_time
http://www.boost.org/libs/date_time/doc/index.html

Maybe they have what you need.

--
Matthias Kaeppler
Jul 23 '05 #14

P: n/a
Chris Croughton wrote:
Military time is normally ISO 8601 now (yyyy.mm.ddThh:mm:ss, with optional
separators). Normal UK date notation is dd/mm/yyyy (continental Europe
tends to use dd.mm.yyyy, using - as separator is also common; prior to
2000 the year was often 2 digits instead of 4 (and sometimes still is
that way). Only the UK and dependencies now generally use the 12-hour
clock, a lot of European languages don't even have a concept of AM and
PM, and most public bodies in the UK now use 24-hour (buses, trains,
etc.).


Another benefit of using ISO8601 time is that comparison is reduced to
plain old string comparison. The documentation of ISO8601 time that I
have seen uses "-" instead of ".": yyyy-mm-ddThh:mm:ss.
Jul 23 '05 #15

P: n/a
On Tue, 22 Feb 2005 08:51:48 -0500, Eric Sokolowsky
<es***@hotmail.com> wrote:
Chris Croughton wrote:
Military time is normally ISO 8601 now (yyyy.mm.ddThh:mm:ss, with optional
separators). Normal UK date notation is dd/mm/yyyy (continental Europe
tends to use dd.mm.yyyy, using - as separator is also common; prior to
2000 the year was often 2 digits instead of 4 (and sometimes still is
that way). Only the UK and dependencies now generally use the 12-hour
clock, a lot of European languages don't even have a concept of AM and
PM, and most public bodies in the UK now use 24-hour (buses, trains,
etc.).


Another benefit of using ISO8601 time is that comparison is reduced to
plain old string comparison. The documentation of ISO8601 time that I
have seen uses "-" instead of ".": yyyy-mm-ddThh:mm:ss.


You're right, I was thinking of the continental version when I typed
that bit (actually, I typed it without separators and then added them,
and added the wrong ones). Having it comparable with a string
comparison makes things very easy.

Chris C
Jul 23 '05 #16

P: n/a
Chris Croughton wrote:
You're right, I was thinking of the continental version when I typed
that bit (actually, I typed it without separators and then added them,
and added the wrong ones). Having it comparable with a string
comparison makes things very easy.

My first thought is that the default std::string comparison operators
will do the job (lexicographical comparison), so if we have
string("27/02/2005 19:20") < string("28/02/2005 19:20")
will always be true as also
string("27/02/2005 19:19") < string("27/02/2005 19:20").
So the only thing needed, is the input to be checked whether it is in
the correct string format for the program (here "dd/mm/yyyy' 'hh:mm").


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #17

P: n/a

"Ioannis Vranos" <iv*@remove.this.grad.com>, haber iletisinde sunlari
yazdi:1109086397.376682@athnrd02...
string("27/02/2005 19:20") < string("28/02/2005 19:20")
will always be true as also
string("27/02/2005 19:19") < string("27/02/2005 19:20").

Not always.
string("31/12/2004 23:59") > string("01/01/2005 00:00") (lexicographically)

If the year is the same in both, what you say is true.
Jul 23 '05 #18

P: n/a
Aslan Kral wrote:
Not always.
string("31/12/2004 23:59") > string("01/01/2005 00:00") (lexicographically)

If the year is the same in both, what you say is true.

string("2004/31/12 23:59") < string("2005/01/01 00:00")
makes the implementation simpler then. :-)


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #19

P: n/a
Ioannis Vranos wrote:
string("2004/31/12 23:59") < string("2005/01/01 00:00")
makes the implementation simpler then. :-)

I meant yyyy/mm/dd hh:mm :

string("2004/12/31 23:59") < string("2005/01/01 00:00")


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #20

P: n/a
On Tue, 22 Feb 2005 17:56:13 +0200, Aslan Kral
<as**********@yahoo.com> wrote:
"Ioannis Vranos" <iv*@remove.this.grad.com>, haber iletisinde sunlari
yazdi:1109086397.376682@athnrd02...
string("27/02/2005 19:20") < string("28/02/2005 19:20")
will always be true as also
string("27/02/2005 19:19") < string("27/02/2005 19:20").

Not always.
string("31/12/2004 23:59") > string("01/01/2005 00:00") (lexicographically)

If the year is the same in both, what you say is true.


No, only if both the year and the month are the same.

string("31/01/2004 23:59") > string("01/02/2004 00:00") (lexicographically)

That's why ISO 8601 is better, because everything proceeds left to right
as the significance is less, just as in numeric formats (excluding
things like Roman numerals!):

string("2004-01-31 23:59") < string("2004-02-01 00:00") (lexicographically)

which is correct for all values of date.

Chris C
Jul 23 '05 #21

P: n/a

"Javier" <jl****@manresa.net> wrote in message
news:cv**********@news.ya.com...

Hi

I4ve a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20
In some moment I4ll need to check if there are in vector a date like
current date with a time >= current time and <= (current time + n minutes)

I4m new to C++ but, nevertheless, I know how to search in a vector, but
how could I make date time comparisons ?

Thanks in advance

J
PD: I4m working with VC++ 6


If you're working with VC++, and if you don't care about the software being
portable to other systems, then you can use the VC++ libraries that provide
date-time conversion routines. Given a specific representation, they will
convert that string to a data structure with the day,month, year,hour, etc.
all filled out. (I think it's call CDateTime, but I'm not sure.) You can
then do your comparison using the values from those structures. That's
easier that parsing the strings yourself. But as I said, it's specific to
VC++ (and Windows?), so you'd need to read their doc's or check on a vc++
newsgroup for more specifics.

-Howard


Jul 23 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.