A
A
Anton2019-04-30 15:38:13
go
Anton, 2019-04-30 15:38:13

How to convert AM/PM time to 24h format in golang?

I'm trying to solve the problem
https://www.hackerrank.com/challenges/time-convers...
Code:

func timeConversion(s string) string {
    format  := s[len(s)-2:]
    strHour := s[:2]
    hour, err := strconv.Atoi(strHour)
    if err != nil {
        fmt.Println(err)
    }
    if format == "AM" && hour == 12 {
        return "00" + s[2:len(s)-2]
    }
    if format == "PM" && hour == 12 {
        return "12" + s[2:len(s)-2]
    }
    if format == "PM" {
        hour = hour + 12
    }
    return strconv.Itoa(hour) + s[2:len(s)-2]
}

2 tests fail.
This code have 2/10 test cases failed :(
What test cases could I have forgotten?
Checked 12:00:00PM, 12:01:00PM, 12:00:00AM, 12:01:00AM.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2019-04-30
@dimonchik2013

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.

hilarious condition
always thought the opposite
(PS not only me)

A
Anton, 2019-05-01
Patsev

Replaced with And the tests passed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question