diff --git a/flag_test.go b/flag_test.go index 465750df78..2e01766ecf 100644 --- a/flag_test.go +++ b/flag_test.go @@ -3459,6 +3459,28 @@ func TestTimestampFlagApply_WithDestination(t *testing.T) { expect(t, *fl.Destination.timestamp, expectedResult) } +func TestTimestampFlagApply_EnvWithDestination(t *testing.T) { + var tsValue Timestamp + + app := NewApp() + app.Flags = []Flag{ + &TimestampFlag{ + Name: "some-timestamp-flag", + EnvVars: []string{"SOME_TIMESTAMP_FLAG"}, + Layout: time.RFC3339, + Destination: &tsValue, + Required: true, + }, + } + + t.Setenv("SOME_TIMESTAMP_FLAG", "2021-03-02T06:20:00Z") + + err := app.Run([]string{}) + + expect(t, err, nil) + expect(t, tsValue.Value().Format(time.RFC3339), "2021-03-02T06:20:00Z") +} + // Test issue #1254 // StringSlice() with UseShortOptionHandling causes duplicated entries, depending on the ordering of the flags func TestSliceShortOptionHandle(t *testing.T) { diff --git a/flag_timestamp.go b/flag_timestamp.go index fa0671fe4e..83f750a319 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -145,11 +145,6 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error { f.defaultValue = f.Value.clone() - if f.Destination != nil { - f.Destination.SetLayout(f.Layout) - f.Destination.SetLocation(f.Timezone) - } - if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if err := f.Value.Set(val); err != nil { return fmt.Errorf("could not parse %q as timestamp value from %s for flag %s: %s", val, source, f.Name, err) @@ -157,6 +152,10 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error { f.HasBeenSet = true } + if f.Destination != nil { + *f.Destination = *f.Value + } + for _, name := range f.Names() { if f.Destination != nil { set.Var(f.Destination, name, f.Usage)