Skip to content

Instantly share code, notes, and snippets.

@aliuygur
Created July 28, 2022 19:18
Show Gist options
  • Save aliuygur/922d18c44eeb5d899b07da0c8cdcf5cc to your computer and use it in GitHub Desktop.
Save aliuygur/922d18c44eeb5d899b07da0c8cdcf5cc to your computer and use it in GitHub Desktop.
hackerrank Time Conversion problem solution in Golang
/*
* 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