-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package timez | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type ctxKeyNow struct{} | ||
|
||
func Now(ctx context.Context) time.Time { | ||
nowFuncRWMu.RLock() | ||
defer nowFuncRWMu.RUnlock() | ||
return nowFunc(ctx) | ||
} | ||
|
||
//nolint:gochecknoglobals | ||
var ( | ||
nowFunc = DefaultNowFunc | ||
nowFuncRWMu sync.RWMutex | ||
) | ||
|
||
type NowFunc = func(ctx context.Context) time.Time | ||
|
||
func DefaultNowFunc(ctx context.Context) time.Time { | ||
if now, ok := FromContext(ctx); ok { | ||
return now | ||
} | ||
|
||
return time.Now() | ||
} | ||
|
||
func SetNowFunc(now NowFunc) (backup NowFunc) { | ||
nowFuncRWMu.Lock() | ||
defer nowFuncRWMu.Unlock() | ||
backup = nowFunc | ||
nowFunc = now | ||
return backup | ||
} | ||
|
||
func WithContext(ctx context.Context, now time.Time) context.Context { | ||
return context.WithValue(ctx, ctxKeyNow{}, now) | ||
} | ||
|
||
func FromContext(ctx context.Context) (time.Time, bool) { | ||
now, ok := ctx.Value(ctxKeyNow{}).(time.Time) | ||
return now, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package timez_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
timez "github.com/kunitsucom/util.go/time" | ||
) | ||
|
||
func TestNow(t *testing.T) { | ||
t.Parallel() | ||
t.Run("success,", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
constant := time.Date(2023, 8, 24, 21, 32, 0, 0, time.UTC) | ||
ctx = timez.WithContext(ctx, constant) | ||
|
||
actual := timez.Now(ctx) | ||
if constant != actual { | ||
t.Errorf("❌: constant(%v) != actual(%v)", constant, actual) | ||
} | ||
|
||
backup := timez.SetNowFunc(func(_ context.Context) time.Time { return time.Now() }) | ||
current := timez.Now(context.Background()) | ||
if current.After(constant) { | ||
t.Errorf("❌: current(%v) after constant(%v)", current, constant) | ||
} | ||
|
||
timez.SetNowFunc(backup) | ||
current2 := timez.Now(context.Background()) | ||
if !current2.After(current) { | ||
t.Errorf("❌: current2(%v) after current(%v)", current2, current) | ||
} | ||
}) | ||
} |