github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/ztime/time_test.go (about) 1 package ztime 2 3 import ( 4 "sync" 5 "testing" 6 "time" 7 8 "github.com/sohaha/zlsgo" 9 "github.com/sohaha/zlsgo/zstring" 10 ) 11 12 func TestNewTime(t *testing.T) { 13 tt := zlsgo.NewTest(t) 14 now := int64(1580809099) 15 nowTime := time.Unix(now, 0) 16 17 tt.Equal(time.Local, GetTimeZone()) 18 19 SetTimeZone(0) 20 21 tt.EqualExit("2020-02-04 09:38:19", FormatTimestamp(now, "Y-m-d H:i:s")) 22 tt.EqualExit("2020-02-04 09:38:19", FormatTimestamp(now)) 23 tt.EqualExit("20/02/ 04", FormatTimestamp(now, "y/m/ d")) 24 25 tt.EqualExit("2020-02-04 10:38:19", New(1).FormatTimestamp(now, "Y-m-d H:i:s")) 26 tt.EqualExit(New(24).Week(nowTime), Week(nowTime)+1) 27 tt.EqualExit("2020-02-04T09:38:19+00:00", FormatTimestamp(now, "c")) 28 tt.EqualExit("2023-03-01T11:30:00+00:00", FormatTimestamp(1677670200000, "c")) 29 30 SetTimeZone(8) 31 32 t.Log(GetTimeZone().String()) 33 34 currentDate := "2020-02-04 17:38:19" 35 tt.Equal(New(8).FormatTimestamp(now, "Y-m-d H:i:s"), FormatTimestamp(now, "Y-m-d H:i:s")) 36 37 for i, v := range []int{2, 3, 4, 5, 6, 0, 1} { 38 tt.Equal(v, Week(nowTime.Add((time.Hour*24)*time.Duration(i)))) 39 } 40 tt.Equal(2, Week(nowTime)) 41 tt.Equal(currentDate, FormatTimestamp(now, "Y-m-d H:i:s")) 42 tt.Equal(currentDate, FormatTime(nowTime)) 43 tt.Equal(currentDate, FormatTimestamp(now)) 44 45 beginTime, endTime, err := MonthRange(2020, 10) 46 tt.EqualNil(err) 47 tt.Equal(int64(1601481600), beginTime) 48 tt.Equal(int64(1604159999), endTime) 49 50 _, _, err = MonthRange(0, 0) 51 tt.EqualNil(err) 52 53 _, _, err = MonthRange(0, 30) 54 tt.Equal(true, err != nil) 55 56 SetTimeZone(0) 57 t.Log(GetTimeZone().String()) 58 59 t.Log(Now()) 60 t.Log(Time()) 61 t.Log(FormatTime(time.Now(), "Y-m-d H:i:s.u")) 62 } 63 64 func TestFormatTlp(tt *testing.T) { 65 t := zlsgo.NewTest(tt) 66 t.Equal("06-01-d", FormatTlp("y-m-\\d")) 67 t.Equal("地球时间:2006y01-d", FormatTlp("地球时间:Y\\ym-\\d")) 68 t.Equal("06-01-02 00:00:00", FormatTlp("y-m-d \\0\\0:\\0\\0:\\0\\0")) 69 } 70 71 func TestUnix(t *testing.T) { 72 now := time.Now() 73 t.Log(Unix(now.Unix())) 74 t.Log(UnixMicro(now.Unix())) 75 t.Log(In(now)) 76 77 t0 := New(0) 78 t.Log(t0.Unix(now.Unix())) 79 t.Log(t0.UnixMicro(now.Unix())) 80 t.Log(t0.In(now)) 81 82 t1 := New(1) 83 t.Log(t1.Unix(now.Unix())) 84 t.Log(t1.UnixMicro(now.Unix())) 85 t.Log(t1.In(now)) 86 87 ti := New(2) 88 t.Log(ti.Unix(1648879934)) 89 90 t.Log(ti.Unix(1648879934)) 91 } 92 93 func TestParse(t *testing.T) { 94 tt := zlsgo.NewTest(t) 95 date, err := Parse("2020-02-04 09:38:19") 96 tt.EqualNil(err) 97 t.Log(date) 98 99 date, err = Parse("2020/2/4 09:38:19") 100 tt.EqualNil(err) 101 t.Log(date, err) 102 103 date, err = Parse("2020.02.04", "Y.m.d") 104 tt.EqualNil(err) 105 t.Log(date, err) 106 107 date, err = Parse("地球时间:2020y02m04 11:11:11", "地球时间:Y\\ym\\md h:i:s") 108 tt.EqualNil(err) 109 t.Log(date, err) 110 111 date, err = Parse("2020.2.4 33") 112 tt.Equal(true, err != nil) 113 t.Log(date, err) 114 115 s := Now("Y-m-d H") 116 date, _ = New(2).Parse(s, "Y-m-d H") 117 t.Log( 118 s, 119 FormatTime(date, "Y-m-d H"), 120 New(24).FormatTime(date, "Y-m-d H"), 121 New(0).FormatTime(date, "Y-m-d H"), 122 ) 123 } 124 125 func TestClock(t *testing.T) { 126 tt := zlsgo.NewTest(t) 127 var wg sync.WaitGroup 128 for i := 0; i < 100; i++ { 129 wg.Add(1) 130 go func() { 131 defer wg.Done() 132 randI := zstring.RandInt(1, 500) 133 time.Sleep(time.Duration(randI) * time.Millisecond) 134 d := ((time.Now().UnixNano()) - (Clock() * 1000)) / 1000 135 b := d <= int64(time.Millisecond) 136 if !b { 137 tt.Log(b, d, randI) 138 } 139 tt.EqualTrue(b) 140 }() 141 } 142 143 wg.Wait() 144 }