Skip to content

Commit

Permalink
Merge pull request #7 from TTNomi/feature/timeformat
Browse files Browse the repository at this point in the history
time parser
  • Loading branch information
sipt authored Jul 31, 2023
2 parents 6a85d55 + f520c51 commit c4238e9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build:
GOOS=darwin GOARCH=amd64 go build -o bin/awpark.amd64 main.go
GOOS=darwin GOARCH=arm64 go build -o bin/awpark.arm64 main.go
mkdir -p bin
lipo -create -output ./bin/awpark bin/awpark.amd64 bin/awpark.arm64
74 changes: 74 additions & 0 deletions cmd/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func init() {
Register(&timestampGetter{})
Register(&timestampFormatter{})
Register(&timestampParser{})
}

type timestampGetter struct{ RunModeRun }
Expand Down Expand Up @@ -71,3 +72,76 @@ func (t *timestampFormatter) Action(args []string) {
wf.NewItem("Formatted: " + formatted).Valid(true).Copytext(formatted).Arg(formatted).Icon(&aw.Icon{Value: "clock.png"}).Subtitle("Press [Enter], copy to the clipboard.")
}
}

type timestampParser struct{ RunModeRun }

func (t *timestampParser) Use() string {
return "timestamp-parser"
}

func (t *timestampParser) ActionItem() *aw.Item {
return wf.NewItem("Timestamp Parser").UID("B00000004-3").Valid(true).Icon(&aw.Icon{Value: "clock.png"})
}

func (t *timestampParser) Action(args []string) {
formatTemplate := []string{
"2006-01-02T15:04:05Z07:00",
"2006-01-02T15:04:05.999999999Z07:00",
"2006-01-02T15:04:05.999999999",
"2006-01-02T15:04:05",
"2006-01-02 15:04:05Z07:00",
"2006-01-02 15:04:05.999999999Z07:00",
"2006-01-02 15:04:05.999999999",
"2006-01-02 15:04:05 -0700 MST",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05 -07:00",
"2006-01-02 15:04:05.999999999 -0700 MST",
"2006-01-02 15:04:05.999999999 -0700",
"2006-01-02 15:04:05.999999999 -07:00",
"2006-01-02 15:04:05",
"2006-01-02",
"01/02 03:04:05PM '06 -0700",
"Mon Jan _2 15:04:05 2006",
"Mon Jan _2 15:04:05 MST 2006",
"Mon Jan 02 15:04:05 -0700 2006",
"02 Jan 06 15:04 MST",
"02 Jan 06 15:04 -0700",
"Monday, 02-Jan-06 15:04:05 MST",
"Mon, 02 Jan 2006 15:04:05 MST",
"Mon, 02 Jan 2006 15:04:05 -0700",
"3:04PM",
"Jan _2 15:04:05",
"Jan _2 15:04:05.000",
"Jan _2 15:04:05.000000",
"Jan _2 15:04:05.000000000",
}
tips := func() {
wf.NewItem("Reference the following date format.").Valid(false).Icon(&aw.Icon{Value: "clock.png"})
for _, t := range formatTemplate {
wf.NewItem("eg. " + t).Valid(false).Icon(&aw.Icon{Value: "clock.png"})
}
}
if len(args) > 0 && len(args[0]) > 0 {
timestr := args[0]
var (
inputTime time.Time
err error
)
for _, format := range formatTemplate {
inputTime, err = time.ParseInLocation(format, timestr, time.Local)
if err == nil {
break
}
}
if err != nil {
tips()
return
}
seconds := fmt.Sprintf("%d", inputTime.Unix())
milliseconds := fmt.Sprintf("%d", inputTime.UnixNano()/int64(time.Millisecond))
wf.NewItem("Seconds: " + seconds).Valid(true).Copytext(seconds).Arg(seconds).Icon(&aw.Icon{Value: "clock.png"})
wf.NewItem("Milliseconds: " + milliseconds).Valid(true).Copytext(milliseconds).Arg(milliseconds).Icon(&aw.Icon{Value: "clock.png"}).Subtitle("Press [Enter], copy to the clipboard.")
} else {
tips()
}
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/sipt/awpark/cmd"
"github.com/sipt/awpark/cmd"
)

func main() {
cmd.Execute()
}
}

0 comments on commit c4238e9

Please sign in to comment.