chenwanli
2019-02-26 17:12:39 +08:00
parent 344caf3cd7
commit fd27ed7755

View File

@@ -16,8 +16,8 @@ const (
) )
type TTL struct { type TTL struct {
count byte Count byte
unit byte Unit byte
} }
var EMPTY_TTL = &TTL{} var EMPTY_TTL = &TTL{}
@@ -43,12 +43,12 @@ func ReadTTL(ttlString string) (*TTL, error) {
} }
count, err := strconv.Atoi(string(countBytes)) count, err := strconv.Atoi(string(countBytes))
unit := toStoredByte(unitByte) unit := toStoredByte(unitByte)
return &TTL{count: byte(count), unit: unit}, err return &TTL{Count: byte(count), Unit: unit}, err
} }
// read stored bytes to a ttl // read stored bytes to a ttl
func LoadTTLFromBytes(input []byte) (t *TTL) { func LoadTTLFromBytes(input []byte) (t *TTL) {
return &TTL{count: input[0], unit: input[1]} return &TTL{Count: input[0], Unit: input[1]}
} }
// read stored bytes to a ttl // read stored bytes to a ttl
@@ -61,25 +61,25 @@ func LoadTTLFromUint32(ttl uint32) (t *TTL) {
// save stored bytes to an output with 2 bytes // save stored bytes to an output with 2 bytes
func (t *TTL) ToBytes(output []byte) { func (t *TTL) ToBytes(output []byte) {
output[0] = t.count output[0] = t.Count
output[1] = t.unit output[1] = t.Unit
} }
func (t *TTL) ToUint32() (output uint32) { func (t *TTL) ToUint32() (output uint32) {
output = uint32(t.count) << 8 output = uint32(t.Count) << 8
output += uint32(t.unit) output += uint32(t.Unit)
return output return output
} }
func (t *TTL) String() string { func (t *TTL) String() string {
if t == nil || t.count == 0 { if t == nil || t.Count == 0 {
return "" return ""
} }
if t.unit == Empty { if t.Unit == Empty {
return "" return ""
} }
countString := strconv.Itoa(int(t.count)) countString := strconv.Itoa(int(t.Count))
switch t.unit { switch t.Unit {
case Minute: case Minute:
return countString + "m" return countString + "m"
case Hour: case Hour:
@@ -115,21 +115,21 @@ func toStoredByte(readableUnitByte byte) byte {
} }
func (t TTL) Minutes() uint32 { func (t TTL) Minutes() uint32 {
switch t.unit { switch t.Unit {
case Empty: case Empty:
return 0 return 0
case Minute: case Minute:
return uint32(t.count) return uint32(t.Count)
case Hour: case Hour:
return uint32(t.count) * 60 return uint32(t.Count) * 60
case Day: case Day:
return uint32(t.count) * 60 * 24 return uint32(t.Count) * 60 * 24
case Week: case Week:
return uint32(t.count) * 60 * 24 * 7 return uint32(t.Count) * 60 * 24 * 7
case Month: case Month:
return uint32(t.count) * 60 * 24 * 31 return uint32(t.Count) * 60 * 24 * 31
case Year: case Year:
return uint32(t.count) * 60 * 24 * 365 return uint32(t.Count) * 60 * 24 * 365
} }
return 0 return 0
} }