forked from Angey40/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
expires.go
60 lines (49 loc) · 965 Bytes
/
expires.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package expires
import (
"fmt"
"time"
_ "unsafe" // for go:linkname
)
type (
Expires interface {
IsExpires() bool
GetExpires() time.Time
SetExpires(e bool)
fmt.Stringer
}
expires struct {
expiresAt time.Time
abort bool
}
)
//go:linkname stripMono time.(*Time).stripMono
func stripMono(t *time.Time)
// StripMono strip monotonic clocks
func StripMono(t *time.Time) {
stripMono(t)
}
func NewExpires(dur time.Duration) Expires {
t := time.Now().Add(dur)
StripMono(&t)
return &expires{
expiresAt: t,
}
}
func NewExpiresAt(at time.Time) Expires {
StripMono(&at)
return &expires{
expiresAt: at,
}
}
func (ep *expires) GetExpires() time.Time {
return ep.expiresAt
}
func (ep *expires) SetExpires(e bool) {
ep.abort = e
}
func (ep *expires) IsExpires() bool {
return ep.abort || time.Now().After(ep.expiresAt)
}
func (ep *expires) String() string {
return fmt.Sprintf("expires at: %s, abort: %t", ep.expiresAt, ep.abort)
}