How can I extract the time from a string using stringstream in C++ -


how extract hours, minutes, seconds, , am/pm string input similar using stringstream?

input:

07:05:45pm 

something along lines of this:

unsigned int hour; unsigned int minute; unsigned int second; char colon1; char colon2; string ampm;  if (stream >> hour >> colon1 >>minute >> colon2 >> second >> ampm) {     if (colon1 == ':' && colon2 == ':')     {         if ((hour < 12) && (minute < 60) && (second < 60))         {             if (ampm == "am")             {                 // nothing             }             else if (ampm == "pm")             {                hour += 12;              }             else             {                 //freak out             }         }         else         {             //freak out         }     }     else     {         //freak out     } } else {     //freak out } 

Comments