package datex import ( "fmt" "regexp" "strconv" "strings" "time" aw "github.com/deanishe/awgo" "gitea.micah.wiki/pandora/alfred/model" "gitea.micah.wiki/pandora/alfred/pkg/stringx" ) var ( regexpTimestamp = regexp.MustCompile(`^[1-9]{1}\d+$`) yyyyMMddHHMMSSLayout = "2006-01-02 15:04:05" moreLayouts = []string{ "2006-01-02", "2006-01-02 15:04", "2006-01-02 15:04:05", } ) func Do(args []string) []*model.Item { if len(args) == 0 { return nil } // 处理 now input := strings.Join(args, " ") if input == "now" || input == "" { input = fmt.Sprintf("%d", time.Now().Unix()) } items := make([]*model.Item, 0) if regexpTimestamp.MatchString(input) { v, err := strconv.ParseInt(input, 10, 32) if err != nil { return nil } value := time.Unix(v, 0).Format(yyyyMMddHHMMSSLayout) items = append(items, &model.Item{ Title: value, Subtitle: stringx.NewStrPoint(yyyyMMddHHMMSSLayout), Arg: []string{"copytext", value}, Valid: true, Icon: aw.IconClock, }) } else { for _, layout := range moreLayouts { v, err := time.Parse(layout, input) if err != nil { continue } value := fmt.Sprintf("%d", v.Unix()) items = append(items, &model.Item{ Title: value, Subtitle: stringx.NewStrPoint(layout), Arg: []string{"copytext", value}, Valid: true, Icon: aw.IconClock, }) } } if len(items) == 0 { return nil } return items }