⏱️
Golangでグローバルにtimezoneを設定する
timezoneの初期値は?
- Golangのtimeパッケージでは、環境変数
TZがtimezone初期値として設定される。 - 環境変数
TZが設定されていない場合、システムのデフォルトの/etc/localtimeの設定がtimezone初期値として設定される。
Local represents the system's local time zone. On Unix systems, Local consults the TZ environment variable to find the time zone to use. No TZ means use the system default /etc/localtime. TZ="" means use UTC. TZ="foo" means use file foo in the system timezone directory.
設定方法
1. TZ環境変数をセットするtzinit.goを作成
tzinit.go
package tzinit
import (
"os"
)
func init() {
os.Setenv("TZ", "UTC")
}
2. tzinit.goをmain.goでインポート
main.go
package main
import _ "path/to/tzinit"
// Your other, "regular" imports:
import (
"fmt"
"os"
"time"
...
)
参考
Discussion