Why Doesn't Go's Time.Parse Function Parse the Timezone Identifier?
When parsing a time string using Go's time.Parse function, timezone identifiers such as "MST" are interpreted based on the "current location" setting. If the current location does not recognize the abbreviation, the time is recorded as being in a fabricated location with a zero offset.
To illustrate, the following code snippet demonstrates the issue:
date := "2018 08 01 12:00 EDT" tn, _ := time.Parse(format, date) fmt.Println(tn) // Print +0000 despite EDT indicating a -0400 offset
This code parses the date string in the format "2006 01 02 15:04 MST" using the time.Parse function, and then prints the resulting Time object. However, it prints " 0000" instead of the expected "-0400" because the current location used by the function does not recognize the "EDT" abbreviation.
To avoid this issue, you can use alternative methods for parsing time strings:
By leveraging these methods, you can accurately parse time strings with zone abbreviations, ensuring the correct interpretation of timezone information.
The above is the detailed content of Why Does Go\'s `time.Parse` Misinterpret Timezone Identifiers Like \'EDT\'?. For more information, please follow other related articles on the PHP Chinese website!