diff --git a/fastime_test.go b/fastime_test.go index 9fa6ef5..912fdb3 100644 --- a/fastime_test.go +++ b/fastime_test.go @@ -2,6 +2,7 @@ package fastime import ( "context" + "math" "reflect" "sync/atomic" "testing" @@ -398,3 +399,28 @@ func TestFastime_store(t *testing.T) { }) } } + +func TestFastime_Since(t *testing.T) { + tests := []struct { + name string + }{ + { + name: "since", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + now := Now() + timeNow := time.Now() + time.Sleep(100 * time.Millisecond) + since1 := Since(now) + since2 := time.Since(timeNow) + if since1 < 50*time.Millisecond { + t.Error("since is not correct") + } + if math.Abs(float64(since1-since2)) > float64(50*time.Millisecond) { + t.Error("since error is too large") + } + }) + } +} diff --git a/global.go b/global.go index 20dcd77..4f7a4e2 100644 --- a/global.go +++ b/global.go @@ -44,6 +44,12 @@ func Now() time.Time { return instance.Now() } +// 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) +} + // Stop stops stopping time refresh daemon func Stop() { instance.Stop()