Files
trivy/pkg/clock/clock.go
Teppei Fukuda 26faf8f3f0 feat: add support for plugin index (#6674)
Signed-off-by: knqyf263 <knqyf263@gmail.com>
Co-authored-by: DmitriyLewen <91113035+DmitriyLewen@users.noreply.github.com>
2024-05-14 08:29:20 +00:00

39 lines
823 B
Go

package clock
import (
"context"
"time"
"k8s.io/utils/clock"
clocktesting "k8s.io/utils/clock/testing"
)
type (
RealClock = clock.RealClock
FakeClock = clocktesting.FakeClock
)
// clockKey is the context key for clock. It is unexported to prevent collisions with context keys defined in
// other packages.
type clockKey struct{}
// With returns a new context with the given time.
func With(ctx context.Context, t time.Time) context.Context {
c := clocktesting.NewFakeClock(t)
return context.WithValue(ctx, clockKey{}, c)
}
// Now returns the current time.
func Now(ctx context.Context) time.Time {
return Clock(ctx).Now()
}
// Clock returns the clock from the context.
func Clock(ctx context.Context) clock.Clock {
t, ok := ctx.Value(clockKey{}).(clock.Clock)
if !ok {
return RealClock{}
}
return t
}