package main

import (
	"fmt"
	"log"
	"time"
)

func TzStrToOffset(ianaZone string) (string, error) {
	loc, err := time.LoadLocation(ianaZone)
	if err != nil {
		return "", fmt.Errorf("无法加载时区 %s: %v", ianaZone, err)
	}

	t := time.Date(2025, time.October, 28, 0, 0, 0, 0, loc)

	_, offsetSeconds := t.Zone()

	offset := time.Duration(offsetSeconds) * time.Second

	// 格式化偏移量。time.Time.Format() 无法直接将 Location 格式化成 UTC+HH:MM，
	// 因此我们需要手动计算并格式化。
	sign := "+"
	if offsetSeconds < 0 {
		sign = "-"
		offset = -offset // 确保偏移量为正值进行计算
	}

	hours := int(offset.Hours())
	minutes := int(offset.Minutes()) % 60

	// 最终的格式化字符串
	utcOffsetString := fmt.Sprintf("%s%02d%02d", sign, hours, minutes)

	return utcOffsetString, nil
}

func main() {
	ianaZone := "Asia/Shanghai"
	ianaZone = "UTC"

	utcOffsetString, err := TzStrToOffset(ianaZone)
	if err != nil {
		log.Fatalf("错误: %v", err)
	}

	fmt.Printf("IANA 时区: %s\n", ianaZone)
	fmt.Printf("UTC 偏移量: %s\n", utcOffsetString)
}
