How to parse timezone codes

In the example below, the result is always "[date] 05:00:00 +0000 UTC" regardless of the time zone selected for the parseAndPrint function. What is wrong with this code? The time should vary depending on the time zone selected. (Go Playground servers are explicitly configured in the UTC time zone).

http://play.golang.org/p/wP207BWYEd

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    parseAndPrint(now, "BRT")
    parseAndPrint(now, "EDT")
    parseAndPrint(now, "UTC")
}

func parseAndPrint(now time.Time, timezone string) {
    test, err := time.Parse("15:04:05 MST", fmt.Sprintf("05:00:00 %s", timezone))
    if err != nil {
        fmt.Println(err)
        return
    }

    test = time.Date(
        now.Year(),
        now.Month(),
        now.Day(),
        test.Hour(),
        test.Minute(),
        test.Second(),
        test.Nanosecond(),
        test.Location(),
    )

    fmt.Println(test.UTC())
}
+4
source share
1 answer

When you analyze time, you analyze it at your current location, which is fine as long as it is what you expect and the reduction of the time zone paragraph is known from your location.

, , , UTC.

, -05:00.

, , time.Location. Locations db time.LoadLocation time.ParseInLocation.

+7

All Articles