github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/timeutil/zoneinfo.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package timeutil 12 13 import ( 14 "sort" 15 "strings" 16 "sync" 17 "time" 18 // embed tzdata in case system tzdata is not available. 19 _ "time/tzdata" 20 ) 21 22 //go:generate go run gen/main.go 23 24 // LoadLocation returns the time.Location with the given name. 25 // The name is taken to be a location name corresponding to a file 26 // in the IANA Time Zone database, such as "America/New_York". 27 // 28 // We do not use Go's time.LoadLocation() directly because it maps 29 // "Local" to the local time zone, whereas we want UTC. 30 func LoadLocation(name string) (*time.Location, error) { 31 loweredName := strings.ToLower(name) 32 switch loweredName { 33 case "local", "default": 34 loweredName = "utc" 35 name = "UTC" 36 } 37 // If we know this is a lowercase name in tzdata, use the uppercase form. 38 if v, ok := lowercaseTimezones[loweredName]; ok { 39 // If this location is not found, we may have a case where the tzdata names 40 // have different values than the system tz names. 41 // If this is the case, allback onto the default logic, where the name is read 42 // off other sources before tzdata. 43 if loc, err := time.LoadLocation(v); err == nil { 44 return loc, nil 45 } 46 } 47 return time.LoadLocation(name) 48 } 49 50 var tzsOnce sync.Once 51 var tzs []string 52 53 // TimeZones lists all supported timezones. 54 func TimeZones() []string { 55 tzsOnce.Do(func() { 56 tzs = make([]string, 0, len(lowercaseTimezones)) 57 for _, tz := range lowercaseTimezones { 58 tzs = append(tzs, tz) 59 } 60 sort.Strings(tzs) 61 }) 62 return tzs 63 }