-
Notifications
You must be signed in to change notification settings - Fork 18k
time: Sub is inaccurate after computer has slept #23178
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
Comments
This is expected, see the documentation for the time package. You can always remove the monotonic part from a time.Time:
See also the issue for this: #12914 |
Now it all makes sense, many thanks! |
I have one concern with this: the use of monotonic clocks is advocated to ensure that elapsed time is measured correctly, independent of changes made to system clocks. However, this falls down if the computer sleeps. It seems there is no safe way to measure the real elapsed time, since:
Particularly with a lot of software running on laptops, the chance of 2) is probably higher than the chance of 1). And currently there is no third option that isn't skewed by sleep or system clock changes. If a third option can't be implemented that solves the deficiencies of 1) and 2), I think it would be good for the docs to specifically call out that during computer sleep, the monotonic clock stops. |
cc @rsc |
I think it is worth pointing out that 1 and 2 are problems of different magnitude. With 1, you could very easily have negative elapsed times. That would not only be confusing, but also easily break programs unless you took it into account. But with 2, a user can only stop the clock, not move it back. In other words, it is impossible to have negative elapsed times - just smaller ones. Not knowing much about monotonic times, I wonder - does any kernel or operating system keep track of sleep time as well as awake time? If it is possible with today's hardware at all. |
Change https://golang.org/cl/103396 mentions this issue: |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.9.2 darwin/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?What did you do?
I ran the following program, and then waited for my iMac sleep. I woke it up again.
What did you expect to see?
I expected the log output for Duration to equal the difference between the timestamps logged for Start and End. For example, the last line is:
If start is 14:03:49.5 and end is 14:14:37.9 then duration should be 10m48.4s not 2m42.6s.
Note, the 8 minute jump in end time from 14:06:26.7 to 14:14:33.9 was when the computer was sleeping.
What did you see instead?
The duration calculation seemed to match the amount of awake time between start and end time. Notice how when the end time jumps eight minutes (due to the computer being asleep), the duration only increments by around one second, as if no sleep had occurred.
The text was updated successfully, but these errors were encountered: