Getting date according to system date command

In Go language, if a user is using time.Now() to get system local time in after every interval of let say 2 secs. Now in the mean time in the system someone updated time zone. Now here the issue is time.Now() still gives time with older time zone. Is there any procedure in golang to get time with updated time?

This sounds indeed unexpected. To narrow down the problem:

When does the Go app pick up the changed time zone? After some delay, or only after restarting the app?

Did you test this with another programming language, too? (To rule out that the issue is outside Go; for example, the shell might still deliver the old time zone until starting a new one.)

Do you have some minimal test code available so that others could try replicating it on their machines?

Which OS and shell are you using? Did you test with other OS’es and/or shells?

for{
time.Sleep(5*time.Second)
t :=time.Now()
fmt.Println(t.String())
}

This program is running in background. At runtime I updated the timezone with command
sudo ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
But still in the program output I am getting the time according to the initial time zone.

The time in Go app is updated only after restarting the app.

What if you do the same in Pyhton/Perl/Bash or some other language? (I ask this to find out if the behavior is specific to Go.)

I am not that familiar with timezone handling lin LInux, but ln -s only creates a new link in the file system. I wonder if every running process in the system is expected to pick up the changed file link immediately.

Most or all Unix programs read timezone data only at startup. At least glibc and, apparently, Go behaves this way. The alternative would be for every gettime() call to make filesystem operations, which would be unsustainable.

1 Like

There is no problem, it’s expected. See this issue.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.