comparing date time

J

Javier

Hi

I´ve a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20


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

I´m 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: I´m working with VC++ 6
 
O

osmium

Javier said:
I´ve 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?
 
R

Ron Natalie

osmium said:
:




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.
 
I

Ioannis Vranos

osmium said:
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.
 
I

Ioannis Vranos

Ioannis said:
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.
 
K

Karl Heinz Buchegger

osmium said:
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.
 
C

Chris Croughton

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
 
J

Javier

osmium said:
:




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
 
C

Chris Croughton

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
 
K

Karl Heinz Buchegger

Javier said:
Hi

I´ve a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20

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

I´m 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 :)
 
M

Matthias Kaeppler

Javier said:
Hi

I´ve a routine that will read date and times in a vector of strings ie:

30/02/2005 19:20


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

I´m 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: I´m 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.
 
E

Eric Sokolowsky

Chris said:
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.
 
C

Chris Croughton

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
 
I

Ioannis Vranos

Chris said:
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").
 
A

Aslan Kral

haber iletisinde sunlari said:
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.
 
I

Ioannis Vranos

Aslan said:
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. :)
 
I

Ioannis Vranos

Ioannis said:
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")
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top