Skip to content

Commit 8fd2a71

Browse files
authored
Merge pull request #2450 from breken-ai/fix/timestamp-time-only-timezone
fix: date a time-only timestamp today when Timezone is not UTC Tested-by: Eng Zer Jun <[email protected]> Reviewed-by: Eng Zer Jun <[email protected]>
2 parents a3f13fc + 343114e commit 8fd2a71

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎flag_test.go‎

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

2721+
func TestTimestampFlagApply_TimeOnlyLayoutTimezoned(t *testing.T) {
2722+
for _, loc := range []*time.Location{
2723+
time.FixedZone("UTC-5", -5*60*60),
2724+
time.FixedZone("UTC+2", 2*60*60),
2725+
} {
2726+
for _, value := range []string{"01:00", "12:00", "20:00"} {
2727+
t.Run(loc.String()+" "+value, func(t *testing.T) {
2728+
var got time.Time
2729+
cmd := &Command{
2730+
Flags: []Flag{
2731+
&TimestampFlag{Name: "time", Config: TimestampConfig{Layouts: []string{"15:04"}, Timezone: loc}},
2732+
},
2733+
Action: func(_ context.Context, cmd *Command) error {
2734+
got = cmd.Timestamp("time")
2735+
return nil
2736+
},
2737+
}
2738+
2739+
assert.NoError(t, cmd.Run(buildTestContext(t), []string{"", "--time", value}))
2740+
2741+
clock, err := time.Parse("15:04", value)
2742+
assert.NoError(t, err)
2743+
now := time.Now().In(loc)
2744+
expected := time.Date(now.Year(), now.Month(), now.Day(), clock.Hour(), clock.Minute(), 0, 0, loc)
2745+
assert.Equal(t, expected, got)
2746+
})
2747+
}
2748+
}
2749+
}
2750+
2751+
func TestTimestampFlagApply_TimeOnlyLayoutWithOffset(t *testing.T) {
2752+
var got time.Time
2753+
cmd := &Command{
2754+
Flags: []Flag{
2755+
&TimestampFlag{Name: "time", Config: TimestampConfig{Layouts: []string{"15:04 -0700"}}},
2756+
},
2757+
Action: func(_ context.Context, cmd *Command) error {
2758+
got = cmd.Timestamp("time")
2759+
return nil
2760+
},
2761+
}
2762+
2763+
assert.NoError(t, cmd.Run(buildTestContext(t), []string{"", "--time", "20:00 -0500"}))
2764+
2765+
loc := time.FixedZone("", -5*60*60)
2766+
now := time.Now().In(loc)
2767+
assert.Equal(t, time.Date(now.Year(), now.Month(), now.Day(), 20, 0, 0, 0, loc), got)
2768+
}
2769+
27212770
func TestTimestampFlagApply_YearlessLayoutJanuaryFirst(t *testing.T) {
27222771
year := time.Now().UTC().Year()
27232772

‎flag_timestamp.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ func (t *timestampValue) Set(value string) error {
8787
return err
8888
}
8989

90-
defaultTS, _ := time.ParseInLocation(time.TimeOnly, time.TimeOnly, timestamp.Location())
91-
9290
n := time.Now().In(timestamp.Location())
9391

9492
// If format is missing date (or year only), set it explicitly to current
9593
// 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() {
94+
// The date is compared in the timestamp's own location: truncating the
95+
// instant to a day works in UTC and misses January 1 of year 0 for part
96+
// of the day whenever Timezone is not UTC.
97+
if !layoutHasDate(matchedLayout) && timestamp.Year() == 0 && timestamp.YearDay() == 1 {
9798
timestamp = time.Date(
9899
n.Year(),
99100
n.Month(),

0 commit comments

Comments
 (0)