Skip to content

Commit

Permalink
Introduce fastime.Since method (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
dongnguyenvt authored Feb 7, 2023
1 parent a86aea0 commit 1df1fff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
26 changes: 26 additions & 0 deletions fastime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fastime

import (
"context"
"math"
"reflect"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -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")
}
})
}
}
6 changes: 6 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1df1fff

Please sign in to comment.