Skip to content

affected/package: time #63843

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
shaoqingyang opened this issue Oct 31, 2023 · 3 comments
Closed

affected/package: time #63843

shaoqingyang opened this issue Oct 31, 2023 · 3 comments

Comments

@shaoqingyang
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.18.10 darwin/arm64

Does this issue reproduce with the latest release?

Indeed, it is.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/shaoqingyang/Library/Caches/go-build"
GOENV="/Users/shaoqingyang/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/shaoqingyang/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/shaoqingyang/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18.10"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/l3/w03l6vys18x_nkn5nsmgpzn00000gn/T/go-build1210182971=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I need to calculate the date one month ago. If today is October 31st, the time calculated using the AddDate() method is October 1st, and the underlying code does not take into account the different days of each month.
I provide some code and hope to receive your modifications.

func GetMonthToday(t time.Time, month int) time.Time {
	if t.Day() <= 28 {
		return t.AddDate(0, -month, 0)
	}
	monthDay := [13]int{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
	target := t.AddDate(0, 0, 1-t.Day()).AddDate(0, -month, 0)
	targetDay := monthDay[target.Month()]
	if target.Month() == time.February && (target.Year()%400 == 0 || (target.Year()%100 != 0 && target.Year()%4 == 0)) {
		targetDay++
	}
	if t.Day() > targetDay {
		return target.AddDate(0, 0, targetDay-1)
	}
	return target.AddDate(0, 0, t.Day()-1)
}

What did you expect to see?

I hope that future versions can consider my proposed issue.

What did you see instead?

The mistake of AddDate()

@mauri870
Copy link
Member

I'm not sure if I follow along with the problem you are reporting, it seems that AddDate is working as intended. Can you clarify what you think the bug is and a clear way to reproduce it? Thanks

@mauri870 mauri870 added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 31, 2023
@doggedOwl
Copy link

doggedOwl commented Oct 31, 2023

@shaoqingyang maybe what is confusing you is the normalization of the date.
addDate(0, -1 , 0) applied for exampe to today (31/10/2023) will correctly produce 01/10/2023 because the calculated date of 1 month ago is 31/09/2023. Since september can not have 31 days the date gets normalised to 01 of October.

@mauri870
Copy link
Member

Normalization is explicitly stated in the docs for AddDate.

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

@mauri870 mauri870 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 31, 2023
@mauri870 mauri870 removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 31, 2023
@golang golang locked and limited conversation to collaborators Nov 6, 2024
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

4 participants