Connecting Tech Pros Worldwide Help | Site Map

Converting date string to a number

  #1  
Old July 18th, 2007, 10:05 AM
Jef Driesen
Guest
 
Posts: n/a
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, 11:55 AM
Jim Langston
Guest
 
Posts: n/a

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, 12:25 PM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a

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, 11:25 AM
Jef Driesen
Guest
 
Posts: n/a

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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a string to the most probable type Pierre Quentel answers 13 March 16th, 2008 07:05 PM
converting Date() to unix timestamp format? marc@gaspdesign.co.uk answers 6 March 6th, 2007 11:55 PM
Converting Date Time Format Alok yadav answers 9 October 10th, 2006 08:25 AM
Converting date to milliseconds since 1-1-70 NateM answers 3 January 26th, 2006 12:25 PM