dromara / carbon

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

AddQuarters(-1) incorrect result #237

Closed jiekun closed 6 months ago

jiekun commented 6 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 6 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 6 months ago

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

jiekun commented 6 months ago

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