Skip to content

time: In() does not replace the time zone correctly #73480

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

Closed
feketegy opened this issue Apr 23, 2025 · 3 comments
Closed

time: In() does not replace the time zone correctly #73480

feketegy opened this issue Apr 23, 2025 · 3 comments
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@feketegy
Copy link

Go version

go version go1.24.0 linux/arm64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN='/go/bin'
GOCACHE='/root/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2315358320=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/srv/ca-if-api/go.mod'
GOMODCACHE='/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/root/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

The time.In() function documentation states:

In returns a copy of t representing the same time instant, but with the copy's location information set to loc for display purposes.

But it changes the time as well, essentially converting the time to the time zone offset.

Example: https://go.dev/play/p/-s0CkU9VSC7

What did you see happen?

  1. In the example below, t1 shows the time in UTC that is used as the base time.
  2. Creating a fixed zone with time.FixedZone that offsets it -8 hours from UTC (in seconds).
  3. Setting t2 time zone with the time.In() function.
  4. t2 should show 2025-04-01 02:15:24 -0800 GMT -8:00 but the time gets offset by -8 hours, showing 2025-03-31 18:15:24 -0800 GMT-8.

Is this the expected behavior?

Example: https://go.dev/play/p/-s0CkU9VSC7

package main

import (
	"fmt"
	"time"
)

func main() {
	t1, _ := time.Parse(time.RFC3339, "2025-04-01T02:15:24Z")
	fmt.Printf("\nOriginal time in UTC: %s", t1)

	fz := time.FixedZone("GMT-8", -8*60*60)

	t2 := t1.In(fz)
	fmt.Printf("\n\nChanged time zone: %s", t2)
}

What did you expect to see?

If t1 in the example is 2025-04-01 02:15:24 UTC, then t2 should also show 2025-04-01 02:15:24 -0800 GMT -8:00, switching the time zone only, but not the date/time.

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Apr 23, 2025
@JunyangShao
Copy link
Contributor

JunyangShao commented Apr 23, 2025

As @seankhliao mentioned, this is working as intended, thanks for asking the question. 👍

@JunyangShao JunyangShao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 23, 2025
@seankhliao
Copy link
Member

seankhliao commented Apr 23, 2025

Working as intended.
It represents the same absolute instant in time, not the same civil time.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 23, 2025
@JunyangShao JunyangShao removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

4 participants