Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
131 lines
2.7 KiB
Go
131 lines
2.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_GetDayRangeByDate(t *testing.T) {
|
|
begin, end, _ := GetDayRangeByDate("2022-04-08")
|
|
fmt.Println(begin, end)
|
|
}
|
|
|
|
func Test_GetDayRange(t *testing.T) {
|
|
now := time.Now()
|
|
fmt.Println(GetDayRange(now))
|
|
}
|
|
|
|
func Test_GetDateString(t *testing.T) {
|
|
fmt.Println(GetDateString(time.Now()))
|
|
}
|
|
|
|
func Test_GetTodayDate(t *testing.T) {
|
|
fmt.Println(GetTodayDate(time.Now()))
|
|
}
|
|
|
|
func Test_GetYesterdayDate(t *testing.T) {
|
|
fmt.Println(GetYesterdayDate(time.Now()))
|
|
}
|
|
|
|
func Test_GetDayBeforeYesterdayDate(t *testing.T) {
|
|
fmt.Println(GetDayBeforeYesterdayDate(time.Now()))
|
|
}
|
|
|
|
func Test_BeginOfWeekByTime(t *testing.T) {
|
|
{
|
|
// 如果当前是周日,获取到的结果应该是这周周一而不是下周周一
|
|
now := time.Date(2022, 4, 3, 1, 0, 0, 0, time.Local)
|
|
actual := BeginOfWeekByTime(now)
|
|
expect := time.Date(2022, 3, 28, 0, 0, 0, 0, time.Local)
|
|
if !reflect.DeepEqual(expect, actual) {
|
|
t.Fatalf("fail")
|
|
}
|
|
}
|
|
{
|
|
now := time.Date(2022, 3, 28, 0, 0, 0, 0, time.Local)
|
|
actual := BeginOfWeekByTime(now)
|
|
expect := time.Date(2022, 3, 28, 0, 0, 0, 0, time.Local)
|
|
if !reflect.DeepEqual(expect, actual) {
|
|
t.Fatalf("fail")
|
|
}
|
|
}
|
|
}
|
|
|
|
func Test_GetDayEnd(t *testing.T) {
|
|
fmt.Println(GetDayEnd("2020-04-09"))
|
|
}
|
|
|
|
func Test_GetDayBegin(t *testing.T) {
|
|
fmt.Println(GetDayBegin("2020-04-09"))
|
|
}
|
|
|
|
func Test_EndOfWeekByTime(t *testing.T) {
|
|
now := time.Now()
|
|
fmt.Println(EndOfWeekByTime(now))
|
|
}
|
|
|
|
func Test_EndOfMonth(t *testing.T) {
|
|
now := time.Now()
|
|
fmt.Println(BeginOfMonth(now))
|
|
fmt.Println(EndOfMonth(now))
|
|
}
|
|
|
|
func Test_WeekDateRange(t *testing.T) {
|
|
fmt.Println(WeekDateRange(time.Now()))
|
|
}
|
|
|
|
func Test_MonthDateRange(t *testing.T) {
|
|
fmt.Println(MonthDateRange(time.Now()))
|
|
}
|
|
|
|
func Test_EndOfLastWeek(t *testing.T) {
|
|
fmt.Println(EndOfLastWeek(time.Now()))
|
|
}
|
|
|
|
func Test_EndOfLastMonth(t *testing.T) {
|
|
fmt.Println(EndOfLastMonth(time.Now()))
|
|
}
|
|
|
|
func Test_EndOfLastWeekDate(t *testing.T) {
|
|
fmt.Println(EndOfLastWeekDate(time.Now()))
|
|
}
|
|
|
|
func Test_EndOfLastMonthDate(t *testing.T) {
|
|
fmt.Println(EndOfLastMonthDate(time.Now()))
|
|
}
|
|
|
|
func TestMustParseTime(t *testing.T) {
|
|
type args struct {
|
|
s string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want time.Time
|
|
}{
|
|
{
|
|
name: "case1",
|
|
args: args{
|
|
s: "2022-04-25",
|
|
},
|
|
want: time.Date(2022, 4, 25, 0, 0, 0, 0, time.Local),
|
|
},
|
|
{
|
|
name: "case2",
|
|
args: args{
|
|
s: "2022-04-25 11:12:13",
|
|
},
|
|
want: time.Date(2022, 4, 25, 11, 12, 13, 0, time.Local),
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := MustParseTime(tt.args.s); !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("MustParseTime() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|