Skip to content

Commit

Permalink
add UnixZero
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Gonzalez <[email protected]>
  • Loading branch information
bg451 committed Mar 14, 2023
1 parent 3595643 commit 397e96b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import (
"go.mongodb.org/mongo-driver/bson/bsontype"
)

var (
// UnixZero sets the zero unix timestamp we want to compare against.
// Unix 0 for an EST timezone is not equivalent to a UTC timezone.
UnixZero = time.Unix(0, 0).UTC()
)

func init() {
dt := DateTime{}
Default.Add("datetime", &dt, IsDateTime)
Expand Down Expand Up @@ -128,6 +134,11 @@ func (t DateTime) IsZero() bool {
return time.Time(t).IsZero()
}

// IsUnixZerom returns whether the date time is equivalent to time.Unix(0, 0).UTC().
func (t DateTime) IsUnixZero() bool {
return time.Time(t) == UnixZero
}

// MarshalText implements the text marshaller interface
func (t DateTime) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
Expand Down
11 changes: 11 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ func TestIsZero(t *testing.T) {
assert.False(t, NewDateTime().IsZero())
}

func TestIsUnixZero(t *testing.T) {
dt := NewDateTime()
assert.True(t, dt.IsUnixZero())
assert.NotEqual(t, dt.IsZero(), dt.IsUnixZero())
// Test configuring UnixZero
estLocation := time.FixedZone("EST", int((-5 * time.Hour).Seconds()))
estUnixZero := time.Unix(0, 0).In(estLocation)
UnixZero = estUnixZero
assert.True(t, DateTime(estUnixZero).IsUnixZero())
}

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

0 comments on commit 397e96b

Please sign in to comment.