github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/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  	"strings"
    15  	"time"
    16  
    17  	"github.com/cockroachdb/errors"
    18  )
    19  
    20  var errTZDataNotFound = errors.New("timezone data cannot be found")
    21  
    22  // LoadLocation returns the time.Location with the given name.
    23  // The name is taken to be a location name corresponding to a file
    24  // in the IANA Time Zone database, such as "America/New_York".
    25  //
    26  // We do not use Go's time.LoadLocation() directly because:
    27  // 1) it maps "Local" to the local time zone, whereas we want UTC.
    28  // 2) when a tz is not found, it reports some garbage message
    29  // related to zoneinfo.zip, which we don't ship, instead
    30  // of a more useful message like "the tz file with such name
    31  // is not present in one of the standard tz locations".
    32  func LoadLocation(name string) (*time.Location, error) {
    33  	switch strings.ToLower(name) {
    34  	case "local", "default":
    35  		name = "UTC"
    36  	case "utc":
    37  		// TODO(knz): See #36864. This code is a crutch, and should be
    38  		// removed in favor of a cache of available locations with
    39  		// case-insensitive lookup.
    40  		name = "UTC"
    41  	}
    42  	l, err := time.LoadLocation(name)
    43  	if err != nil && strings.Contains(err.Error(), "zoneinfo.zip") {
    44  		err = errTZDataNotFound
    45  	}
    46  	return l, err
    47  }