Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: notice system timezone changes #28020

Open
tv42 opened this issue Oct 4, 2018 · 6 comments
Open

time: notice system timezone changes #28020

tv42 opened this issue Oct 4, 2018 · 6 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@tv42
Copy link

tv42 commented Oct 4, 2018

What version of Go are you using (go version)?

go version go1.11.1 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

linux/amd64

What did you do?

package main

import (
	"fmt"
	"time"
)

func main() {
	for {
		time.Sleep(1 * time.Second)
		fmt.Println(time.Now())
	}
}

in one terminal:

$ go run time.go

in another (note, this changes your computer's timezone):

$ timedatectl set-timezone America/Denver
$ date
Thu Oct  4 12:48:50 MDT 2018
$ timedatectl set-timezone America/Los_Angeles
$ date
Thu Oct  4 11:48:56 PDT 2018
$ 

What did you expect to see?

Go process seeing that the current time changes between MDT and PDT, time displayed changing by an hour.

As far as I can tell, there's not even a way to tell package time to re-initialize its idea of local time, even if my app somehow (in a platform-specific way) realized that it was necessary.

This hurts long-running end-user apps when users travel. Think of e.g. a desktop clock.

What did you see instead?

2018-10-04 12:48:48.12727581 -0600 MDT m=+1.000767878
2018-10-04 12:48:49.127890815 -0600 MDT m=+2.001383092
2018-10-04 12:48:50.128187914 -0600 MDT m=+3.001680261
2018-10-04 12:48:51.128403029 -0600 MDT m=+4.001895306
2018-10-04 12:48:52.128632471 -0600 MDT m=+5.002124678
2018-10-04 12:48:53.128842855 -0600 MDT m=+6.002335551
2018-10-04 12:48:54.129075736 -0600 MDT m=+7.002568154
2018-10-04 12:48:55.129282227 -0600 MDT m=+8.002787356
2018-10-04 12:48:56.129528956 -0600 MDT m=+9.003021303
2018-10-04 12:48:57.129734907 -0600 MDT m=+10.003227044
2018-10-04 12:48:58.129960422 -0600 MDT m=+11.003452839
2018-10-04 12:48:59.130145298 -0600 MDT m=+12.003637576
@ianlancetaylor ianlancetaylor changed the title time: Follow timezone changes time: notice system timezone changes Oct 4, 2018
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 4, 2018
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Oct 4, 2018
@ianlancetaylor
Copy link
Contributor

What does the C library do?

@tv42
Copy link
Author

tv42 commented Oct 5, 2018

I haven't (yet) written C to test it, but my current understanding is:

localtime(3) and friends cache values on first use. tzset(3) reloads the cache. So libc enables one to easily handle this in the app; Go doesn't. (And, IMHO, Go should aim higher than libc.)

glibc argument for not stat(2)ing /etc/localtime on every localtime(3) call was performance. inotify and friends might change that equation.

Random discussion found:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=48184
https://sourceware.org/bugzilla/show_bug.cgi?id=154

soumya92 added a commit to soumya92/barista that referenced this issue Dec 7, 2018
Go pins time.Local at the start of the binary (golang/go#28020), but because the bar can run for days on end even as the machine moves around the world, we need to track the machine timezone for local clock modules.
This changes clock.Module to handle a nil timezone as "true" machine-local time zone, updating the output any time the timezone changes locally.
@perillo
Copy link
Contributor

perillo commented Feb 10, 2022

I found an example in C from https://stackoverflow.com/questions/2251635/how-to-detect-change-of-system-time-in-linux.
The code uses timerfd_settime with TFD_TIMER_CANCEL_ON_SET.

I have not tested it personally.

@tv42
Copy link
Author

tv42 commented Feb 10, 2022

@perillo The timezone can change without clock_settime, just by changing /etc/localtime. So your link detects actual clock changes, and might be useful for "do this at 08:00" (though TFD_TIMER_ABSTIME seems to handle that already?), but something that just shows a timestamp on demand or once a second doesn't really benefit from it, and TFD_TIMER_CANCEL_ON_SET by itself is not sufficient to notice human-visible clock changes.

@wizardishungry
Copy link

I cooked up a little library to watch /etc/localtime for changes to the symlink – https://github.com/wizardishungry/tznotify

@elbertcastaneda
Copy link

elbertcastaneda commented May 3, 2023

Here is the error happening on live also in Windows:

golang-timezone-changing-bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants