Created
June 18, 2017 22:37
-
-
Save stellamiranda/852a4a371723ee780dc785dd514ea72c to your computer and use it in GitHub Desktop.
Time Conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Challenge: Time Conversion - HakerRank | |
# Author: Stella Miranda | |
# 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. | |
# Input Format: | |
# A single string containing a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), | |
# where 1<= hh <= 12 and 0<= mm,ss<=59 . | |
# Output Format: | |
# Convert and print the given time in 24-hour format, where 00<= hh<= 23 . | |
# Sample Input | |
# 07:05:45PM | |
# Sample Output | |
# 19:05:45 | |
#!/bin/ruby | |
time = gets.strip | |
temp = time.split(":") | |
result = "" | |
if temp[0].to_i<12 and temp[2].to_s[2,3] == "PM" | |
result = (temp[0].to_i+12).to_s+":"+temp[1]+":"+temp[2].to_s[0,2] | |
elsif temp[0].to_i ==12 and temp[2].to_s[2,3] == "AM" | |
result = "00:"+temp[1]+":"+temp[2].to_s[0,2] | |
else | |
result = temp[0]+":"+temp[1]+":"+temp[2].to_s[0,2] | |
end | |
puts result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment