bitbucket.org/alima123/expires@v0.0.0-20191226173914-cddaf5cffb3d/expires.go (about) 1 package expires 2 3 import ( 4 "fmt" 5 "time" 6 _ "unsafe" // for go:linkname 7 ) 8 9 type ( 10 Expires interface { 11 IsExpires() bool 12 GetExpires() time.Time 13 SetExpires(e bool) 14 fmt.Stringer 15 } 16 17 expires struct { 18 expiresAt time.Time 19 abort bool 20 } 21 ) 22 23 // StripMono strip monotonic clocks 24 //go:linkname StripMono time.(*Time).stripMono 25 func StripMono(t *time.Time) 26 27 func NewExpires(dur time.Duration) Expires { 28 t := time.Now().Add(dur) 29 StripMono(&t) 30 return &expires{ 31 expiresAt: t, 32 } 33 } 34 35 func NewExpiresAt(at time.Time) Expires { 36 StripMono(&at) 37 return &expires{ 38 expiresAt: at, 39 } 40 } 41 42 func (ep *expires) GetExpires() time.Time { 43 return ep.expiresAt 44 } 45 46 func (ep *expires) SetExpires(e bool) { 47 ep.abort = e 48 } 49 50 func (ep *expires) IsExpires() bool { 51 return ep.abort || time.Now().After(ep.expiresAt) 52 } 53 54 func (ep *expires) String() string { 55 return fmt.Sprintf("expires at: %s, abort: %t", ep.expiresAt, ep.abort) 56 }