Skip to content

Commit

Permalink
added setLocaltime
Browse files Browse the repository at this point in the history
  • Loading branch information
kwitsch authored and 0xERR0R committed Nov 12, 2022
1 parent f15c7d3 commit cf4e894
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions main_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package main

import (
"fmt"
"os"
"time"
_ "time/tzdata"
Expand All @@ -16,12 +15,27 @@ import (
func init() {
go reaper.Reap()

setLocaltime()
}

// set localtime to /etc/localtime if available
// or modify the system time with the TZ environment variable if it is provided
func setLocaltime() {
// load /etc/localtime without modifying it
if lt, err := os.ReadFile("/etc/localtime"); err == nil {
if t, err := time.LoadLocationFromTZData("", lt); err == nil {
time.Local = t

return
}
}

// use zoneinfo from time/tzdata and set location with the TZ environment variable
if tz := os.Getenv("TZ"); tz != "" {
var err error
time.Local, err = time.LoadLocation(tz)
if t, err := time.LoadLocation(tz); err == nil {
time.Local = t

if err != nil {
fmt.Printf("error loading location '%s': %v\n", tz, err)
return
}
}
}

0 comments on commit cf4e894

Please sign in to comment.