Files
2026-10-26 16:28:14 +08:00

170 lines
4.2 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_BeginOfYear(t *testing.T) {
got := BeginOfYear(time.Date(2026, 9, 12, 15, 4, 5, 0, LocBJ))
want := time.Date(2026, 1, 1, 0, 0, 0, 0, LocBJ)
if !got.Equal(want) {
t.Fatalf("BeginOfYear = %v, want %v", got, want)
}
// UTC 2025-12-31 16:30 已是北京时间 2026-01-01 00:30,应按 2026 年切。
utc := time.Date(2025, 12, 31, 16, 30, 0, 0, time.UTC)
if got = BeginOfYear(utc); !got.Equal(want) {
t.Fatalf("BeginOfYear(UTC year boundary) = %v, want %v", got, want)
}
}
func Test_BeginOfMonthBeijing(t *testing.T) {
// UTC 2026-08-31 16:30 已是北京时间 2026-09-01 00:30,应按 9 月切。
utc := time.Date(2026, 8, 31, 16, 30, 0, 0, time.UTC)
want := time.Date(2026, 9, 1, 0, 0, 0, 0, LocBJ)
if got := BeginOfMonth(utc); !got.Equal(want) {
t.Fatalf("BeginOfMonth(UTC month boundary) = %v, want %v", got, want)
}
}
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")
}
}
{
// UTC 2026-09-06 16:30 已是北京时间周一 00:30,应按本周一切。
utc := time.Date(2026, 9, 6, 16, 30, 0, 0, time.UTC)
want := time.Date(2026, 9, 7, 0, 0, 0, 0, LocBJ)
if got := BeginOfWeekByTime(utc); !got.Equal(want) {
t.Fatalf("BeginOfWeekByTime(UTC week boundary) = %v, want %v", got, want)
}
}
{
// UTC 2026-09-06 15:30 仍是北京时间周日 23:30,应落在上一周周一。
utc := time.Date(2026, 9, 6, 15, 30, 0, 0, time.UTC)
want := time.Date(2026, 8, 31, 0, 0, 0, 0, LocBJ)
if got := BeginOfWeekByTime(utc); !got.Equal(want) {
t.Fatalf("BeginOfWeekByTime(UTC sunday) = %v, want %v", got, want)
}
}
}
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)
}
})
}
}