Connecting Tech Pros Worldwide Help | Site Map

parsing string with std::stringstream

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2007, 06:35 AM
Grey Alien
Guest
 
Posts: n/a
Default parsing string with std::stringstream

Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:

'YYYY-MM-DD HH:MM:SS AM'


  #2  
Old July 18th, 2007, 07:05 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: parsing string with std::stringstream

"Grey Alien" <grey@andromeda.comwrote in message
news:HsydnVc4ZYfhLwDbnZ2dnUVZ8vWdnZ2d@bt.com...
Quote:
Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:
>
'YYYY-MM-DD HH:MM:SS AM'
There were 2 replies, one of mine included, to your previous post showing
how to parse this string using std::stringstream. Did you not read the
replies, or did you not like them?


  #3  
Old July 18th, 2007, 08:35 AM
Grey Alien
Guest
 
Posts: n/a
Default Re: parsing string with std::stringstream



Jim Langston wrote:
Quote:
"Grey Alien" <grey@andromeda.comwrote in message
news:HsydnVc4ZYfhLwDbnZ2dnUVZ8vWdnZ2d@bt.com...
>
Quote:
>>Does *ANYONE* in here know how I may parse the various date/time
>>'elements' from a string?. The input string has the ff format:
>>
>>'YYYY-MM-DD HH:MM:SS AM'
>
>
There were 2 replies, one of mine included, to your previous post showing
how to parse this string using std::stringstream. Did you not read the
replies, or did you not like them?
>
>
Apologies - my original post (and any subsequent replies) for some
reason - did not show up on my newsreader. Hence my repost yesterday. I
did not get any reply yesterday to my repost - hence this post today.
Could you kindly post your soln to this thread - I don't understand why
the other posts did not appear. tx
  #4  
Old July 18th, 2007, 11:05 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: parsing string with std::stringstream


"Grey Alien" <grey@andromeda.comwrote in message
news:D4udnTLiIpqmUgDbnZ2dnUVZ8qjinZ2d@bt.com...
Quote:
>
>
Jim Langston wrote:
>
Quote:
>"Grey Alien" <grey@andromeda.comwrote in message
>news:HsydnVc4ZYfhLwDbnZ2dnUVZ8vWdnZ2d@bt.com...
>>
Quote:
>>>Does *ANYONE* in here know how I may parse the various date/time
>>>'elements' from a string?. The input string has the ff format:
>>>
>>>'YYYY-MM-DD HH:MM:SS AM'
>>
>>
>There were 2 replies, one of mine included, to your previous post showing
>how to parse this string using std::stringstream. Did you not read the
>replies, or did you not like them?
>
Apologies - my original post (and any subsequent replies) for some
reason - did not show up on my newsreader. Hence my repost yesterday. I
did not get any reply yesterday to my repost - hence this post today.
Could you kindly post your soln to this thread - I don't understand why
the other posts did not appear. tx
"Erik Wikström" <Erik-wikstrom@telia.comwrote in message
news:D27ni.4282$ZA.2044@newsb.telia.net...
Quote:
On 2007-07-17 18:19, Grey Alien wrote:
Quote:
>I want to parse the various time elements from a string. The input string
>has the ff format:
>>
>'YYYY-MM-DD HH:MM:SS AM'
>>
>Can anyone show me how to do this?
>
This is the only way that I know of that don't require any extra
libraries/extensions:
>
#include <iostream>
#include <sstream>
#include <string>
>
template <class T>
T stoa(const std::string& s)
{
T t;
std::istringstream iss(s);
if (!(iss >t))
throw "Can't convert";
return t;
}
>
int main()
{
std::string input;
std::getline(std::cin, input);
>
int year, mon, day, hour, min, sec;
>
year = stoa<int>(input.substr(0, 4));
mon = stoa<int>(input.substr(5, 2));
day = stoa<int>(input.substr(8, 2));
hour = stoa<int>(input.substr(11, 2));
min = stoa<int>(input.substr(14, 2));
sec = stoa<int>(input.substr(17, 2));
>
if (input.substr(20, 2) == "PM")
hour += 12;
}
>
Don't forget to apply error checking
This works also, although you should throw in error checking.

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::cout << "Enter Date/Time format: YYYY-MM-DD HH:MM:SS AM: ";
std::string DateTime;
std::getline( std::cin, DateTime );

std::stringstream Stream( DateTime );
int Year, Month, Day, Hour, Minute, Second;
std::string AmPm;
char ThrowAway;
Stream >Year >ThrowAway >Month >ThrowAway >Day;
Stream >Hour >ThrowAway >Minute >ThrowAway >Second;
Stream >AmPm;

std::cout << Year << "-" << Month << "-" << Day << " " << Hour << ":" <<
Minute << ":" << Second << " " << AmPm;

}


  #5  
Old July 18th, 2007, 01:15 PM
Grey Alien
Guest
 
Posts: n/a
Default Re: parsing string with std::stringstream

Thanks Jim, I'll study the code and make any suitable
checks/modifications/error handling.
  #6  
Old July 18th, 2007, 08:55 PM
BobR
Guest
 
Posts: n/a
Default Re: parsing string with std::stringstream


Grey Alien <grey@andromeda.comwrote in message...
Quote:
>
Apologies - my original post (and any subsequent replies) for some
reason - did not show up on my newsreader.
It's those dang iPhones screwing up the net!! <G>

Some ISPs are slow to propagate (and getting worse (spam filters, etc.)).

--
Bob R
POVrookie


  #7  
Old July 19th, 2007, 09:25 AM
Martin
Guest
 
Posts: n/a
Default Re: parsing string with std::stringstream

On Jul 18, 8:25 am, Grey Alien <g...@andromeda.comwrote:
Quote:
Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:
>
'YYYY-MM-DD HH:MM:SS AM'
Sure. Try something like this.

std::stringstream ss;
ss << "2007-07-19 11:18:01 AM";
int year, month, day, hour, min, sec;
char c;
ss >year >c >month >c >day >hour >c >min >c >>
sec;
std::cout << " " << year << " " << month << " " << day << " " <<
hour << " " << min << " " << sec;

  #8  
Old July 19th, 2007, 09:25 AM
keith@bytebrothers.co.uk
Guest
 
Posts: n/a
Default Re: parsing string with std::stringstream

On 18 Jul, 07:25, Grey Alien <g...@andromeda.comwrote:
Quote:
Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:
>
'YYYY-MM-DD HH:MM:SS AM'
sscanf(myString.c_str(), "%4d-%2d-%2d %2d:%2d:%2d %s",
&year, &month, &day, &hour, &min, &sec, ampm_str);

 

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,989 network members.