Skip to content

Commit

Permalink
[patch] add fastime.Since
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <[email protected]>
  • Loading branch information
kpango committed Feb 8, 2023
1 parent 1df1fff commit 7ad0375
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
```
Expand Down
16 changes: 0 additions & 16 deletions example/result.txt

This file was deleted.

5 changes: 5 additions & 0 deletions fastime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions fastime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/kpango/fastime

go 1.18
go 1.20

0 comments on commit 7ad0375

Please sign in to comment.