Skip to content

time: Sub is inaccurate after computer has slept #23178

Closed
@petemoore

Description

@petemoore

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)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/pmoore/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v9/mll6p_rj5h94dt_m5m8j0f9c0000gn/T/go-build287238210=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.13.1
BuildVersion:	17B1003

What did you do?

I ran the following program, and then waited for my iMac sleep. I woke it up again.

$ cat main.go 
package main

import (
	"fmt"
	"time"
)

func main() {
	start := time.Now()
	for {
		time.Sleep(time.Second)
		end := time.Now()
		fmt.Printf("Start: %v, End: %v, Duration: %v\n", start, end, end.Sub(start))
	}
}
$ go run main.go 
....
....
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:06:22.691501 +0100 CET m=+153.190165077, Duration: 2m33.189775511s
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:06:23.691737 +0100 CET m=+154.190447504, Duration: 2m34.190057938s
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:06:24.691824 +0100 CET m=+155.190580407, Duration: 2m35.190190841s
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:06:25.692191 +0100 CET m=+156.190990386, Duration: 2m36.19060082s
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:06:26.692307 +0100 CET m=+157.191153372, Duration: 2m37.190763806s
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:14:33.892169 +0100 CET m=+158.581754838, Duration: 2m38.581365272s
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:14:34.893832 +0100 CET m=+159.583462000, Duration: 2m39.583072434s
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:14:35.894375 +0100 CET m=+160.584051261, Duration: 2m40.583661695s
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:14:36.895479 +0100 CET m=+161.585199077, Duration: 2m41.584809511s
Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:14:37.895651 +0100 CET m=+162.585417039, Duration: 2m42.585027473s

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:

Start: 2017-12-19 14:03:49.50865 +0100 CET m=+0.000389566, End: 2017-12-19 14:14:37.895651 +0100 CET m=+162.585417039, Duration: 2m42.585027473s

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions