package timeutils import ( "testing" "time" ) var testDateTimeStr = "2024-02-27 13:49:50" var testDateStr = "2024-02-27" func TestParseLocalTime(t *testing.T) { timeParsed := ParseLocalDateTime(testDateTimeStr) if testDateTimeStr != timeParsed.Format(time.DateTime) { t.Fatal("时间解析失败") } } func TestParseLocalDate(t *testing.T) { timeParsed := ParseLocalDateOnly(testDateStr) if testDateStr != timeParsed.Format(time.DateOnly) { t.Fatal("时间解析失败") } } func TestParseNowDateOnly(t *testing.T) { now := time.Now().Local() if now.Format(time.DateOnly) != ParseNowDateOnly().Format(time.DateOnly) { t.Fatal("时间解析错误") } } func TestFormatNowDateTime(t *testing.T) { now := time.Now().Local() if now.Format(time.DateTime) != FormatNowDateTime() { t.Fatal("时间解析错误") } } func TestFormatLocalDateTime(t *testing.T) { now := time.Now().Local() if now.Format(time.DateTime) != FormatLocalDateTime(&now) { t.Fatal("时间解析错误") } } func TestFormatNowDate(t *testing.T) { now := time.Now().Local() if now.Format(time.DateOnly) != FormatNowDateOnly() { t.Fatal("时间解析错误") } } func TestFormatLocalDate(t *testing.T) { now := time.Now().Local() if now.Format(time.DateOnly) != FormatLocalDateOnly(&now) { t.Fatal("时间解析错误") } } func TestGetYear(t *testing.T) { now := time.Now().Local() if now.Year() != GetYear(&now) { t.Fatal("时间解析错误") } } func TestGetWeek(t *testing.T) { now := time.Now().Local() _, week := now.ISOWeek() if week != GetWeek(&now) { t.Fatal("时间解析错误") } } func TestDiffNatureDays(t *testing.T) { now := time.Now().Local() afterOneDay := now.Add(24 * time.Hour) if DiffNatureDays(now.Format(time.DateTime), afterOneDay.Format(time.DateTime)) != 1 { t.Fatal("时间解析错误") } } func TestGetChinaMonth(t *testing.T) { timeParsed := ParseLocalDateTime(testDateTimeStr) if GetChinaMonth(timeParsed) != "二月" { t.Fatal("时间解析错误") } } func TestGetChinaWeek(t *testing.T) { timeParsed := ParseLocalDateTime(testDateTimeStr) if GetChinaWeek(timeParsed) != "星期二" { t.Fatal("时间解析错误") } }