2019-04-19 04:43:36 +00:00
|
|
|
package needle
|
2014-09-20 19:38:59 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-20 00:59:43 +00:00
|
|
|
"fmt"
|
2014-09-20 19:38:59 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-20 00:59:43 +00:00
|
|
|
// stored unit types
|
2014-09-20 19:38:59 +00:00
|
|
|
Empty byte = iota
|
|
|
|
Minute
|
|
|
|
Hour
|
|
|
|
Day
|
|
|
|
Week
|
|
|
|
Month
|
|
|
|
Year
|
|
|
|
)
|
|
|
|
|
|
|
|
type TTL struct {
|
2019-02-26 09:12:39 +00:00
|
|
|
Count byte
|
|
|
|
Unit byte
|
2014-09-20 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var EMPTY_TTL = &TTL{}
|
|
|
|
|
|
|
|
// translate a readable ttl to internal ttl
|
|
|
|
// Supports format example:
|
|
|
|
// 3m: 3 minutes
|
|
|
|
// 4h: 4 hours
|
|
|
|
// 5d: 5 days
|
|
|
|
// 6w: 6 weeks
|
|
|
|
// 7M: 7 months
|
|
|
|
// 8y: 8 years
|
|
|
|
func ReadTTL(ttlString string) (*TTL, error) {
|
|
|
|
if ttlString == "" {
|
|
|
|
return EMPTY_TTL, nil
|
|
|
|
}
|
|
|
|
ttlBytes := []byte(ttlString)
|
|
|
|
unitByte := ttlBytes[len(ttlBytes)-1]
|
|
|
|
countBytes := ttlBytes[0 : len(ttlBytes)-1]
|
|
|
|
if '0' <= unitByte && unitByte <= '9' {
|
|
|
|
countBytes = ttlBytes
|
|
|
|
unitByte = 'm'
|
|
|
|
}
|
|
|
|
count, err := strconv.Atoi(string(countBytes))
|
|
|
|
unit := toStoredByte(unitByte)
|
2019-02-26 09:12:39 +00:00
|
|
|
return &TTL{Count: byte(count), Unit: unit}, err
|
2014-09-20 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read stored bytes to a ttl
|
|
|
|
func LoadTTLFromBytes(input []byte) (t *TTL) {
|
2019-06-26 18:18:23 +00:00
|
|
|
if input[0] == 0 && input[1] == 0 {
|
|
|
|
return EMPTY_TTL
|
|
|
|
}
|
2019-02-26 09:12:39 +00:00
|
|
|
return &TTL{Count: input[0], Unit: input[1]}
|
2014-09-20 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read stored bytes to a ttl
|
|
|
|
func LoadTTLFromUint32(ttl uint32) (t *TTL) {
|
|
|
|
input := make([]byte, 2)
|
|
|
|
input[1] = byte(ttl)
|
|
|
|
input[0] = byte(ttl >> 8)
|
|
|
|
return LoadTTLFromBytes(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
// save stored bytes to an output with 2 bytes
|
2015-03-22 19:50:04 +00:00
|
|
|
func (t *TTL) ToBytes(output []byte) {
|
2019-02-26 09:12:39 +00:00
|
|
|
output[0] = t.Count
|
|
|
|
output[1] = t.Unit
|
2014-09-20 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 19:50:04 +00:00
|
|
|
func (t *TTL) ToUint32() (output uint32) {
|
2020-01-08 16:49:18 +00:00
|
|
|
if t == nil || t.Count == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2019-02-26 09:12:39 +00:00
|
|
|
output = uint32(t.Count) << 8
|
|
|
|
output += uint32(t.Unit)
|
2014-09-20 19:38:59 +00:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2015-03-22 19:50:04 +00:00
|
|
|
func (t *TTL) String() string {
|
2019-02-26 09:12:39 +00:00
|
|
|
if t == nil || t.Count == 0 {
|
2014-09-20 19:38:59 +00:00
|
|
|
return ""
|
|
|
|
}
|
2019-02-26 09:12:39 +00:00
|
|
|
if t.Unit == Empty {
|
2014-09-20 19:38:59 +00:00
|
|
|
return ""
|
|
|
|
}
|
2019-02-26 09:12:39 +00:00
|
|
|
countString := strconv.Itoa(int(t.Count))
|
|
|
|
switch t.Unit {
|
2014-09-20 19:38:59 +00:00
|
|
|
case Minute:
|
|
|
|
return countString + "m"
|
|
|
|
case Hour:
|
|
|
|
return countString + "h"
|
|
|
|
case Day:
|
|
|
|
return countString + "d"
|
|
|
|
case Week:
|
|
|
|
return countString + "w"
|
|
|
|
case Month:
|
|
|
|
return countString + "M"
|
|
|
|
case Year:
|
|
|
|
return countString + "y"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func toStoredByte(readableUnitByte byte) byte {
|
|
|
|
switch readableUnitByte {
|
|
|
|
case 'm':
|
|
|
|
return Minute
|
|
|
|
case 'h':
|
|
|
|
return Hour
|
|
|
|
case 'd':
|
|
|
|
return Day
|
|
|
|
case 'w':
|
|
|
|
return Week
|
|
|
|
case 'M':
|
|
|
|
return Month
|
2014-09-21 03:51:24 +00:00
|
|
|
case 'y':
|
2014-09-20 19:38:59 +00:00
|
|
|
return Year
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TTL) Minutes() uint32 {
|
2019-02-26 09:12:39 +00:00
|
|
|
switch t.Unit {
|
2014-09-20 19:38:59 +00:00
|
|
|
case Empty:
|
|
|
|
return 0
|
|
|
|
case Minute:
|
2019-02-26 09:12:39 +00:00
|
|
|
return uint32(t.Count)
|
2014-09-20 19:38:59 +00:00
|
|
|
case Hour:
|
2019-02-26 09:12:39 +00:00
|
|
|
return uint32(t.Count) * 60
|
2014-09-20 19:38:59 +00:00
|
|
|
case Day:
|
2019-02-26 09:12:39 +00:00
|
|
|
return uint32(t.Count) * 60 * 24
|
2014-09-20 19:38:59 +00:00
|
|
|
case Week:
|
2019-02-26 09:12:39 +00:00
|
|
|
return uint32(t.Count) * 60 * 24 * 7
|
2014-09-20 19:38:59 +00:00
|
|
|
case Month:
|
2020-12-03 18:53:29 +00:00
|
|
|
return uint32(t.Count) * 60 * 24 * 30
|
2014-09-20 19:38:59 +00:00
|
|
|
case Year:
|
2019-02-26 09:12:39 +00:00
|
|
|
return uint32(t.Count) * 60 * 24 * 365
|
2014-09-20 19:38:59 +00:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2020-07-20 00:59:43 +00:00
|
|
|
|
|
|
|
func SecondsToTTL(seconds int32) string {
|
|
|
|
if seconds == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2020-12-04 03:23:59 +00:00
|
|
|
if seconds%(3600*24*365) == 0 && seconds/(3600*24*365) < 256 {
|
|
|
|
return fmt.Sprintf("%dy", seconds/(3600*24*365))
|
|
|
|
}
|
|
|
|
if seconds%(3600*24*30) == 0 && seconds/(3600*24*30) < 256 {
|
|
|
|
return fmt.Sprintf("%dM", seconds/(3600*24*30))
|
|
|
|
}
|
|
|
|
if seconds%(3600*24*7) == 0 && seconds/(3600*24*7) < 256 {
|
|
|
|
return fmt.Sprintf("%dw", seconds/(3600*24*7))
|
|
|
|
}
|
|
|
|
if seconds%(3600*24) == 0 && seconds/(3600*24) < 256 {
|
|
|
|
return fmt.Sprintf("%dd", seconds/(3600*24))
|
|
|
|
}
|
|
|
|
if seconds%(3600) == 0 && seconds/(3600) < 256 {
|
|
|
|
return fmt.Sprintf("%dh", seconds/(3600))
|
|
|
|
}
|
2020-12-03 18:53:29 +00:00
|
|
|
if seconds/60 < 256 {
|
|
|
|
return fmt.Sprintf("%dm", seconds/60)
|
|
|
|
}
|
|
|
|
if seconds/(3600) < 256 {
|
|
|
|
return fmt.Sprintf("%dh", seconds/(3600))
|
|
|
|
}
|
|
|
|
if seconds/(3600*24) < 256 {
|
|
|
|
return fmt.Sprintf("%dd", seconds/(3600*24))
|
|
|
|
}
|
|
|
|
if seconds/(3600*24*7) < 256 {
|
|
|
|
return fmt.Sprintf("%dw", seconds/(3600*24*7))
|
|
|
|
}
|
|
|
|
if seconds/(3600*24*30) < 256 {
|
|
|
|
return fmt.Sprintf("%dM", seconds/(3600*24*30))
|
|
|
|
}
|
|
|
|
if seconds/(3600*24*365) < 256 {
|
|
|
|
return fmt.Sprintf("%dy", seconds/(3600*24*365))
|
|
|
|
}
|
|
|
|
return ""
|
2020-07-20 00:59:43 +00:00
|
|
|
}
|