Skip to content

Commit

Permalink
feat: Add function to compare two time instances
Browse files Browse the repository at this point in the history
- Add a function that compares two time instances and returns true if the difference is less than a specified duration.
- Write tests to test the time comparison function and specifically cover scenarios with milliseconds.
  • Loading branch information
Laisky committed Jan 9, 2024
1 parent 8f7e21f commit a96184e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func ParseHex2UTC(ts string) (t time.Time, err error) {
return ParseUnix2UTC(ut), nil
}

// TimeEqual compare two time with difference,
// return true if time difference less than difference
func TimeEqual(ts1, ts2 time.Time, difference time.Duration) bool {
sub := ts1.Sub(ts2)
return sub < difference && sub > -difference
}

// ParseHexNano2UTC parse hex contains nano to UTC time
func ParseHexNano2UTC(ts string) (t time.Time, err error) {
var ut int64
Expand Down
62 changes: 62 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ func TestParseTs2String(t *testing.T) {
}
}

func TestTimeCompare(t *testing.T) {
str1 := "2019-10-12T02:03:14Z"
str2 := "2019-10-12T02:03:14.123Z"

ts1, err := time.Parse(time.RFC3339, str1)
require.NoError(t, err)
ts2, err := time.Parse(time.RFC3339, str2)
require.NoError(t, err)

// ⚠️ Even though RFC3339 doesn't support milliseconds,
// if the string has a decimal point, the parsing result
// will actually include the millisecond automatically
require.False(t, ts1.Equal(ts2))
require.True(t, ts2.After(ts1))
}

func TestParseUnix2UTC(t *testing.T) {
ut := int64(1570845794)
ts := ParseUnix2UTC(ut).Format(time.RFC3339)
Expand Down Expand Up @@ -368,3 +384,49 @@ func TestSleepWithContext(t *testing.T) {
require.Greater(t, time.Since(startAt), time.Millisecond*10)
})
}

func TestTimeEqual(t *testing.T) {
t.Parallel()

t.Run("eq: no timezone", func(t *testing.T) {
t.Parallel()
str1 := "2019-10-12T02:03:14Z"
str2 := "2019-10-12T02:03:14.123Z"

t1, err := time.Parse(time.RFC3339, str1)
require.NoError(t, err)
t2, err := time.Parse(time.RFC3339, str2)
require.NoError(t, err)

require.False(t, t1.Equal(t2))
require.True(t, TimeEqual(t1, t2, time.Second))
})

t.Run("ne: no timezone", func(t *testing.T) {
t.Parallel()
str1 := "2019-10-12T02:03:14Z"
str2 := "2019-10-12T02:03:15.123Z"

t1, err := time.Parse(time.RFC3339, str1)
require.NoError(t, err)
t2, err := time.Parse(time.RFC3339, str2)
require.NoError(t, err)

require.False(t, t1.Equal(t2))
require.False(t, TimeEqual(t1, t2, time.Second))
})

t.Run("eq: in different timezone", func(t *testing.T) {
t.Parallel()
str1 := "2019-10-12T02:03:14+07:00"
str2 := "2019-10-12T03:03:14.123+08:00"

t1, err := time.Parse(time.RFC3339, str1)
require.NoError(t, err)
t2, err := time.Parse(time.RFC3339, str2)
require.NoError(t, err)

require.False(t, t1.Equal(t2))
require.True(t, TimeEqual(t1, t2, time.Second))
})
}

0 comments on commit a96184e

Please sign in to comment.