Skip to content

Commit

Permalink
feat: pointer time to int64 (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: liutong.eric <liutong.eric@bytedance.com>
  • Loading branch information
BooksLiu and liutong.eric committed Nov 16, 2021
1 parent b4f8c13 commit 31a4089
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ func PointerInt64ToTime(i *int64) *time.Time {
}
return Time(time.Unix(*i, 0))
}

func PointerTimeToInt64(i *time.Time) *int64 {
if i == nil {
return nil
}
return Int64(i.Unix())
}
28 changes: 28 additions & 0 deletions p2p_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ptr

import (
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -36,3 +38,29 @@ func TestPointerInt64ToTime(t *testing.T) {
})
}
}

func TestPointerTimeToInt64(t *testing.T) {
tests := []struct {
name string
args *time.Time
want *int64
}{
{
name: "nil time to nil int64",
args: nil,
want: nil,
},
{
name: "pointer time to pointer int64",
args: Time(time.Unix(24580, 0)),
want: Int64(24580),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := PointerTimeToInt64(tt.args); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PointerTimeToInt64() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 31a4089

Please sign in to comment.