revamp
This commit is contained in:
41
utils.go
Normal file
41
utils.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Expiration(now time.Time, deadline string) (time.Time, error) {
|
||||
log.WithFields(log.Fields{"now": now, "deadline": deadline}).Debugf("starting")
|
||||
defer log.WithFields(log.Fields{"now": now, "deadline": deadline}).Debugf("done")
|
||||
|
||||
if deadline == "forever" {
|
||||
return time.Unix(1<<63-1, 0), nil
|
||||
}
|
||||
|
||||
reExpiration := regexp.MustCompile(`([0-9]+)([a-z]+)`)
|
||||
for _, v := range reExpiration.FindAllStringSubmatch(deadline, -1) {
|
||||
log.WithFields(log.Fields{"now": now, "deadline": deadline}).Debugf("duration[%d] : %v", len(v), v)
|
||||
count, _ := strconv.Atoi(v[1])
|
||||
switch v[2] {
|
||||
case "y":
|
||||
now = now.AddDate(count, 0, 0)
|
||||
case "m":
|
||||
now = now.AddDate(0, count, 0)
|
||||
case "d":
|
||||
now = now.AddDate(0, 0, count)
|
||||
case "h":
|
||||
now = now.Add(time.Duration(time.Duration(count) * time.Hour))
|
||||
default:
|
||||
err := errors.New("invalid duration")
|
||||
log.WithFields(log.Fields{"now": now, "deadline": deadline, "attr": v[2], "error": err}).Errorf("")
|
||||
return time.Now(), err
|
||||
}
|
||||
}
|
||||
|
||||
return now, nil
|
||||
}
|
||||
Reference in New Issue
Block a user