advent-of-code/hacker-rank/time-conversion/main.go

61 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-10-26 12:03:06 -06:00
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"time"
)
/*
* 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 {
dt, err := time.Parse("03:04:05PM", s)
if err != nil {
println(err.Error())
return ""
}
return dt.Format("15:04:05")
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16 * 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)
}
}