Skip to content

Commit

Permalink
Adds IsZero functionality to DateTime
Browse files Browse the repository at this point in the history
This PR adds a new function `IsZero` to the DateTime struct. The goal of
this is to satisfy a common interface, IsZeroer, used by go-yaml/yaml
marshaller. `IsZero` already exists on the time.Time object, so this
does not introduce any novel implementation.

For reference, here is the [documentation
note](https://pkg.go.dev/gopkg.in/yaml.v3#Marshal) on using IsZero by the
`(yaml).Marshal` function.

Signed-off-by: Brandon Gonzalez <bg@chronosphere.io>
  • Loading branch information
bg451 committed Mar 2, 2023
1 parent a239ff1 commit d203a72
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ func (t DateTime) String() string {
return NormalizeTimeForMarshal(time.Time(t)).Format(MarshalFormat)
}

// IsZero returns whether the date time is a zero value
func (t DateTime) IsZero() bool {
return time.Time(t).IsZero()
}

// MarshalText implements the text marshaller interface
func (t DateTime) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
Expand Down
10 changes: 10 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func TestNewDateTime(t *testing.T) {
assert.EqualValues(t, time.Unix(0, 0).UTC(), NewDateTime())
}

func TestIsZero(t *testing.T) {
var empty DateTime
assert.True(t, empty.IsZero())
assert.False(t, DateTime(time.Unix(100, 5)).IsZero())

// time.Unix(0,0) does not produce a true zero value struct,
// so this is expected to fail.
assert.False(t, NewDateTime().IsZero())
}

func TestParseDateTime_errorCases(t *testing.T) {
_, err := ParseDateTime("yada")
assert.Error(t, err)
Expand Down

0 comments on commit d203a72

Please sign in to comment.