Home  >  Article  >  Backend Development  >  Go hour conversion algorithm is not converting correctly

Go hour conversion algorithm is not converting correctly

WBOY
WBOYforward
2024-02-14 10:00:09733browse

Go 小时转换算法未正确转换

What php editor Xinyi will introduce to you today is a problem in the Go language, that is, "Go hour conversion algorithm is not converted correctly". In Go language, time conversion is a common operation, but in some cases, the result of hour conversion may be wrong. This article will explain the cause of this problem in detail and provide solutions to help developers avoid errors when using the Go language for time conversion.

Question content

I came across a hackerrank challenge where I was supposed to build a function that converts a string in a given time format from 12 hour format to 24 hour format.

I managed to do this using the code below, but as you noticed, there is a special case at 9pm. Whenever I enter an hour that starts with 09 (that's the only damn thing), it converts 09 to 12. So I had to create a specific case to handle this issue, which worked, but I wanted to understand why it happened. Do you know what the problem is?

package main

import (
    "fmt"
    "strconv"
    "strings"
)

type Conversion struct {
    conversion string
}

func timeConversion(s string) string {
    var conversion Conversion
    firstValue := fmt.Sprintf("%s%s", string(s[0]), string(s[1]))
    secondValue := fmt.Sprintf("%s%s", string(s[3]), string(s[4]))
    firstValueNumber, _ := strconv.ParseInt(firstValue, 0, 16)

    fmt.Print()

    if strings.Contains(s, "A") {
        if firstValue == "12" {
            conversion.conversion = fmt.Sprintf("%s:%s:%s%s", "00", secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        } else {
            conversion.conversion = fmt.Sprintf("%s:%s:%s%s", firstValue, secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        }
    } else if strings.Contains(s, "P") {
        if firstValue == "12" {
            conversion.conversion = fmt.Sprintf("%s:%s:%s%s", "12", secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        } else if firstValue == "09" {
            conversion.conversion = fmt.Sprintf("%s:%s:%s%s", "21", secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        } else {
            conversion.conversion = fmt.Sprintf("%d:%s:%s%s", firstValueNumber+12, secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        }
    }

    return conversion.conversion
}

func main() {
    fmt.Print(timeConversion("09:08:23PM"))
}

Workaround

If you find an error during string to int conversion, you will notice that for the value >7, the conversion fails

firstValueNumber, err := strconv.ParseInt(firstValue, 0, 64)
if err != nil {
     fmt.Printf("%s", err.Error())
}
// "08" results in invalid syntaxfirstValueNumber being printed

This happens because you pass 0 as the base, telling parseint to infer the base from the first character of the string. The leading '0' implies octal base, so numbers greater than 7 will be invalid. In contrast, passing 10 requires no special handling.

The above is the detailed content of Go hour conversion algorithm is not converting correctly. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete