Skip to content

Commit

Permalink
Merge pull request #71 from grafana/56quarters/flagext-tz
Browse files Browse the repository at this point in the history
Use UTC explicitly for flagext.DayValue
  • Loading branch information
56quarters committed Nov 3, 2021
2 parents 72fa100 + 07135d7 commit 4e78497
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog

* [CHANGE] Flagext: `DayValue` now always uses UTC when parsing or displaying dates. #71
* [CHANGE] Closer: remove the closer package since it's trivial to just copy/paste. #70
* [CHANGE] Memberlist: allow specifying address and port advertised to the memberlist cluster members by setting the following configuration: #37
* `-memberlist.advertise_addr`
Expand Down
6 changes: 3 additions & 3 deletions flagext/day.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func NewDayValue(t model.Time) DayValue {

// String implements flag.Value
func (v DayValue) String() string {
return v.Time.Time().Format(time.RFC3339)
return v.Time.Time().UTC().Format(time.RFC3339)
}

// Set implements flag.Value
func (v *DayValue) Set(s string) error {
t, err := time.Parse("2006-01-02", s)
t, err := time.ParseInLocation("2006-01-02", s, time.UTC)
if err != nil {
return err
}
Expand All @@ -55,5 +55,5 @@ func (v *DayValue) UnmarshalYAML(unmarshal func(interface{}) error) error {

// MarshalYAML implements yaml.Marshaler.
func (v DayValue) MarshalYAML() (interface{}, error) {
return v.Time.Time().Format("2006-01-02"), nil
return v.Time.Time().UTC().Format("2006-01-02"), nil
}
10 changes: 4 additions & 6 deletions flagext/day_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
)

func TestDayValueYAML(t *testing.T) {
// Test embedding of DayValue.
{
t.Run("embedding DayValue", func(t *testing.T) {
type TestStruct struct {
Day DayValue `yaml:"day"`
}
Expand All @@ -28,10 +27,9 @@ func TestDayValueYAML(t *testing.T) {
err = yaml.Unmarshal(expected, &actualStruct)
require.NoError(t, err)
assert.Equal(t, testStruct, actualStruct)
}
})

// Test pointers of DayValue.
{
t.Run("pointer of DayValue", func(t *testing.T) {
type TestStruct struct {
Day *DayValue `yaml:"day"`
}
Expand All @@ -50,5 +48,5 @@ func TestDayValueYAML(t *testing.T) {
err = yaml.Unmarshal(expected, &actualStruct)
require.NoError(t, err)
assert.Equal(t, testStruct, actualStruct)
}
})
}

0 comments on commit 4e78497

Please sign in to comment.