T
tech
Hi, I need to parse a string used to represent a time and then
populate
a simple time struct. The time string will always be this format
23:45.45 ie hours separated from mins by ':' and minutes separated
from seconds by '.'
The string will be 8 chars in len. I've come up with some simple code
below
but am wondering do i really need a wstringstream and a string to do
this. Can
the parsing just be done with a wstringstream.
struct TIMESTRUCT
{
unsigned short Hour;
unsigned short Minute;
unsigned short Second;
}
bool TimeParser(const std::wstring& time, TIMESTRUCT& st)
{
std::wistringstream tmp;
// a time string must be 8 chars in len
assert(time.size() == 8);
tmp.str(time.substr(0, 2));
tmp >> st.Hour;
if (time[2] != ':') return false;
tmp.clear();
tmp.str(time.substr(3, 2));
tmp >> st.Minute;
if (time[5] != '.') return false;
tmp.clear();
tmp.str(time.substr(6, 2));
tmp >> st.Second;
return true;
}
int main()
{
TIMESTRUCT st;
std::wstring t(L"23:34.45");
TimeParser(t, st);
return 0;
}
populate
a simple time struct. The time string will always be this format
23:45.45 ie hours separated from mins by ':' and minutes separated
from seconds by '.'
The string will be 8 chars in len. I've come up with some simple code
below
but am wondering do i really need a wstringstream and a string to do
this. Can
the parsing just be done with a wstringstream.
struct TIMESTRUCT
{
unsigned short Hour;
unsigned short Minute;
unsigned short Second;
}
bool TimeParser(const std::wstring& time, TIMESTRUCT& st)
{
std::wistringstream tmp;
// a time string must be 8 chars in len
assert(time.size() == 8);
tmp.str(time.substr(0, 2));
tmp >> st.Hour;
if (time[2] != ':') return false;
tmp.clear();
tmp.str(time.substr(3, 2));
tmp >> st.Minute;
if (time[5] != '.') return false;
tmp.clear();
tmp.str(time.substr(6, 2));
tmp >> st.Second;
return true;
}
int main()
{
TIMESTRUCT st;
std::wstring t(L"23:34.45");
TimeParser(t, st);
return 0;
}