Connecting Tech Pros Worldwide Help | Site Map

Converting date string to a number

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2007, 09:05 AM
Jef Driesen
Guest
 
Posts: n/a
Default Converting date string to a number

How can I convert a date string to a number (e.g. a time_t value or a tm
struct)? I know about the strptime function, but then I have to know the
format string. And that is a problem.

I'm trying to autoformat the contents of text entries in a GUI. For
numbers, I'm converting the text representation to the appropriate type
(using atoi, atof, ...) and converting the result back to text with the
correct format (using sprintf). But this does not work for date (or
time) strings.

For instance, if the desired date format is "dd-mm-yy", but the user
enters "dd-mm-yyyy", I can't convert the string to a valid date, because
I don't know its format.

  #2  
Old July 18th, 2007, 10:55 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: Converting date string to a number

"Jef Driesen" <jefdriesen@hotmail.com.invalidwrote in message
news:f7kkqb$487$1@ikaria.belnet.be...
Quote:
How can I convert a date string to a number (e.g. a time_t value or a tm
struct)? I know about the strptime function, but then I have to know the
format string. And that is a problem.
>
I'm trying to autoformat the contents of text entries in a GUI. For
numbers, I'm converting the text representation to the appropriate type
(using atoi, atof, ...) and converting the result back to text with the
correct format (using sprintf). But this does not work for date (or time)
strings.
>
For instance, if the desired date format is "dd-mm-yy", but the user
enters "dd-mm-yyyy", I can't convert the string to a valid date, because I
don't know its format.
atoi, atof, etc.. are a pain in the neck. If you use stringstream it's
rather easy.

std::cout << "Enter Date:";
std::cin >Date;
std::stringstream Stream(Date);
int Month, Day, Year;
char Hyphen;

if ( Stream >Month >Hyphen >Day >Hyphen >Year )
// Got good date. Values in Month, Day and year. Should validate them
somehow
else
// Something went wrong, not in right format.

The good thing about this is, you don't have to care how long the month, day
or year entries are. They could enter a 1 or 100 digit number, and it would
read it. Of course, after this is done you want to do reality checking.
I.E. Is month 1-12? Is day 1-31? If year is 2 digit, do you want to add
1900 or 2000? If year is 4 digit, is it realistic? Etc...


  #3  
Old July 18th, 2007, 11:25 AM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a
Default Re: Converting date string to a number

On 2007-07-18 11:01, Jef Driesen wrote:
Quote:
How can I convert a date string to a number (e.g. a time_t value or a tm
struct)? I know about the strptime function, but then I have to know the
format string. And that is a problem.
>
I'm trying to autoformat the contents of text entries in a GUI. For
numbers, I'm converting the text representation to the appropriate type
(using atoi, atof, ...) and converting the result back to text with the
correct format (using sprintf). But this does not work for date (or
time) strings.
>
For instance, if the desired date format is "dd-mm-yy", but the user
enters "dd-mm-yyyy", I can't convert the string to a valid date, because
I don't know its format.
Since you are using a GUI framework check if there is some kind of
date/time class/thingie which can parse time and dates. Standard C++ is
quite poorly equipped on this area but most frameworks have better support.

--
Erik Wikström
  #4  
Old July 19th, 2007, 10:25 AM
Jef Driesen
Guest
 
Posts: n/a
Default Re: Converting date string to a number

Erik Wikström wrote:
Quote:
On 2007-07-18 11:01, Jef Driesen wrote:
Quote:
>How can I convert a date string to a number (e.g. a time_t value or a tm
>struct)? I know about the strptime function, but then I have to know the
>format string. And that is a problem.
>>
>I'm trying to autoformat the contents of text entries in a GUI. For
>numbers, I'm converting the text representation to the appropriate type
>(using atoi, atof, ...) and converting the result back to text with the
>correct format (using sprintf). But this does not work for date (or
>time) strings.
>>
>For instance, if the desired date format is "dd-mm-yy", but the user
>enters "dd-mm-yyyy", I can't convert the string to a valid date, because
>I don't know its format.
>
Since you are using a GUI framework check if there is some kind of
date/time class/thingie which can parse time and dates. Standard C++ is
quite poorly equipped on this area but most frameworks have better support.
I'm using gtkmm (the C++ bindings for gtk+), but it doesn't seem to have
more advanced date/time functions.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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.