Skip to content

Instantly share code, notes, and snippets.

@devmeireles
Created August 10, 2020 14:30
Show Gist options
  • Save devmeireles/7520ccf9391f0065cc0fb473f4f430e6 to your computer and use it in GitHub Desktop.
Save devmeireles/7520ccf9391f0065cc0fb473f4f430e6 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"strconv"
)
/*
* Complete the timeConversion function below.
*/
func timeConversion(s string) string {
var militaryHour int
var formatedHour string
std := s[len(s)-2:]
splitTime := strings.Split(s[:len(s)-2], ":")
hour, _ := strconv.Atoi(splitTime[0])
if std == "PM" {
if hour == 12 {
militaryHour = hour
} else {
militaryHour = hour + 12
}
} else {
if hour == 12 {
militaryHour = 00
} else {
militaryHour = hour
}
}
formatedHour = fmt.Sprintf("%02d", militaryHour)
time := fmt.Sprint(formatedHour, ":", splitTime[1], ":", splitTime[2])
return time
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 1024 * 1024)
outputFile, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer outputFile.Close()
writer := bufio.NewWriterSize(outputFile, 1024 * 1024)
s := readLine(reader)
result := timeConversion(s)
fmt.Fprintf(writer, "%s\n", result)
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment