Time Conversion Algorithm in C++: Hackerrank Solution Explained

Time Conversion Algorithm in C++: Hackerrank Solution Explained

Play this article

Question:

Given a time in 12-hour AM/PM format, how can you convert it to military (24-hour) time?

Question Link: https://www.hackerrank.com/challenges/time-conversion/problem?isFullScreen=true

IMPORTANT: Attempt to solve the problem on your own first. If you encounter any difficulties, feel free to watch the first half of the video, which explains the logic behind this problem. Give it another try before returning to watch the complete solution.

What You Should Know:

1. std::string 2. std::stringstream 3. std::setw() 4. std::setfill()

Notes: Be sure to watch this video in Full HD. The video is available in 4K.

Video Explanation:

/**
        Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
        - 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

        Example:
        s = '12:01:00PM'
        Return '12:01:00'.

        s = '12:01:00AM'
        Return '00:01:00'.
        =======================
 * setw(n)- Sets the width parameter of the stream ss to exactly n. (Header "iomanip")
 * setfill(0) - fills the charecter passed(0) if width is not satisfied.
 * To extract the h, m ,s & meridian use: 
    std::stringstream
    string::substr()
 * Explore more on std::left(),std::right() & std::internal()
 * 
 * @Logic
 * 1. Extract individual attributes like hh,mm,ss and meridian.
  keep hr,m,sec as int., as we are going to do some arithmetic operations & comparisions) - Use std::stringstream
 * 3. If AM, and hr=12, then make hr = 00(This is midnight)
 * 4. If PM, and hr != 12, then make hr = 12 + hr(Afternoon )
 * 5. Form the output by using hh:mm:ss format. - Use std::setw() & std::setfill()

 * Github Link: https://github.com/subrat80/hacker-rank-algo/blob/main/30-timeConversion.cpp
****/

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

// #include<format> // C++20

std::string timeConversion(std::string s) {
    std::stringstream ss{s};
    int hour, minute, second;
    std::string meridian;
    ss >> hour;
    ss.ignore(1);
    ss >> minute;
    ss.ignore(1);
    ss >> second;
    ss >> meridian;
    if(meridian == "AM" && hour == 12) {
        hour = 0;
    }
    if(meridian == "PM" && hour < 12) {
        hour += 12;
    }
/* ANOTHER APPROACH
    if (hour == 12) {
        hour = 0;
    }

    if (meridian == "PM") {
        hour += 12;
    }*/

    // return std::format("{:02d}:{:02d}:{:02d}", hour, minute, second); // C++20-Compiler Support is neeed.
    std::stringstream military_time;
    military_time << std::setw(2) << std::setfill('0') << hour << ":"
                  << std::setw(2) << std::setfill('0') << minute << ":"
                  << std::setw(2) << std::setfill('0') << second;
    return military_time.str();
}

int main() {
    std::cout << timeConversion("07:05:45PM") << '\n'; // 19:05:45
    std::cout << timeConversion("12:15:45AM") << '\n'; // 00:15:45

    std::cout << timeConversion("06:40:03AM") << '\n'; // 06:40:03
    std::cout << timeConversion("04:59:59AM") << '\n'; // 04:59:59 
    std::cout << timeConversion("12:45:54PM") << '\n'; // 12:45:54   
    // Space test  before AM/PM
    std::cout << '\n'
              << timeConversion("12:45:54 PM") << '\n'; // 12:45:54  
    return 0;
}

Compile & Test: here

GitHub Link: here

Happy learning and coding!


Want to take your C++ skills to the next level? Visit cppforme.com for resources, tips, and expert advice to help you improve your coding techniques and become a better developer. And if you need additional help or support, feel free to contact me by visiting my site.