Time Conversion – HackerRank Solution

Time Conversion – HackerRank Solution

Problem :

Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.

Note: 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.

Function Description :

Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.

timeConversion has the following parameter(s):

  • s: a string representing time in 12 hour format

Input Format :

A single string s containing a time in 12-hour clock format  (i.e.: hh:mm:ssAM  or hh:mm:ssPM), where 01 <= hh <= 12 and 00 <= mm,ss <= 59.

Constraints :

  • All input times are valid

Output Format :

Convert and print the given time in 24-hour format, where 00 <= hh <= 23.

Sample Input 0 :

07:05:45PM

Sample Output 0 :

19:05:45

Solution :

#include <bits/stdc++.h>
#include <sstream>
#include <iomanip>


using namespace std;

string timeConversion(string s) {
    string time;
    char dot;
    int time1 = 0, time2 = 0, time3 = 0;
    string hours;

    stringstream ss;
    ss << s;
    ss >> time1 >> dot >> time2 >> dot >> time3 >> hours;

    if (time1 == 12 && hours == "AM") {
        time1 = 00;
    } else if ((0 <= time1 && time1 <= 6) && (hours == "AM")) {
        time1 = time1 + 0;
    } else if (time1 == 12 && hours == "PM") {
        time1 = time1 + 0;
    } else {
        time1 = time1 + 12;
    }
    if (0 <= time1 && time1 < 10) {
        if ((0 <= time2 && time2 <= 9) && (0 <= time3 && time3 <= 9)) {
            time = '0' + to_string(time1) + dot + '0' + to_string(time2) + dot + '0' + to_string(time3);
        } else if (0 <= time2 && time2 <= 9) {
            time = '0' + to_string(time1) + dot + '0' + to_string(time2) + dot + to_string(time3);
        } else if (0 <= time3 && time3 <= 9) {
            time = '0' + to_string(time1) + dot + to_string(time2) + dot + '0' + to_string(time3);
        } else {
            time = '0' + to_string(time1) + dot + to_string(time2) + dot + to_string(time3);
        }
    } else {
        if ((0 <= time2 && time2 <= 9) && (0 <= time3 && time3 <= 9)) {
            time = to_string(time1) + dot + '0' + to_string(time2) + dot + '0' + to_string(time3);
        } else if (0 <= time2 && time2 <= 9) {
            time = to_string(time1) + dot + '0' + to_string(time2) + dot + to_string(time3);
        } else if (0 <= time3 && time3 <= 9) {
            time = to_string(time1) + dot + to_string(time2) + dot + '0' + to_string(time3);
        } else {
            time = to_string(time1) + dot + to_string(time2) + dot + to_string(time3);
        }
    }
    return time;

}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string s;
    getline(cin, s);

    string result = timeConversion(s);

    fout << result << "\n";

    fout.close();

    return 0;
}

 427 total views,  1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *