Skip to content

time: Round does not work with 7ms. #70449

@KyleSanderson

Description

@KyleSanderson

Go version

go1.23.2 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Carlos\AppData\Local\go-build
set GOENV=C:\Users\Carlos\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Carlos\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Carlos\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.23.2
set GODEBUG=
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\Carlos\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\Carlos\AppData\Local\Temp\go-build825637060=/tmp/go-build -gno-record-gcc-switches

What did you do?

https://go.dev/play/p/Mvbh3SQwa3b

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"time"
)

func main() {
	const rounds = 700

	for k := 1; k < 37; k++ {
		unique := 0
		old := time.Now().Round(time.Millisecond * time.Duration(k)).UnixMilli()
		for i := 0; i < rounds; i++ {
			new := time.Now().Round(time.Millisecond * time.Duration(k)).UnixMilli()
			if new > old {
				unique++
				old = new
			}

			if div := new % int64(k); div != 0 {
				fmt.Printf("%d is broken. not a multiple of %d\n", k, div)
				break
			}
			time.Sleep(time.Millisecond * 1)
		}

		if unique < rounds/k-1 {
			fmt.Printf("%d is broken. failed round count %d\n", k, unique)
			continue
		}
	}
}

What did you see happen?

7 is broken. not a multiple of 4
7 is broken. failed round count 0
11 is broken. not a multiple of 2
11 is broken. failed round count 0
13 is broken. not a multiple of 4
13 is broken. failed round count 0
14 is broken. not a multiple of 4
14 is broken. failed round count 0
17 is broken. not a multiple of 11
17 is broken. failed round count 0
19 is broken. not a multiple of 18
19 is broken. failed round count 0
21 is broken. not a multiple of 18
21 is broken. failed round count 0
22 is broken. not a multiple of 2
22 is broken. failed round count 0
23 is broken. not a multiple of 11
23 is broken. failed round count 0
26 is broken. not a multiple of 4
26 is broken. failed round count 0
28 is broken. not a multiple of 4
28 is broken. failed round count 0
29 is broken. not a multiple of 3
29 is broken. failed round count 0
31 is broken. not a multiple of 13
31 is broken. failed round count 0
33 is broken. not a multiple of 24
33 is broken. failed round count 0
34 is broken. not a multiple of 28
34 is broken. failed round count 0
35 is broken. not a multiple of 25
35 is broken. failed round count 0

What did you expect to see?

.

Something's not right with the rounding code. 5 and 10 work, but once you start to get up there things fall apart fast.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.OS-Windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions