45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package timex
|
|
|
|
import "testing"
|
|
|
|
func TestGetYearMonthToDay(t *testing.T) {
|
|
cases := []struct {
|
|
year, month, want int
|
|
}{
|
|
{2024, 2, 29}, // 闰年
|
|
{2023, 2, 28}, // 平年
|
|
{2000, 2, 29}, // 400 整除
|
|
{1900, 2, 28}, // 100 整除非 400
|
|
{2026, 1, 31},
|
|
{2026, 4, 30},
|
|
}
|
|
for _, c := range cases {
|
|
if got := GetYearMonthToDay(c.year, c.month); got != c.want {
|
|
t.Errorf("GetYearMonthToDay(%d,%d) = %d, want %d", c.year, c.month, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNtUtcConvert(t *testing.T) {
|
|
nt := UtcToNt(1000000000)
|
|
if got := NtToUtc(nt); got != 1000000000 {
|
|
t.Errorf("NT/UTC 往返 = %d, want 1000000000", got)
|
|
}
|
|
}
|
|
|
|
func TestTimeStrToTimestamp(t *testing.T) {
|
|
ts, err := TimeStrToTimestamp("2026-08-29 12:00:00")
|
|
if err != nil {
|
|
t.Fatalf("TimeStrToTimestamp: %v", err)
|
|
}
|
|
if ts <= 0 {
|
|
t.Errorf("时间戳应为正数: %d", ts)
|
|
}
|
|
if ts%1000 != 0 {
|
|
t.Errorf("毫秒时间戳应为整秒: %d", ts)
|
|
}
|
|
if _, err := TimeStrToTimestamp("bad"); err == nil {
|
|
t.Error("非法格式应返回错误")
|
|
}
|
|
}
|