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.

GoData Integrity

Code

gosnippet
33 lines
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

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:`.