diff --git a/README.md b/README.md index 6e4200b..4c26ba6 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,11 @@ go get github.com/kpango/fastime ## Example ```go - now := fastime.Now() + now := fastime.Now().StartTimerD(context.Background(), time.Millisecond*5) defer fastime.Stop() // Create Instance - ft := fastime.New() + ft := fastime.New().StartTimerD(context.Background(), time.Millisecond*5) defer ft.Stop() ft.Now() ``` diff --git a/example/result.txt b/example/result.txt deleted file mode 100644 index 23f028c..0000000 --- a/example/result.txt +++ /dev/null @@ -1,16 +0,0 @@ -s1=2022-02-08 22:37:21.754272 +0900 JST -s2=2022-02-08 22:37:21.754272 +0900 JST -s3=2022-02-08 22:37:21.754272 +0900 JST -s4=2022-02-08 22:37:23.755217 +0900 JST -nanonow 1644327448754453000 -now unixnano 1644327448 -now add unixnano2022-02-08 22:37:29.754453 +0900 JST -nanonow + dur 1644327449754453000 -string 2022-02-08T22:37:28+09:00 -Address of buf=fuckkkking: 0xc0001c8498 -Address of buf=: 0xc0001c8498 -Address of a1=[102 117 99 107 107 107 107 105 110 103]: 0xc000010350 -Address of b1=fuckkkking: 0xc0001c84c8 -Address of buf=helloooooo: 0xc0001c8498 -Address of a2=[102 117 99 107 107 107 107 105 110 103]: 0xc000010380 -Address of b2=fuckkkking: 0xc0001c84e0 diff --git a/fastime.go b/fastime.go index 6d4c02a..6479ce4 100644 --- a/fastime.go +++ b/fastime.go @@ -22,6 +22,7 @@ type Fastime interface { UnixNanoNow() int64 UnixUNanoNow() uint32 FormattedNow() []byte + Since(t time.Time) time.Duration StartTimerD(ctx context.Context, dur time.Duration) Fastime } @@ -167,6 +168,10 @@ func (f *fastime) Stop() { } } +func (f *fastime) Since(t time.Time) time.Duration { + return f.Now().Sub(t) +} + // UnixNow returns current unix time func (f *fastime) UnixNow() int64 { return atomic.LoadInt64(&f.ut) diff --git a/fastime_test.go b/fastime_test.go index 912fdb3..b30cc2c 100644 --- a/fastime_test.go +++ b/fastime_test.go @@ -410,16 +410,17 @@ func TestFastime_Since(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - now := Now() + f := New().StartTimerD(context.Background(), time.Millisecond*5) + now := f.Now() timeNow := time.Now() - time.Sleep(100 * time.Millisecond) - since1 := Since(now) + time.Sleep(time.Second) + since1 := f.Since(now) since2 := time.Since(timeNow) if since1 < 50*time.Millisecond { - t.Error("since is not correct") + t.Errorf("since is not correct.\tfastime.Now: %v,\ttime.Now: %v\tsince1: %d, \tsince2: %d", now.UnixNano(), timeNow.UnixNano(), since1, since2) } if math.Abs(float64(since1-since2)) > float64(50*time.Millisecond) { - t.Error("since error is too large") + t.Errorf("since error too large.\tfastime.Now: %v,\ttime.Now: %v\tsince1: %d, \tsince2: %d", now.UnixNano(), timeNow.UnixNano(), since1, since2) } }) } diff --git a/global.go b/global.go index 4f7a4e2..8afa373 100644 --- a/global.go +++ b/global.go @@ -47,7 +47,7 @@ func Now() time.Time { // Since returns the time elapsed since t. // It is shorthand for fastime.Now().Sub(t). func Since(t time.Time) time.Duration { - return instance.Now().Sub(t) + return instance.Since(t) } // Stop stops stopping time refresh daemon diff --git a/go.mod b/go.mod index ee70ece..1367cac 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/kpango/fastime -go 1.18 +go 1.20