stringstream question

G

Grey Alien

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?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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
 
J

Jim Langston

Erik Wikström said:
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;

}
 
F

faceman28208

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?

Do you actually need to "parse" this or can you just extract from
fixed positions? If it's the latter, all you need to do is substring
operations. For the former, it's a bit harder but you would need to
specify in more detail what is permitted.
 
J

James Kanze

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;
}

That's more or less what I'd do, too, except that instead of
ThrowAway, I'd use a manipulator:

class CheckSeparator
{
public:
explicit CheckSeparator( char separ )
: mySepar( static_cast< unsigned char >( separ ) )
{
}

friend std::istream& operator<<(
std::istream& source,
CheckSeparator const& obj )
{
if ( source.get() != ch ) {
source.setstate( std::ios::failbit ) ;
}
}

private:
int mySepar ;
} ;

Then:

stream >> year
// etc.

Alternatively, I'll check the entire line with boost::regex,
then use transform to convert the punctuation into spaces, and
read directly. Or have boost::regex extract the individual
fields, and convert each of them separately. The last solution
is, of course, the most generic.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top