Skip to content

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

Closed as not planned
Closed as not planned
@feketegy

Description

@feketegy

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions