Skip to content

time.AddDate() can't get Feb #31130

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
ppmoon opened this issue Mar 29, 2019 · 4 comments
Closed

time.AddDate() can't get Feb #31130

ppmoon opened this issue Mar 29, 2019 · 4 comments

Comments

@ppmoon
Copy link

ppmoon commented Mar 29, 2019

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

$ go version
1.11

Does this issue reproduce with the latest release?

I don't try 1.12

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

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\hetao\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\hetao\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\hetao\AppData\Local\Temp\go-build476396638=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
	"log"
	"time"
)


func main() {
	curTime := time.Now()
	lastMonthTime := curTime.AddDate(0,-1,0)
	log.Println("curTime:",curTime)
	log.Println("lastMonthTime:",lastMonthTime)
}

2019/03/29 16:11:39 curTime: 2019-03-29 16:11:39.1469807 +0800 CST m=+0.002000201
2019/03/29 16:11:39 lastMonthTime: 2019-03-01 16:11:39.1469807 +0800 CST

What did you expect to see?

If -1 I expect to see the time is Feb

What did you see instead?

@VirrageS
Copy link

From docs:

AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.

So if you do "03-29 - 1 Month" then you obtain 02-29 but there is no 29th of February so it is normalized to 1st of March

@ianlancetaylor
Copy link
Contributor

Closing because this is working as documented and expected.

@happilymarrieddad
Copy link

This is so ridiculous. Obviously just because something is documented and expected doesn't mean it's right. It makes no sense that adding a month to January results in March. Momentjs and Java don't work this way. I had to write a Date library in C a long time ago and this wouldn't fly then. I understand why the developers are sticking to this narrative but I think it's silly.

@ianlancetaylor
Copy link
Contributor

Please see the comment I just added to #31145 (#31145 (comment))

@golang golang locked and limited conversation to collaborators Apr 1, 2020
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

5 participants