Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
Add Before/After/Equal to UnixNano
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Artoul committed Oct 26, 2017
1 parent 74f8fce commit b6d0744
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions time/unix_nano.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ func (u UnixNano) ToTime() time.Time {
func ToUnixNano(t time.Time) UnixNano {
return UnixNano(t.UnixNano())
}

// Before reports whether the time instant u is before t.
func (u UnixNano) Before(t UnixNano) bool {
return u < t
}

// After reports whether the time instant u is after t.
func (u UnixNano) After(t UnixNano) bool {
return u > t
}

// Equal reports whether the time instant u is equal to t.
func (u UnixNano) Equal(t UnixNano) bool {
return u == t
}
28 changes: 28 additions & 0 deletions time/unix_nano_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -33,3 +34,30 @@ func TestUnixNano(t *testing.T) {
require.Equal(t, UnixNano(1000), unixNano)
require.Equal(t, time, unixNano.ToTime())
}

func TestUnixNanoBefore(t *testing.T) {
t0 := UnixNano(0)
t1 := UnixNano(1)

assert.Equal(t, true, t0.Before(t1))
assert.Equal(t, false, t1.Before(t0))
assert.Equal(t, false, t0.Before(t0))
}

func TestUnixNanoAfter(t *testing.T) {
t0 := UnixNano(0)
t1 := UnixNano(1)

assert.Equal(t, false, t0.After(t1))
assert.Equal(t, true, t1.After(t0))
assert.Equal(t, false, t0.After(t0))
}

func TestUnixNanoEqual(t *testing.T) {
t0 := UnixNano(0)
t1 := UnixNano(1)

assert.Equal(t, false, t0.Equal(t1))
assert.Equal(t, false, t1.Equal(t0))
assert.Equal(t, true, t0.Equal(t0))
}

0 comments on commit b6d0744

Please sign in to comment.