feat(timex): 自 go-hua/common 迁入时间日期工具并独立成包
- NT/UTC 时间戳互转、时间字符串转毫秒时间戳、指定年月天数 - 附单元测试、examples/timex 例程与包 README
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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("非法格式应返回错误")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user