2021-02-18 14:32:31 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-07-19 09:33:57 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-18 14:32:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCarbon_DiffInYears(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-12-31 13:14:15"),
|
|
|
|
carbon2: Parse("2021-01-01 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2021-08-28 13:14:59"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2018-08-28 13:14:59"),
|
|
|
|
want: -1,
|
|
|
|
},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffInYears(tt.carbon2), "DiffInYears()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:52:02 +08:00
|
|
|
func TestCarbon_DiffAbsInYears(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-12-31 13:14:15"),
|
|
|
|
carbon2: Parse("2021-01-01 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2021-08-28 13:14:59"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2018-08-28 13:14:59"),
|
|
|
|
want: 1,
|
|
|
|
},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffAbsInYears(tt.carbon2), "DiffAbsInYears()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_DiffInMonths(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-07-28 13:14:00"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-09-06 13:14:59"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2018-08-28 13:14:59"),
|
|
|
|
want: -24,
|
|
|
|
},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffInMonths(tt.carbon2), "DiffInMonths()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:52:02 +08:00
|
|
|
func TestCarbon_DiffAbsInMonths(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-07-28 13:14:00"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-09-06 13:14:59"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2018-08-28 13:14:59"),
|
|
|
|
want: 24,
|
|
|
|
},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffAbsInMonths(tt.carbon2), "DiffAbsInMonths()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_DiffInWeeks(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-07-28 13:14:00"),
|
|
|
|
want: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-12 13:14:15"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffInWeeks(tt.carbon2), "DiffInWeeks()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:52:02 +08:00
|
|
|
func TestCarbon_DiffAbsInWeeks(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-07-28 13:14:00"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-12 13:14:15"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffAbsInWeeks(tt.carbon2), "DiffAbsInWeeks()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_DiffInDays(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-04 13:14:59"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-06 13:14:15"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-04 13:00:00"),
|
|
|
|
want: -1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffInDays(tt.carbon2), "DiffInDays()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:52:02 +08:00
|
|
|
func TestCarbon_DiffAbsInDays(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-04 13:14:59"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-06 13:14:15"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-04 13:00:00"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffAbsInDays(tt.carbon2), "DiffAbsInDays()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_DiffInHours(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 12:14:00"),
|
|
|
|
want: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 14:14:15"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffInHours(tt.carbon2), "DiffInHours()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:52:02 +08:00
|
|
|
func TestCarbon_DiffAbsInHours(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 12:14:00"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 14:14:15"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffAbsInHours(tt.carbon2), "DiffAbsInHours()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_DiffInMinutes(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:13:00"),
|
|
|
|
want: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:15:15"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffInMinutes(tt.carbon2), "DiffInMinutes()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:52:02 +08:00
|
|
|
func TestCarbon_DiffAbsInMinutes(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:13:00"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:15:15"),
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffAbsInMinutes(tt.carbon2), "DiffAbsInMinutes()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_DiffInSeconds(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:20"),
|
|
|
|
want: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:20"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: -5,
|
|
|
|
},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffInSeconds(tt.carbon2), "DiffInSeconds()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:52:02 +08:00
|
|
|
func TestCarbon_DiffAbsInSeconds(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want int64
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:20"),
|
|
|
|
want: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:20"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: 5,
|
|
|
|
},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffAbsInSeconds(tt.carbon2), "DiffAbsInSeconds()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:07:45 +08:00
|
|
|
func TestCarbon_DiffInString(t *testing.T) {
|
2024-02-20 11:08:21 +08:00
|
|
|
now := Now()
|
2021-09-06 11:07:45 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want string
|
2021-09-06 11:07:45 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("xxx"),
|
|
|
|
carbon2: Parse("xxx"),
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: "just now",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: now,
|
|
|
|
carbon2: now,
|
|
|
|
want: "just now",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: now.AddYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "-1 year",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case6",
|
|
|
|
carbon1: now.SubYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 year",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case7",
|
|
|
|
carbon1: now.AddMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "-1 month",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case8",
|
|
|
|
carbon1: now.SubMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 month",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case9",
|
|
|
|
carbon1: now.AddDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "-1 day",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case10",
|
|
|
|
carbon1: now.SubDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 day",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case11",
|
|
|
|
carbon1: now.AddHours(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "-1 hour",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case12",
|
|
|
|
carbon1: now.SubHours(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 hour",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case13",
|
|
|
|
carbon1: now.AddHours(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "-1 hour",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case14",
|
|
|
|
carbon1: now.SubMinutes(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 minute",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case15",
|
|
|
|
carbon1: now.AddMinutes(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "-1 minute",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case16",
|
|
|
|
carbon1: now.SubMinutes(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 minute",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "case17",
|
|
|
|
carbon1: now.AddSeconds(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "-1 second",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case18",
|
|
|
|
carbon1: now.SubSeconds(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 second",
|
|
|
|
},
|
2021-09-06 11:07:45 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffInString(tt.carbon2), "DiffInString()")
|
|
|
|
})
|
2021-09-06 11:07:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:52:02 +08:00
|
|
|
func TestCarbon_DiffAbsInString(t *testing.T) {
|
2024-02-20 11:08:21 +08:00
|
|
|
now := Now()
|
2021-09-06 11:07:45 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want string
|
2021-09-06 11:07:45 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("xxx"),
|
|
|
|
carbon2: Parse("xxx"),
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: Parse("2020-08-05 13:14:15"),
|
|
|
|
carbon2: Parse("2020-08-05 13:14:15"),
|
|
|
|
want: "just now",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: now,
|
|
|
|
carbon2: now,
|
|
|
|
want: "just now",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: now.AddYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 year",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case6",
|
|
|
|
carbon1: now.SubYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 year",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case7",
|
|
|
|
carbon1: now.AddMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 month",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case8",
|
|
|
|
carbon1: now.SubMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 month",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case9",
|
|
|
|
carbon1: now.AddDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 day",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case10",
|
|
|
|
carbon1: now.SubDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 day",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case11",
|
|
|
|
carbon1: now.AddHours(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 hour",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case12",
|
|
|
|
carbon1: now.SubHours(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 hour",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case13",
|
|
|
|
carbon1: now.AddHours(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 hour",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case14",
|
|
|
|
carbon1: now.SubMinutes(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 minute",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case15",
|
|
|
|
carbon1: now.AddMinutes(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 minute",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case16",
|
|
|
|
carbon1: now.SubMinutes(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 minute",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "case17",
|
|
|
|
carbon1: now.AddSeconds(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 second",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case18",
|
|
|
|
carbon1: now.SubSeconds(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 second",
|
|
|
|
},
|
2021-09-06 11:07:45 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffAbsInString(tt.carbon2), "DiffAbsInString()")
|
|
|
|
})
|
2021-09-06 11:07:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 11:08:21 +08:00
|
|
|
func TestCarbon_DiffInDuration(t *testing.T) {
|
|
|
|
now := Now()
|
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want string
|
2024-02-20 11:08:21 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: "0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("xxx"),
|
|
|
|
carbon2: Parse("xxx"),
|
|
|
|
want: "0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: now,
|
|
|
|
carbon2: now,
|
|
|
|
want: "0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: now.AddYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "-8760h0m0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: now.SubYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "8784h0m0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case6",
|
|
|
|
carbon1: now.AddMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
2024-10-11 22:51:51 +08:00
|
|
|
want: "-744h0m0s",
|
2024-04-09 22:09:34 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case7",
|
|
|
|
carbon1: now.SubMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
2024-10-11 22:51:51 +08:00
|
|
|
want: "720h0m0s",
|
2024-04-09 22:09:34 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "case8",
|
|
|
|
carbon1: now.AddDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "-24h0m0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case9",
|
|
|
|
carbon1: now.SubDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "24h0m0s",
|
|
|
|
},
|
2024-02-20 11:08:21 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffInDuration(tt.carbon2).String(), "DiffInDuration()")
|
|
|
|
})
|
2024-02-20 11:08:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_DiffAbsInDuration(t *testing.T) {
|
|
|
|
now := Now()
|
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want string
|
2024-02-20 11:08:21 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: "0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("xxx"),
|
|
|
|
carbon2: Parse("xxx"),
|
|
|
|
want: "0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: now,
|
|
|
|
carbon2: now,
|
|
|
|
want: "0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: now.AddYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "8760h0m0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: now.SubYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "8784h0m0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case6",
|
|
|
|
carbon1: now.AddMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
2024-10-11 22:51:51 +08:00
|
|
|
want: "744h0m0s",
|
2024-04-09 22:09:34 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case7",
|
|
|
|
carbon1: now.SubMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
2024-10-11 22:51:51 +08:00
|
|
|
want: "720h0m0s",
|
2024-04-09 22:09:34 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "case8",
|
|
|
|
carbon1: now.AddDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "24h0m0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case9",
|
|
|
|
carbon1: now.SubDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "24h0m0s",
|
|
|
|
},
|
2024-02-20 11:08:21 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffAbsInDuration(tt.carbon2).String(), "DiffAbsInDuration()")
|
|
|
|
})
|
2024-02-20 11:08:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 13:27:17 +08:00
|
|
|
func TestCarbon_DiffForHumans(t *testing.T) {
|
2024-02-20 11:08:21 +08:00
|
|
|
now := Now()
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2024-04-09 22:09:34 +08:00
|
|
|
name string
|
|
|
|
carbon1 Carbon
|
|
|
|
carbon2 Carbon
|
|
|
|
want string
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2024-04-09 22:09:34 +08:00
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
carbon1: Parse(""),
|
|
|
|
carbon2: Parse(""),
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case2",
|
|
|
|
carbon1: Parse("xxx"),
|
|
|
|
carbon2: Parse("xxx"),
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case3",
|
|
|
|
carbon1: now,
|
|
|
|
carbon2: now,
|
|
|
|
want: "just now",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case4",
|
|
|
|
carbon1: now.AddYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 year after",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case5",
|
|
|
|
carbon1: now.SubYearsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 year before",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "case6",
|
|
|
|
carbon1: now.AddMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 month after",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case7",
|
|
|
|
carbon1: now.SubMonthsNoOverflow(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 month before",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "case8",
|
|
|
|
carbon1: now.AddDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 day after",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case9",
|
|
|
|
carbon1: now.SubDays(1),
|
|
|
|
carbon2: now,
|
|
|
|
want: "1 day before",
|
|
|
|
},
|
2021-07-19 09:33:57 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:09:34 +08:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, tt.carbon1.DiffForHumans(tt.carbon2), "DiffForHumans()")
|
|
|
|
})
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
2021-09-06 11:07:45 +08:00
|
|
|
|
|
|
|
}
|