代码拉取完成,页面将自动刷新
package add_locale
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/processors"
)
type addLocale struct {
TimezoneFormat TimezoneFormat
}
// TimezoneFormat type
type TimezoneFormat int
// Timezone formats
const (
Abbreviation TimezoneFormat = iota
Offset
)
var timezoneFormats = map[TimezoneFormat]string{
Abbreviation: "abbreviation",
Offset: "offset",
}
func (t TimezoneFormat) String() string {
return timezoneFormats[t]
}
func init() {
processors.RegisterPlugin("add_locale", newAddLocale)
}
func newAddLocale(c common.Config) (processors.Processor, error) {
config := struct {
Format string `config:"format"`
}{
Format: "offset",
}
err := c.Unpack(&config)
if err != nil {
return nil, errors.Wrap(err, "fail to unpack the add_locale configuration")
}
var loc addLocale
switch strings.ToLower(config.Format) {
case "abbreviation":
loc.TimezoneFormat = Abbreviation
case "offset":
loc.TimezoneFormat = Offset
default:
return nil, errors.Errorf("'%s' is not a valid format option for the "+
"add_locale processor. Valid options are 'abbreviation' and 'offset'.",
config.Format)
}
return loc, nil
}
func (l addLocale) Run(event common.MapStr) (common.MapStr, error) {
zone, offset := time.Now().Zone()
format := l.Format(zone, offset)
event.Put("beat.timezone", format)
return event, nil
}
const (
sec = 1
min = 60 * sec
hour = 60 * min
)
func (l addLocale) Format(zone string, offset int) string {
var ft string
switch l.TimezoneFormat {
case Abbreviation:
ft = zone
case Offset:
sign := "+"
if offset < 0 {
sign = "-"
offset *= -1
}
h := offset / hour
m := (offset - (h * hour)) / min
ft = fmt.Sprintf("%s%02d:%02d", sign, h, m)
}
return ft
}
func (l addLocale) String() string {
return "add_locale=[format=" + l.TimezoneFormat.String() + "]"
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。