Golang:使用自定义时区分叉进程。

huangapple go评论71阅读模式
英文:

Golang: fork process with custom timezone

问题

在Golang中,是否可以fork一个子进程并强制其使用与UTC不同的时区?例如,我的服务器时区是UTC,我希望fork的进程使用UTC+5时区。

英文:

In Golang is it possible to fork a child process and force it to use a shifted timezone from UTC? For instance, my server's timezone is UTC, and I want the forked process to use UTC+5.

答案1

得分: 2

这个问题的答案将为您提供更多详细信息(https://stackoverflow.com/questions/54363451/setting-timezone-globally-in-golang),但最终您可以简单地设置TZ环境变量,然后执行以下命令:

cmd := exec.Command("myprogram")
cmdtz := "TZ=America/New_York"
cmd.Env = append(os.Environ(), cmdtz)
if err := cmd.Run(); err != nil {
    log.Fatal(err)
}
英文:

The answers to this question will give you more details (https://stackoverflow.com/questions/54363451/setting-timezone-globally-in-golang) but ultimately you can simply set the TZ environment variable and then execute the command:

cmd := exec.Command("myprogram")
cmdtz := "TZ=America/New_York"
cmd.Env = append(os.Environ(), cmdtz)
if err := cmd.Run(); err != nil {
	log.Fatal(err)
}

huangapple
  • 本文由 发表于 2021年12月28日 22:44:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/70508635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定