12345678910111213141516171819202122232425262728 |
- /* Write a function that receives the time in 12-hour AM/PM format
- * and returns a string representation of the time in military (24-hour)
- * format.
- * Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock.
- * Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
- */
- std::string convertTime(std::string str) {
- // create substr of first 2 characters in string
- int cCheck = std::stoi (str.substr(0,2));
- // if cCheck is 12, set it equal to 0. Now AM or PM will show correctly. URHMHUMPH!!!
- if (12 == cCheck){
- cCheck = 0;
- }
- // use the substr for conditional if/else to see if 12 needs to be added
- // use AM/PM to see if it needs to be modified.
- if(str.substr(8,2)== "PM"){
- cCheck += 12;
- }
- // if cCheck is one digit, add a leading zero
- if(cCheck<10){
- str.replace(0,2,"0" + std::to_string(cCheck));
- }
- // otherwise cCheck is a two digit replacement
- else {
- str.replace(0,2,std::to_string(cCheck));
- }
- return str.substr(0,8);
- }
|