Files
tools/internal/packer/localtime.go
2023-01-15 14:12:11 +01:00

61 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package packer
import (
"archive/zip"
"io"
"os"
"path/filepath"
"runtime"
)
func hostLocaltime(tmpdir string) (string, error) {
hostLocaltime := "/etc/localtime"
if _, err := os.Stat(hostLocaltime); err == nil {
return hostLocaltime, nil
}
// Fallback to time zone “Factory” from Gos copy of zoneinfo.zip.
//
// Unfortunately, we cannot directly use the time/tzdata package (an
// embedded copy of the timezone database in the Go standard library),
// because the standard library only provides code to *load* that copy, but
// not to write the loadable copy to a file (or to generate a timezone
// database file).
r, err := zip.OpenReader(filepath.Join(runtime.GOROOT(), "lib", "time", "zoneinfo.zip"))
if err != nil {
if os.IsNotExist(err) {
// Some Go installations are missing lib/zoneinfo.zip
// (e.g. Debians).
return "", nil
}
return "", err
}
defer r.Close()
for _, f := range r.File {
if f.Name != "Factory" {
continue
}
hostLocaltime = filepath.Join(tmpdir, "Factory")
out, err := os.Create(hostLocaltime)
if err != nil {
return "", err
}
defer out.Close()
rc, err := f.Open()
if err != nil {
return "", err
}
defer rc.Close()
if _, err := io.Copy(out, rc); err != nil {
return "", err
}
rc.Close()
if err := out.Close(); err != nil {
return "", err
}
break
}
return hostLocaltime, nil
}