Table of Contents
ToggleProblem Statement
Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
Sample Input:
07:05:45PM
Sample Output:
19:05:45
Time Conversion HackerRank Solution
Intuition
Converting a time from a 12-hour format (AM/PM) to the military 24-hour format might sound straightforward at first glance, but there are a few intricacies to consider:
AM/PM Designation
This is the primary differentiator between the two-time formats. The presence of “AM” or “PM” at the end of the time string clearly indicates the 12-hour format. This designation affects how we interpret the hours.
The Hour Component (hh):
- In a 12-hour format:
- 12:00 AM represents midnight and 12:00 PM represents noon.
- The hours are identical in both formats for times from 01:00 AM to 11:59 AM.
- For times from 01:00 PM to 11:59 PM, the 24-hour format requires adding 12 to the hour component.
- In a 24-hour format:
- Midnight is represented as 00:00.
- Noon remains at 12:00.
- Post-noon, the hour component increases beyond 12, up to 23.
The Minute and Second Components (mm: ss)
These remain unchanged between the two formats, so no conversion is required for them.
Given this understanding, here’s a step-by-step breakdown of the conversion:
- Extract the Components: Parse the input string to extract the hour, minute, and second components. Also, identify the AM/PM designation.
- Hour Conversion
- If the time is in the PM (except for 12:00 PM to 12:59 PM), add 12 to the hour component.
- If the time is in the AM and falls between 12:00 AM and 12:59 AM, change the hour component to 00.
- In all other cases, the hour component remains unchanged.
- Reassemble the Time: Using the possibly modified hour component and the unchanged minute and second components, construct the time string in the 24-hour format.
Code Implementation
C
// Time Conversion: HackerRank Problem C Solution
#include <stdio.h>
#include <string.h>
int main() {
char time[10];
scanf("%s", time);
// Extract hours, minutes, and seconds from the input
int hh, mm, ss;
sscanf(time, "%02d:%02d:%02d", &hh, &mm, &ss);
// Check if the time is PM, but not 12:00 PM
if (strstr(time, "PM") && hh != 12) {
hh += 12;
}
// Check if the time is AM and exactly 12:00 AM
if (strstr(time, "AM") && hh == 12) {
hh = 0;
}
// Print the result in 24-hour format
printf("%02d:%02d:%02d", hh, mm, ss);
return 0;
}
C++
// Time Conversion: HackerRank Problem C++ Solution
#include <iostream>
#include <string>
#include <iomanip>
int main() {
// Read the input time in a 12-hour format
std::string time;
std::cin >> time;
// Extract hour, minute, and second components from the input string
int hh = std::stoi(time.substr(0, 2));
int mm = std::stoi(time.substr(3, 2));
int ss = std::stoi(time.substr(6, 2));
// Extract AM/PM designation
std::string period = time.substr(8, 2);
// Convert 12-hour format to 24-hour format
if (period == "PM" && hh != 12) {
hh += 12;
}
if (period == "AM" && hh == 12) {
hh = 0;
}
// Print the result in 24-hour format
std::cout << std::setw(2) << std::setfill('0') << hh << ":"
<< std::setw(2) << std::setfill('0') << mm << ":"
<< std::setw(2) << std::setfill('0') << ss;
return 0;
}
Java
// Time Conversion: HackerRank Problem Java Solution
import java.util.Scanner;
public class TimeConversion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read the input time in a 12-hour format
String time = sc.nextLine();
sc.close();
// Extract hour, minute, and second components from the input string
int hh = Integer.parseInt(time.substring(0, 2));
int mm = Integer.parseInt(time.substring(3, 5));
int ss = Integer.parseInt(time.substring(6, 8));
// Extract AM/PM designation
String period = time.substring(8, 10);
// Convert 12-hour format to 24-hour format
if (period.equals("PM") && hh != 12) {
hh += 12;
}
if (period.equals("AM") && hh == 12) {
hh = 0;
}
// Print the result in 24-hour format
System.out.printf("%02d:%02d:%02d", hh, mm, ss);
}
}
Python
# Time Conversion: HackerRank Problem Python Solution
# Read the input time in a 12-hour format
time = input()
# Split the input string to extract hour, minute, and second components
hh, mm, ss = map(int, time[:-2].split(':'))
# Extract AM/PM designation
period = time[-2:]
# Convert 12-hour format to 24-hour format
if period == "PM" and hh != 12:
hh += 12
if period == "AM" and hh == 12:
hh = 0
# Print the result in a 24-hour format
print(f"{hh:02}:{mm:02}:{ss:02}")
Output:
For the input 07:05:45PM, the output will be 19:05:45.
Dry Run and Code Explanation
- Consider the input 07:05:45PM.
- Extract the hours, minutes, and seconds using sscanf(). So, hh = 7, mm = 5, and ss = 45.
- Check if the time string contains “PM” and the hour is not 12. If true, add 12 to the hours. Here, since the time is 07:05:45PM, the hour becomes 7 + 12 = 19.
- If the time string contains “AM” and the hour is 12, set the hour to 0. This handles the edge case of 12:xx:xxAM.
- Finally, print the hours, minutes, and seconds in 24-hour format. The result is 19:05:45.
I hope You liked the post ?. For more such posts, ? subscribe to our newsletter. Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.