diff --git a/command_test.go b/command_test.go index 4c1a4e8631..f910c6e085 100644 --- a/command_test.go +++ b/command_test.go @@ -3254,6 +3254,44 @@ func TestCommand_Duration(t *testing.T) { r.Equal(13*time.Second, cmd.Duration("top-flag")) } +func TestCommand_Timestamp(t *testing.T) { + t1 := time.Time{}.Add(12 * time.Second) + t2 := time.Time{}.Add(13 * time.Second) + + cmd := &Command{ + Name: "hello", + Flags: []Flag{ + &TimestampFlag{ + Name: "myflag", + Value: t1, + }, + }, + Action: func(ctx context.Context, c *Command) error { + return nil + }, + } + + pCmd := &Command{ + Flags: []Flag{ + &TimestampFlag{ + Name: "top-flag", + Value: t2, + }, + }, + Commands: []*Command{ + cmd, + }, + } + + if err := pCmd.Run(context.Background(), []string{"foo", "hello"}); err != nil { + t.Error(err) + } + + r := require.New(t) + r.Equal(t1, cmd.Timestamp("myflag")) + r.Equal(t2, cmd.Timestamp("top-flag")) +} + func TestCommand_String(t *testing.T) { set := flag.NewFlagSet("test", 0) set.String("myflag", "hello world", "doc") diff --git a/flag_timestamp.go b/flag_timestamp.go index 6c5cf963a1..3709bd83e8 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -1,7 +1,6 @@ package cli import ( - "flag" "fmt" "time" ) @@ -85,25 +84,12 @@ func (t *timestampValue) Get() any { } // Timestamp gets the timestamp from a flag name -func (cmd *Command) Timestamp(name string) *time.Time { - if fs := cmd.lookupFlagSet(name); fs != nil { - return lookupTimestamp(name, fs, cmd.Name) +func (cmd *Command) Timestamp(name string) time.Time { + if v, ok := cmd.Value(name).(time.Time); ok { + tracef("time.Time available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmd.Name) + return v } - return nil -} - -// Fetches the timestamp value from the local timestampWrap -func lookupTimestamp(name string, set *flag.FlagSet, cmdName string) *time.Time { - fl := set.Lookup(name) - if fl != nil { - if tv, ok := fl.Value.(*timestampValue); ok { - v := tv.Value() - tracef("timestamp available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmdName) - return v - } - } - - tracef("timestamp NOT available for flag name %[1]q (cmd=%[2]q)", name, cmdName) - return nil + tracef("time.Time NOT available for flag name %[1]q (cmd=%[2]q)", name, cmd.Name) + return time.Time{} } diff --git a/godoc-current.txt b/godoc-current.txt index 5229697ea0..f39be4666b 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -456,7 +456,7 @@ func (cmd *Command) StringSlice(name string) []string StringSlice looks up the value of a local StringSliceFlag, returns nil if not found -func (cmd *Command) Timestamp(name string) *time.Time +func (cmd *Command) Timestamp(name string) time.Time Timestamp gets the timestamp from a flag name func (cmd *Command) ToFishCompletion() (string, error) diff --git a/testdata/godoc-v3.x.txt b/testdata/godoc-v3.x.txt index 5229697ea0..f39be4666b 100644 --- a/testdata/godoc-v3.x.txt +++ b/testdata/godoc-v3.x.txt @@ -456,7 +456,7 @@ func (cmd *Command) StringSlice(name string) []string StringSlice looks up the value of a local StringSliceFlag, returns nil if not found -func (cmd *Command) Timestamp(name string) *time.Time +func (cmd *Command) Timestamp(name string) time.Time Timestamp gets the timestamp from a flag name func (cmd *Command) ToFishCompletion() (string, error)