-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess_time_utilities.go
77 lines (71 loc) · 2.21 KB
/
access_time_utilities.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package auth
import "time"
func SetTokenExpiredTime(accessTimeFrom *time.Time, accessTimeTo *time.Time, expires int64) (time.Time, int64) {
if accessTimeTo == nil || accessTimeFrom == nil {
var tokenExpiredTime = time.Now().Add(time.Second * time.Duration(int(expires/1000)))
return tokenExpiredTime, expires
}
if accessTimeTo.Before(*accessTimeFrom) || accessTimeTo.Equal(*accessTimeFrom) {
tmp := accessTimeTo.Add(time.Hour * 24)
accessTimeTo = &tmp
}
var tokenExpiredTime time.Time
var jwtExpiredTime int64
if time.Millisecond*time.Duration(expires) > accessTimeTo.Sub(time.Now()) {
tokenExpiredTime = time.Now().Add(accessTimeTo.Sub(time.Now())).UTC()
jwtExpiredTime = int64(accessTimeTo.Sub(time.Now()).Seconds() * 1000)
} else {
tokenExpiredTime = time.Now().Add(time.Millisecond * time.Duration(expires)).UTC()
jwtExpiredTime = expires
}
return tokenExpiredTime, jwtExpiredTime
}
func IsAccessDateValid(fromDate, toDate *time.Time) bool {
today := time.Now()
if fromDate == nil && toDate == nil {
return true
} else if fromDate == nil {
toDateStr := toDate.Add(time.Hour * 24)
if toDateStr.After(today) {
return true
}
} else if toDate == nil {
if fromDate.Before(today) || fromDate.Equal(today) {
return true
}
} else {
toDateStr := toDate.Add(time.Hour * 24)
if (fromDate.Before(today) || fromDate.Equal(today)) && toDateStr.After(today) {
return true
}
}
return false
}
func IsAccessTimeValid(fromTime, toTime *time.Time) bool {
today := time.Now()
location := time.Now().Location()
if fromTime == nil && toTime == nil {
return true
} else if fromTime == nil {
toTimeStr := toTime.In(location)
if toTimeStr.After(today) || toTimeStr.Equal(today) {
return true
}
return false
} else if toTime == nil {
fromTimeStr := fromTime.In(location)
if fromTimeStr.Before(today) || fromTimeStr.Equal(today) {
return true
}
return false
}
toTimeStr := toTime.In(location)
fromTimeStr := fromTime.In(location)
if toTimeStr.Before(fromTimeStr) || toTimeStr.Equal(fromTimeStr) {
toTimeStr = toTimeStr.Add(time.Hour * 24)
}
if (fromTimeStr.Before(today) || fromTimeStr.Equal(today)) && (toTimeStr.After(today) || toTimeStr.Equal(today)) {
return true
}
return false
}