Time Conversion

  • + 0 comments

    I tried the most manual approach to do these. I am not sure if these the best way to do it . PLease someone try and lets discuss if these is an acceptable answer for interviewer or not . string timeConversion(string s) { vector result; stringstream ss(s); string strToken;

    while(getline(ss,strToken,':')){
        result.push_back(strToken);
    }
    
    if(result[2][2] == 'A'){
        if(result[0] == "12"){
            result[0] = "00";
        }
    }
    else{
        int value = stoi(result[0]);
        if(value >= 1 && value <= 11){
            result[0] = to_string(12 + value);
        }
    }
    
    string ans = result[0] + ":" + result[1] + ":" + result[2].erase(4-2);
    return ans;  
    

    }