Skip to content

Commit

Permalink
add add & sub
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed Jun 25, 2019
1 parent 708dd50 commit 9883e18
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
16 changes: 12 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"
"time"

"github.com/araddon/dateparse"
Expand Down Expand Up @@ -35,11 +36,16 @@ func Main(cmd *cobra.Command, args []string) error {
//times
times := make([]time.Time, 0, len(args)+1)
if len(args) == 0 {
times = append(times, time.Now())
t := time.Now()
t = t.Add(viper.GetDuration("add"))
t = t.Add(-viper.GetDuration("sub"))
times = append(times, t)
}
for _, arg := range args {
t, err := dateparse.ParseStrict(arg)
t, err := dateparse.ParseStrict(strings.TrimSpace(arg))
exitForErr(err)
t = t.Add(viper.GetDuration("add"))
t = t.Add(-viper.GetDuration("sub"))
times = append(times, t)
}

Expand Down Expand Up @@ -81,8 +87,8 @@ func Main(cmd *cobra.Command, args []string) error {
// StampMilli = "Jan _2 15:04:05.000"
// StampMicro = "Jan _2 15:04:05.000000"
// StampNano = "Jan _2 15:04:05.000000000"
dest := fmt.Sprint(time.Now().UnixNano() / 1000000)
if len(viper.GetString("format")) > 0 {
dest := ""
switch viper.GetString("format") {
case "ANSIC":
dest = time.ANSIC
Expand Down Expand Up @@ -119,8 +125,10 @@ func Main(cmd *cobra.Command, args []string) error {
exitForErr(err)
dest = d
}
fmt.Fprintln(stdout, tm.Format(dest))
continue
}
fmt.Fprintln(stdout, tm.Format(dest))
fmt.Fprintln(stdout, tm.UnixNano()/1000000)
}
return nil
}
Expand Down
18 changes: 13 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -16,11 +17,15 @@ func RootCmd() *cobra.Command {
Use: "ts",
Short: "timestamp convert & compare tool",
Example: `
(timestamp) $: ts
(format) $: ts -f "2019/06/25 23:30:10"
(before) $: ts -b "2019/06/25 23:30:10" ; echo $?
(after) $: ts -a "2019/06/25 23:30:10" ; echo $?
(timezone) $: ts -f "2019/06/25 23:30:10" -z "Asia/Shanghai"
(now timestamp) $: ts
(now add) $: ts --add 1d
(now sub) $: ts --sub 1d
(convert) $: ts "2019/06/24 23:30:10"
(pipe) $: echo "2019/06/24 23:30:10" | ts
(format) $: ts -f "2019/06/25 23:30:10"
(before) $: ts -b "2019/06/25 23:30:10" ; echo $?
(after) $: ts -a "2019/06/25 23:30:10" ; echo $?
(timezone) $: ts -f "2019/06/25 23:30:10" -z "Asia/Shanghai"
`,
Run: func(cmd *cobra.Command, args []string) {
//pipe stdin
Expand All @@ -37,10 +42,13 @@ func RootCmd() *cobra.Command {
exitForErr(Main(cmd, args))
},
}

cmd.Flags().StringP("after", "a", "", "after compare")
cmd.Flags().StringP("before", "b", "", "before compare")
cmd.Flags().StringP("format", "f", "", "time format")
cmd.Flags().StringP("timezone", "z", "", "time zone")
cmd.Flags().DurationP("add", "", 0*time.Second, "add duration")
cmd.Flags().DurationP("sub", "", 0*time.Second, "sub duration")
return cmd
}

Expand Down

0 comments on commit 9883e18

Please sign in to comment.