package main import ( "flag" "fmt" "os" "time" ) var ( zoneinfoPath = flag.String("path", "/usr/share/zoneinfo", "used to parse timezone") timezoneName = flag.String("tz", "America/Adak", "the timezone you want transfer") localTime = flag.String("l", "2022-11-01 00:00:00", "local time need to transfer") ) const ( envZoneinfo = "ZONEINFO" longForm = "2006-01-02 15:04:05" ) func main() { flag.Parse() os.Setenv(envZoneinfo, *zoneinfoPath) if path := os.Getenv(envZoneinfo); path != *zoneinfoPath { fmt.Println("The env ZONEINFO is not what you set: ", path, *zoneinfoPath) return } // after set the env ZONEINFO to the zoneinfo path location, err := time.LoadLocation(*timezoneName) if err != nil { fmt.Println("Load location fail", *timezoneName, err) return } parseTime, err := time.ParseInLocation(longForm, *localTime, location) if err != nil { fmt.Println("Parse local time fail", *localTime, err) return } fmt.Println("UTC:", parseTime.UTC()) }