dromara / carbon

A simple, semantic and developer-friendly golang package for time
https://pkg.go.dev/github.com/golang-module/carbon/v2
MIT License
4.77k stars 238 forks source link

AddQuarters(-1) incorrect result #237

Closed jiekun closed 5 months ago

jiekun commented 5 months ago

Hi.

func TestCarbon(t *testing.T) {
    endDate := carbon.Parse("2023-12-31 23:59:59")
    endDate = endDate.AddQuarters(-1)
    fmt.Println(endDate)
}

the printed result is 2023-10-01 23:59:59

May I ask if it's expected?

gouguoyin commented 5 months ago

AddQuarters(-1) = AddMonths(-3),in theory, 2023-12-31 23:59:59 was 2023-09-31 23:59:59 three months ago, but there were no 31 days in September

gouguoyin commented 5 months ago

If you want to get 2023-09-30 23:59:59, you can use AddQuartersNoOverflow(-1) or SubQuartersNoOverflow(1)

jiekun commented 5 months ago

Thank you so much for your hint. The explanation is clear. And I'm going to use AddQuartersNoOverflow.