Created
July 28, 2022 19:18
-
-
Save aliuygur/922d18c44eeb5d899b07da0c8cdcf5cc to your computer and use it in GitHub Desktop.
hackerrank Time Conversion problem solution in Golang
This file contains hidden or 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
/* | |
* Complete the 'timeConversion' function below. | |
* | |
* The function is expected to return a STRING. | |
* The function accepts STRING s as parameter. | |
*/ | |
func timeConversion(s string) string { | |
var AM_PM string = s[len(s)-2:] | |
// split time strings | |
tt := strings.Split(s[:len(s)-2], ":") | |
// convert hour to int | |
h, err := strconv.Atoi(tt[0]) | |
if err != nil { | |
panic(err) | |
} | |
// if before noon minus 12 from hour else add 12 to hour | |
if AM_PM == "AM" { | |
if h == 12 { | |
h = 0 | |
} | |
} else { | |
if h != 12 { | |
h += 12 | |
} | |
} | |
return fmt.Sprintf("%02d:%s:%s", h, tt[1], tt[2]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment