473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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'

Jul 18 '07 #1
7 13602
"Grey Alien" <gr**@andromeda.comwrote in message
news:Hs******************************@bt.com...
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?
Jul 18 '07 #2


Jim Langston wrote:
"Grey Alien" <gr**@andromeda.comwrote in message
news:Hs******************************@bt.com...
>>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
Jul 18 '07 #3

"Grey Alien" <gr**@andromeda.comwrote in message
news:D4******************************@bt.com...
>

Jim Langston wrote:
>"Grey Alien" <gr**@andromeda.comwrote in message
news:Hs******************************@bt.com...
>>>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" <Er***********@telia.comwrote in message
news:D2****************@newsb.telia.net...
On 2007-07-17 18:19, Grey Alien wrote:
>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;

}
Jul 18 '07 #4
Thanks Jim, I'll study the code and make any suitable
checks/modifications/error handling.
Jul 18 '07 #5

Grey Alien <gr**@andromeda.comwrote in message...
>
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
Jul 18 '07 #6
On Jul 18, 8:25 am, Grey Alien <g...@andromeda.comwrote:
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;

Jul 19 '07 #7
On 18 Jul, 07:25, Grey Alien <g...@andromeda.comwrote:
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);

Jul 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ellarco | last post by:
Im sorry for asking a question that is surely in the archives somewhere, but I have been unable to locate it. Its about string memory management. I need to dynamically construct a C-style string...
6
by: Giampiero Gabbiani | last post by:
Is it possible to reset a std::stringstream in order to reuse it once more? I tried with flush() method without any success... Thanks in advance Giampiero
5
by: Christopher Benson-Manica | last post by:
If std::stringstreams are unavailable, how would you go about adding an integer (or unsigned integer) to a string? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I...
4
by: Dylan | last post by:
Hi again, In the following program I expected step 3 to assign the values 1 and 2 to iVal1 and iVal2 yet it appears ss.seekg(0, std::ios::beg) does not move the read position back to the...
5
by: Mr Fish | last post by:
Is it possible for me to record floats in a std::stringstream with 100% accuracy? If so how? Here's a little prog that demonstrates how the default ss loses accuracy ...
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
1
by: magix | last post by:
I got this reply in my previous post a month ago: May I know, how can I automatically create the folder if it doesn't exist ? In previous reply, it said: -------------------------...
0
by: ziyanjoe | last post by:
Hi! I am writing a program for the robot that runs as a daemon on a linux machine. Since debug output cannot be seen on stdout, I want to create an iostream to handle all the output messages....
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.