Time Zones Load Parse And Convert
Load named zones with LoadLocation, pin offset-less wall clocks with ParseInLocation, and convert the same instant across UTC and other zones.
Code
package main
import (
"fmt"
"time"
)
func main() {
shanghai, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println("load shanghai:", err)
return
}
// ParseInLocation:无偏移的挂钟时间落到指定时区
deployed, err := time.ParseInLocation("2006-01-02 15:04:05", "2026-03-01 09:30:00", shanghai)
if err != nil {
fmt.Println("parse:", err)
return
}
name, offset := deployed.Zone()
fmt.Println("zone:", name, "offset seconds:", offset)
fmt.Println("utc:", deployed.UTC().Format(time.RFC3339))
// 同一时刻换时区:挂钟时间随 Zone 变化
tokyo, err := time.LoadLocation("Asia/Tokyo")
if err != nil {
fmt.Println("load tokyo:", err)
return
}
fmt.Println("tokyo:", deployed.In(tokyo).Format(time.RFC3339))
}How it works
Time Zones Load Parse And Convert is built around the function main.
Nothing falls through silently — 3 checkpoints test err != nil on the way through.
Applying it
Running the file prints labeled results such as load shanghai:, parse:, zone: — each operation’s effect is visible in the output.
Statements that matter
if err != nil {
fmt.Println("load shanghai:", err)
return
}
if err != nil {
fmt.Println("parse:", err)
return
}
if err != nil {
fmt.Println("load tokyo:", err)
return
}
Interactive Tools for This Topic
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.ISO 8601 Duration ParserParse ISO 8601 durations like PT1H30M into year, month, week, day, and time components with exact or approximate total seconds.
FAQ
- What does Time Zones Load Parse And Convert print when run?
- Execute the whole file; each printed line is prefixed with a label like `load shanghai:`, `parse:`, `zone:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.