Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions iso8601/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"fmt"
"math"
"time"

"github.com/Code-Hex/synchro/internal/constraints"
)
Expand All @@ -24,6 +25,15 @@ var _ interface {
encoding.TextUnmarshaler
} = (*Time)(nil)

// TimeOf returns the Time representing the time of day in which a time occurs
// in that time's location. It ignores the date.
func TimeOf(t time.Time) Time {
var tm Time
tm.Hour, tm.Minute, tm.Second = t.Clock()
tm.Nanosecond = t.Nanosecond()
return tm
}

// String returns the ISO8601 string representation of the format "hh:mm:dd".
// For example: "12:59:59.123456789".
func (t Time) String() string {
Expand All @@ -39,6 +49,25 @@ func (t Time) IsZero() bool {
return (t.Hour == 0) && (t.Minute == 0) && (t.Second == 0) && (t.Nanosecond == 0)
}

// Before reports whether t occurs before t2.
func (t Time) Before(t2 Time) bool {
if t.Hour != t2.Hour {
return t.Hour < t2.Hour
}
if t.Minute != t2.Minute {
return t.Minute < t2.Minute
}
if t.Second != t2.Second {
return t.Second < t2.Second
}
return t.Nanosecond < t2.Nanosecond
}

// After reports whether t occurs after t2.
func (t Time) After(t2 Time) bool {
return t2.Before(t)
}

// MarshalText implements the encoding.TextMarshaler interface.
// The output is the result of t.String().
func (t Time) MarshalText() ([]byte, error) {
Expand Down
48 changes: 48 additions & 0 deletions iso8601/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,51 @@ func TestTimeJSON(t *testing.T) {
})
}
}

func TestTimeOf(t *testing.T) {
for _, test := range []struct {
time time.Time
want Time
}{
{time.Date(2014, 8, 20, 15, 8, 43, 1, time.Local), Time{15, 8, 43, 1}},
{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC), Time{0, 0, 0, 0}},
} {
if got := TimeOf(test.time); got != test.want {
t.Errorf("TimeOf(%v) = %+v, want %+v", test.time, got, test.want)
}
}
}

func TestTimeBefore(t *testing.T) {
for _, test := range []struct {
t1, t2 Time
want bool
}{
{Time{12, 0, 0, 0}, Time{14, 0, 0, 0}, true},
{Time{12, 20, 0, 0}, Time{12, 30, 0, 0}, true},
{Time{12, 20, 10, 0}, Time{12, 20, 20, 0}, true},
{Time{12, 20, 10, 5}, Time{12, 20, 10, 10}, true},
{Time{12, 20, 10, 5}, Time{12, 20, 10, 5}, false},
} {
if got := test.t1.Before(test.t2); got != test.want {
t.Errorf("%v.Before(%v): got %t, want %t", test.t1, test.t2, got, test.want)
}
}
}

func TestTimeAfter(t *testing.T) {
for _, test := range []struct {
t1, t2 Time
want bool
}{
{Time{12, 0, 0, 0}, Time{14, 0, 0, 0}, false},
{Time{12, 20, 0, 0}, Time{12, 30, 0, 0}, false},
{Time{12, 20, 10, 0}, Time{12, 20, 20, 0}, false},
{Time{12, 20, 10, 5}, Time{12, 20, 10, 10}, false},
{Time{12, 20, 10, 5}, Time{12, 20, 10, 5}, false},
} {
if got := test.t1.After(test.t2); got != test.want {
t.Errorf("%v.After(%v): got %t, want %t", test.t1, test.t2, got, test.want)
}
}
}