Skip to content

golang time.AddDate operation incorrect #65397

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
yanmxa opened this issue Jan 31, 2024 · 2 comments
Closed

golang time.AddDate operation incorrect #65397

yanmxa opened this issue Jan 31, 2024 · 2 comments

Comments

@yanmxa
Copy link

yanmxa commented Jan 31, 2024

Go version

go version go1.21.6 darwin/arm64

Output of go env in your module/workspace:

go env                              
GO111MODULE='on'
GOARCH='arm64'
GOBIN='/usr/local/go/bin'
GOCACHE='/Users/yanmeng/Library/Caches/go-build'
GOENV='/Users/yanmeng/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/yanmeng/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/yanmeng/go'
GOPRIVATE=''
GOPROXY='https://goproxy.cn'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.6'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/yanmeng/Workspace/redhat/hub-of-hubs/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/4t/fhhxz9q56vd0mk9t7ytjpvbm0000gn/T/go-build2116447077=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I want to obtain the current time for the previous month and the month before the previous month. I ran the following code:

package main

import (
	"fmt"
	"time"
)

func main() {
	current := time.Now()
	date1 := current.AddDate(0, -1, 0)
	date2 := current.AddDate(0, -2, 0)

	fmt.Println("date", current)
	fmt.Println("date1", date1)
	fmt.Println("date2", date2)
}

What did you see happen?

It echoes the following result

 $ go run time/main.go                                                                                                                                       [16:05:27]
date 2024-01-31 16:05:38.159025 +0800 CST m=+0.000179293
date1 2023-12-31 16:05:38.159025 +0800 CST
date2 2023-12-01 16:05:38.159025 +0800 CST

What did you expect to see?

 $ go run time/main.go                                                                                                                                       [16:05:27]
date 2024-01-31 16:05:38.159025 +0800 CST m=+0.000179293
date1 2023-12-31 16:05:38.159025 +0800 CST
date2 2023-11-31 16:05:38.159025 +0800 CST
@yanmxa
Copy link
Author

yanmxa commented Jan 31, 2024

Close it cause there is no 31th for some months

@yanmxa
Copy link
Author

yanmxa commented Jan 31, 2024

Ref:#31145

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

No branches or pull requests

2 participants