Skip to content

Commit ad394bd

Browse files
authored
DateTime and the different zero values used. (#148)
* DateTime and the different zero values used. * fixed the documentation for NewDateTime() to state explicitly that it does not yield the zero value. This value is checked against IsUNIXZero(). * introduced a new method MakeDateTime() for users who want a zero value that checks against IsZero(). * fixes #141 Signed-off-by: Frederic BIDON <[email protected]> * addressed review comments Signed-off-by: Frederic BIDON <[email protected]> --------- Signed-off-by: Frederic BIDON <[email protected]>
1 parent c8413cf commit ad394bd

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

time.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import (
2929
)
3030

3131
var (
32-
// UnixZero sets the zero unix timestamp we want to compare against.
32+
// UnixZero sets the zero unix UTC timestamp we want to compare against.
33+
//
3334
// Unix 0 for an EST timezone is not equivalent to a UTC timezone.
3435
UnixZero = time.Unix(0, 0).UTC()
3536
)
@@ -121,19 +122,31 @@ func ParseDateTime(data string) (DateTime, error) {
121122
return DateTime{}, lastError
122123
}
123124

124-
// DateTime is a time but it serializes to ISO8601 format with millis
125+
// DateTime is a time but it serializes to ISO8601 format with millis.
126+
//
125127
// It knows how to read 3 different variations of a RFC3339 date time.
126128
// Most APIs we encounter want either millisecond or second precision times.
127129
// This just tries to make it worry-free.
128130
//
129131
// swagger:strfmt date-time
130132
type DateTime time.Time
131133

132-
// NewDateTime is a representation of zero value for DateTime type
134+
// NewDateTime is a representation of the UNIX epoch (January 1, 1970 00:00:00 UTC) for the [DateTime] type.
135+
//
136+
// Notice that this is not the zero value of the [DateTime] type.
137+
//
138+
// You may use [DateTime.IsUNIXZero] to check against this value.
133139
func NewDateTime() DateTime {
134140
return DateTime(time.Unix(0, 0).UTC())
135141
}
136142

143+
// MakeDateTime is a representation of the zero value of the [DateTime] type (January 1, year 1, 00:00:00 UTC).
144+
//
145+
// You may use [Datetime.IsZero] to check against this value.
146+
func MakeDateTime() DateTime {
147+
return DateTime(time.Time{})
148+
}
149+
137150
// String converts this time to a string
138151
func (t DateTime) String() string {
139152
return NormalizeTimeForMarshal(time.Time(t)).Format(MarshalFormat)

time_test.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,24 @@ func TestNewDateTime(t *testing.T) {
5656
}
5757

5858
func TestIsZero(t *testing.T) {
59-
var empty DateTime
60-
assert.True(t, empty.IsZero())
61-
assert.False(t, DateTime(time.Unix(100, 5)).IsZero())
62-
63-
// time.Unix(0,0) does not produce a true zero value struct,
64-
// so this is expected to fail.
65-
assert.False(t, NewDateTime().IsZero())
59+
t.Run("time.Unix(100,5) should not be zero", func(t *testing.T) {
60+
assert.False(t, DateTime(time.Unix(100, 5)).IsZero())
61+
})
62+
63+
t.Run("NewDateTime() should not be zero", func(t *testing.T) {
64+
// time.Unix(0,0) does not produce a true zero value struct,
65+
// so this is expected to fail.
66+
assert.False(t, NewDateTime().IsZero())
67+
})
68+
69+
t.Run("MakeDateTime() should be zero", func(t *testing.T) {
70+
assert.True(t, MakeDateTime().IsZero())
71+
})
72+
73+
t.Run("empty DateTime should be zero", func(t *testing.T) {
74+
dt := DateTime{}
75+
assert.True(t, dt.IsZero())
76+
})
6677
}
6778

6879
func TestIsUnixZero(t *testing.T) {

0 commit comments

Comments
 (0)