...

Source file src/github.com/thlib/go-timezone-local/tzlocal/tz.go

Documentation: github.com/thlib/go-timezone-local/tzlocal

     1  package tzlocal
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"time"
     7  )
     8  
     9  // EnvTZ will return the TZ env value if it is set, go will revert any invalid timezone to UTC
    10  func EnvTZ() (string, bool) {
    11  	if name, ok := os.LookupEnv("TZ"); ok {
    12  		// Go treats blank as UTC
    13  		if name == "" {
    14  			return "UTC", true
    15  		}
    16  		_, err := time.LoadLocation(name)
    17  		// Go treats invalid as UTC
    18  		if err != nil {
    19  			return "UTC", true
    20  		}
    21  		return name, true
    22  	}
    23  	return "", false
    24  }
    25  
    26  // RuntimeTZ get the full timezone name of the local machine
    27  func RuntimeTZ() (string, error) {
    28  
    29  	// Get the timezone from the TZ env variable
    30  	if name, ok := EnvTZ(); ok {
    31  		return name, nil
    32  	}
    33  
    34  	// Get the timezone from the system file
    35  	name, err := LocalTZ()
    36  	if err != nil {
    37  		err = fmt.Errorf("failed to get local machine timezone: %w", err)
    38  		return "", err
    39  	}
    40  
    41  	return name, err
    42  }
    43  

View as plain text