mirror of
https://github.com/containers/podman.git
synced 2025-10-13 09:15:20 +08:00

Get the timezone off the localtime symlink like systemd does it. It is more efficient then fork/exec another command for it that may or may not exits and the /etc/timezone files doesn't exist on most distros so that is not a great fallback. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
30 lines
748 B
Go
30 lines
748 B
Go
package ignition
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func getLocalTimeZone() (string, error) {
|
|
path, err := filepath.EvalSymlinks("/etc/localtime")
|
|
if err != nil {
|
|
// of the path does not exist, ignore it as the code default to UTC then
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return "", nil
|
|
}
|
|
return "", err
|
|
}
|
|
|
|
// Allow using TZDIR per:
|
|
// https://sourceware.org/git/?p=glibc.git;a=blob;f=time/tzfile.c;h=8a923d0cccc927a106dc3e3c641be310893bab4e;hb=HEAD#l149
|
|
zoneinfo := os.Getenv("TZDIR")
|
|
if zoneinfo == "" {
|
|
// default zoneinfo location
|
|
zoneinfo = "/usr/share/zoneinfo"
|
|
}
|
|
// Trim of the TZDIR part to extract the actual timezone name
|
|
return strings.TrimPrefix(path, filepath.Clean(zoneinfo)+"/"), nil
|
|
}
|