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": same string becomes unequal and after time.Format and same index returned different "rune" #69832

Closed
yusing opened this issue Oct 9, 2024 · 3 comments

Comments

@yusing
Copy link

yusing commented Oct 9, 2024

Go version

go 1.23.2 linux/amd64

Output of `go env`
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/yusing/.cache/go-build'
GOENV='/home/yusing/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/yusing/.local/share/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/yusing/.local/share/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/yusing/sdk/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/yusing/sdk/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/yusing/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/path/to/test/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3518165204=/tmp/go-build -gno-record-gcc-switches'

What did you do?

import (
	"testing"
	timePkg "time"
)

func TestDatetime(t *testing.T) {
	const dateFmt = "2006-1-02 15:04:05 -0700 MST"
	const expect = "2024-08-22 21:11:11 +0800 HKT"

	time, err := timePkg.Parse(dateFmt, expect)
	if err != nil {
		t.Fatal(err)
	}
	got := time.Format(dateFmt)
	if got != expect {
		t.Logf("date not match: %q != %q", time, expect)
		for i := range expect {
			if got[i] != expect[i] {
				t.Fatalf("date not match: %q != %q at index %d", rune(got[i]), rune(expect[i]), i)
			}
		}
	}
}

What did you see happen?

--- FAIL: TestDatetime (0.00s)
    /path/to/test/test.go:18: date not match: "2024-08-22 21:11:11 +0800 HKT" != "2024-08-22 21:11:11 +0800 HKT"
    /path/to/test/test.go:21: date not match: '8' != '0' at index 5
FAIL
FAIL	test	0.003s
FAIL

What did you expect to see?

Test pass

@fzipp
Copy link
Contributor

fzipp commented Oct 9, 2024

This

t.Logf("date not match: %q != %q", time, expect)

should be

t.Logf("date not match: %q != %q", got, expect)

if you want to see what is actually being compared.

Then you will see that the month should have a leading 0 in the format string for the test to pass:

const dateFmt = "2006-01-02 15:04:05 -0700 MST"

@yusing
Copy link
Author

yusing commented Oct 9, 2024

ahhhh me being so dumb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants