Skip to content

Commit

Permalink
add Formats flag & update compare result
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed Jun 26, 2019
1 parent b257f86 commit ec79267
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 26 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,26 @@ Examples:
(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"

(Formats) $: ts -F
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
TimestampSec = "time.Unix()"
TimestampMilli = "time.UnixNano()/1000000"
TimestampMicro = "time.UnixNano()/1000"
TimestampNano = "time.UnixNano()"

Flags:
--add duration add duration
Expand Down
85 changes: 64 additions & 21 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,66 @@ import (
"github.com/x-mod/errors"
)

type cmpCode struct {
val int32
}

func (cc *cmpCode) Value() int32 {
if cc.val != 0 {
return 1
}
return cc.val
}

func (cc *cmpCode) String() string {
if cc.val == 1 {
return "True"
}
return "False"
}

func exitForErr(err error) {
if err != nil {
os.Stderr.Write([]byte(err.Error()))
os.Stdout.Write([]byte(err.Error() + "\n"))
os.Exit(int(errors.ValueFrom(err)))
}
}

func printFormats() {
fmt.Println(`
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
TimestampSec = "time.Unix()"
TimestampMilli = "time.UnixNano()/1000000"
TimestampMicro = "time.UnixNano()/1000"
TimestampNano = "time.UnixNano()"
`)
}

func Main(cmd *cobra.Command, args []string) error {
//version
if viper.GetBool("version") {
fmt.Println(build.String())
return nil
}
//formats
if viper.GetBool("Formats") {
printFormats()
return nil
}
//pipe stdin
if len(args) == 0 {
info, err := os.Stdin.Stat()
Expand Down Expand Up @@ -75,9 +122,9 @@ func Main(cmd *cobra.Command, args []string) error {
return errors.Annotate(err, "parse strict")
}
if t.After(times[0]) {
return errors.ValueErr(1)
return errors.CodeError(&cmpCode{val: 1})
}
return nil
return errors.CodeError(&cmpCode{})
}

//after compare
Expand All @@ -87,29 +134,13 @@ func Main(cmd *cobra.Command, args []string) error {
return errors.Annotate(err, "parse strict")
}
if t.Before(times[0]) {
return errors.ValueErr(1)
return errors.CodeError(&cmpCode{val: 1})
}
return nil
return errors.CodeError(&cmpCode{})
}

//convert
for _, tm := range times {
// ANSIC = "Mon Jan _2 15:04:05 2006"
// UnixDate = "Mon Jan _2 15:04:05 MST 2006"
// RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
// RFC822 = "02 Jan 06 15:04 MST"
// RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
// RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
// RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
// RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
// RFC3339 = "2006-01-02T15:04:05Z07:00"
// RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
// Kitchen = "3:04PM"
// // Handy time stamps.
// Stamp = "Jan _2 15:04:05"
// StampMilli = "Jan _2 15:04:05.000"
// StampMicro = "Jan _2 15:04:05.000000"
// StampNano = "Jan _2 15:04:05.000000000"
if len(viper.GetString("format")) > 0 {
dest := ""
switch viper.GetString("format") {
Expand Down Expand Up @@ -143,6 +174,18 @@ func Main(cmd *cobra.Command, args []string) error {
dest = time.StampMicro
case "StampNano":
dest = time.StampNano
case "TimestampSec":
fmt.Fprintln(os.Stdout, tm.Unix())
continue
case "TimestampMilli":
fmt.Fprintln(os.Stdout, tm.UnixNano()/1000000)
continue
case "TimestampMicro":
fmt.Fprintln(os.Stdout, tm.UnixNano()/1000)
continue
case "TimestampNano":
fmt.Fprintln(os.Stdout, tm.UnixNano())
continue
default:
d, err := dateparse.ParseFormat(viper.GetString("format"))
if err != nil {
Expand Down
17 changes: 14 additions & 3 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ func TestVersion(t *testing.T) {
assert.Nil(t, Main(cmd, []string{}))
}

func TestFormats(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("Formats", "true")
assert.Nil(t, Main(cmd, []string{}))
}

func TestArgs(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
Expand Down Expand Up @@ -45,11 +52,13 @@ func TestBefore(t *testing.T) {
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("before", "2039adfasdf")
assert.NotNil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("before", "2039/12/12")
cmd.Flags().Set("before", "2029/12/12")
e1 := Main(cmd, []string{"2019/06/12"})
assert.NotNil(t, e1)
assert.Equal(t, int32(1), errors.ValueFrom(e1), e1.Error())
assert.Nil(t, Main(cmd, []string{"2040/06/12"}))
e2 := Main(cmd, []string{"2039/06/12"})
assert.NotNil(t, e2)
assert.Equal(t, int32(0), errors.ValueFrom(e2), e2.Error())
}

func TestAfter(t *testing.T) {
Expand All @@ -58,10 +67,12 @@ func TestAfter(t *testing.T) {
cmd.Flags().Set("after", "2009fasdfa")
assert.NotNil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("after", "2009/01/01")
e1 := Main(cmd, []string{"2008/06/12"})
assert.NotNil(t, e1)
assert.Equal(t, int32(0), errors.ValueFrom(e1), e1.Error())
e2 := Main(cmd, []string{"2019/06/12"})
assert.NotNil(t, e2)
assert.Equal(t, int32(1), errors.ValueFrom(e2), e2.Error())
assert.Nil(t, Main(cmd, []string{"2008/06/12"}))
}

func TestFormat(t *testing.T) {
Expand Down
23 changes: 22 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ func RootCmd() *cobra.Command {
(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"
(Formats) $: ts -F
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
TimestampSec = "time.Unix()"
TimestampMilli = "time.UnixNano()/1000000"
TimestampMicro = "time.UnixNano()/1000"
TimestampNano = "time.UnixNano()"
`,
Run: func(cmd *cobra.Command, args []string) {
exitForErr(Main(cmd, args))
Expand All @@ -29,8 +49,9 @@ func RootCmd() *cobra.Command {
cmd.Flags().BoolP("version", "v", false, "current version")
cmd.Flags().StringP("after", "a", "", "after compare")
cmd.Flags().StringP("before", "b", "", "before compare")
cmd.Flags().StringP("format", "f", "", "time format")
cmd.Flags().StringP("format", "f", "TimestampMilli", "time format")
cmd.Flags().StringP("timezone", "z", "", "time zone")
cmd.Flags().BoolP("Formats", "F", false, "show formats ?")
cmd.Flags().DurationP("add", "", 0*time.Second, "add duration")
cmd.Flags().DurationP("sub", "", 0*time.Second, "sub duration")
return cmd
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/araddon/dateparse v0.0.0-20190622164848-0fb0a474d195
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.3.0
github.com/x-mod/build v0.1.0
github.com/x-mod/errors v0.1.6
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand Down Expand Up @@ -67,6 +68,7 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
Expand Down Expand Up @@ -98,6 +100,7 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
Expand Down

0 comments on commit ec79267

Please sign in to comment.