Skip to content

Commit e8c2eb5

Browse files
committed
Fix a year-less timestamp layout losing January 1
timestampValue.Set decides "this layout has no date" by comparing the parsed instant against defaultTS, a probe parsed from time.TimeOnly that lands on year-0 January 1. A layout that does carry a date parses "01-01" onto that same day, so the two cases are indistinguishable and the date-less branch overwrites the month and day with time.Now(). Layouts: []string{"01-02"} --time 01-01 before: 2026-09-08 after: 2026-01-01 --time 01-02 2026-01-02 both --time 03-04 2026-03-04 both Ask the layout instead of the parsed value. The next branch already handles "has a date, no year" correctly for every other day of the year.
1 parent 5081218 commit e8c2eb5

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎flag_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,36 @@ func TestTimestampFlagApply_ShortenedLayouts(t *testing.T) {
27182718
}
27192719
}
27202720

2721+
func TestTimestampFlagApply_YearlessLayoutJanuaryFirst(t *testing.T) {
2722+
year := time.Now().UTC().Year()
2723+
2724+
for _, tc := range []struct {
2725+
layout string
2726+
value string
2727+
expected time.Time
2728+
}{
2729+
{layout: "01-02", value: "01-01", expected: time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)},
2730+
{layout: "01-02", value: "03-04", expected: time.Date(year, time.March, 4, 0, 0, 0, 0, time.UTC)},
2731+
{layout: "Jan _2 15:04:05", value: "Jan 1 06:07:08", expected: time.Date(year, time.January, 1, 6, 7, 8, 0, time.UTC)},
2732+
} {
2733+
t.Run(tc.layout+" "+tc.value, func(t *testing.T) {
2734+
var got time.Time
2735+
cmd := &Command{
2736+
Flags: []Flag{
2737+
&TimestampFlag{Name: "time", Config: TimestampConfig{Layouts: []string{tc.layout}}},
2738+
},
2739+
Action: func(_ context.Context, cmd *Command) error {
2740+
got = cmd.Timestamp("time")
2741+
return nil
2742+
},
2743+
}
2744+
2745+
assert.NoError(t, cmd.Run(buildTestContext(t), []string{"", "--time", tc.value}))
2746+
assert.Equal(t, tc.expected, got)
2747+
})
2748+
}
2749+
}
2750+
27212751
func TestTimestampFlagApplyValue(t *testing.T) {
27222752
expectedResult, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
27232753
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Config: TimestampConfig{Layouts: []string{time.RFC3339}}, Value: expectedResult}

‎flag_timestamp.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (t *timestampValue) Set(value string) error {
6363
return errors.New("got nil/empty layouts slice")
6464
}
6565

66+
matchedLayout := ""
6667
for _, layout := range t.layouts {
6768
var locErr error
6869

@@ -78,6 +79,7 @@ func (t *timestampValue) Set(value string) error {
7879
}
7980

8081
err = nil
82+
matchedLayout = layout
8183
break
8284
}
8385

@@ -90,7 +92,8 @@ func (t *timestampValue) Set(value string) error {
9092
n := time.Now().In(timestamp.Location())
9193

9294
// If format is missing date (or year only), set it explicitly to current
93-
if timestamp.Truncate(time.Hour*24).UnixNano() == defaultTS.Truncate(time.Hour*24).UnixNano() {
95+
// A layout that carries a date parses "January 1" to the same instant a date-less layout does, so ask the layout.
96+
if !layoutHasDate(matchedLayout) && timestamp.Truncate(time.Hour*24).UnixNano() == defaultTS.Truncate(time.Hour*24).UnixNano() {
9497
timestamp = time.Date(
9598
n.Year(),
9699
n.Month(),
@@ -121,6 +124,14 @@ func (t *timestampValue) Set(value string) error {
121124
return nil
122125
}
123126

127+
// layoutHasDate reports whether layout carries month/day components, probed by
128+
// round-tripping a reference date that is not January 1.
129+
func layoutHasDate(layout string) bool {
130+
ref := time.Date(2, time.March, 4, 5, 6, 7, 0, time.UTC)
131+
p, err := time.Parse(layout, ref.Format(layout))
132+
return err == nil && (p.Month() != time.January || p.Day() != 1)
133+
}
134+
124135
// String returns a readable representation of this value (for usage defaults)
125136
func (t *timestampValue) String() string {
126137
return fmt.Sprintf("%v", t.timestamp)

0 commit comments

Comments
 (0)