period.go 918 字节
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9
package goku_log

import (
	"fmt"
	"strings"
)

type LogPeriod interface {
	String() string
Y
Your Name 已提交
10
	FormatLayout() string
E
eoLinker API Management 已提交
11 12 13
}
type LogPeriodType int

Y
Your Name 已提交
14
func ParsePeriod(v string) (LogPeriod, error) {
E
eoLinker API Management 已提交
15 16
	switch strings.ToLower(v) {
	case "month":
Y
Your Name 已提交
17
		return PeriodMonth, nil
E
eoLinker API Management 已提交
18
	case "day":
Y
Your Name 已提交
19
		return PeriodDay, nil
E
eoLinker API Management 已提交
20
	case "hour":
Y
Your Name 已提交
21
		return PeriodHour, nil
E
eoLinker API Management 已提交
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
	}

	return nil, fmt.Errorf("not a valid period: %q", v)
}
func (period LogPeriodType) String() string {
	switch period {
	case PeriodMonth:
		return "month"
	case PeriodDay:
		return "day"
	case PeriodHour:
		return "hour"
	default:
		return "unknown"
	}
}

const (
	PeriodMonth LogPeriodType = iota
	PeriodDay
	PeriodHour
)

func (period LogPeriodType) FormatLayout() string {
	switch period {
	case PeriodHour:
		{
Y
Your Name 已提交
49
			return "2006-01-02-15"
E
eoLinker API Management 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
		}
	case PeriodDay:
		{
			return "2006-01-02"
		}
	case PeriodMonth:
		{
			return "2006-01"
		}
	default:
		return "2006-01-02-15"
	}
}