github.heygears.com/openimsdk/tools@v0.0.49/utils/timeutil/time_format_test.go (about) 1 // Copyright © 2023 OpenIM. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package timeutil 16 17 import ( 18 "testing" 19 "time" 20 ) 21 22 func TestGetCurrentTimestampBySecond(t *testing.T) { 23 now := time.Now().Unix() 24 got := GetCurrentTimestampBySecond() 25 if got < now { 26 t.Errorf("GetCurrentTimestampBySecond() = %v, want at least %v", got, now) 27 } 28 } 29 30 func TestUnixSecondToTime(t *testing.T) { 31 now := time.Now().Unix() 32 got := UnixSecondToTime(now) 33 if got.Unix() != now { 34 t.Errorf("UnixSecondToTime(%v) = %v, want %v", now, got.Unix(), now) 35 } 36 } 37 38 func TestUnixNanoSecondToTime(t *testing.T) { 39 now := time.Now().UnixNano() 40 got := UnixNanoSecondToTime(now) 41 if got.UnixNano() != now { 42 t.Errorf("UnixNanoSecondToTime(%v) = %v, want %v", now, got.UnixNano(), now) 43 } 44 } 45 46 func TestUnixMillSecondToTime(t *testing.T) { 47 now := time.Now().UnixNano() / 1e6 48 got := UnixMillSecondToTime(now) 49 if got.UnixNano()/1e6 != now { 50 t.Errorf("UnixMillSecondToTime(%v) = %v, want %v", now, got.UnixNano()/1e6, now) 51 } 52 } 53 54 func TestGetCurrentTimestampByNano(t *testing.T) { 55 now := time.Now().UnixNano() 56 got := GetCurrentTimestampByNano() 57 if got < now { 58 t.Errorf("GetCurrentTimestampByNano() = %v, want at least %v", got, now) 59 } 60 } 61 62 func TestGetCurrentTimestampByMill(t *testing.T) { 63 now := time.Now().UnixNano() / 1e6 64 got := GetCurrentTimestampByMill() 65 if got < now { 66 t.Errorf("GetCurrentTimestampByMill() = %v, want at least %v", got, now) 67 } 68 } 69 70 func TestGetCurDayHalfTimestamp(t *testing.T) { 71 expected := GetCurDayZeroTimestamp() + HalfOffset 72 got := GetCurDayHalfTimestamp() 73 if got != expected { 74 t.Errorf("GetCurDayHalfTimestamp() = %v, want %v", got, expected) 75 } 76 } 77 78 func TestGetCurDayZeroTimeFormat(t *testing.T) { 79 // This test may need adjustment based on the local time zone of the testing environment. 80 expected := time.Unix(GetCurDayZeroTimestamp(), 0).Format("2006-01-02_15-04-05") 81 got := GetCurDayZeroTimeFormat() 82 if got != expected { 83 t.Errorf("GetCurDayZeroTimeFormat() = %v, want %v", got, expected) 84 } 85 } 86 87 func TestGetCurDayHalfTimeFormat(t *testing.T) { 88 // This test may need adjustment based on the local time zone of the testing environment. 89 expected := time.Unix(GetCurDayZeroTimestamp()+HalfOffset, 0).Format("2006-01-02_15-04-05") 90 got := GetCurDayHalfTimeFormat() 91 if got != expected { 92 t.Errorf("GetCurDayHalfTimeFormat() = %v, want %v", got, expected) 93 } 94 }