Skip to content

Commit

Permalink
use time.Time instead of func() time.Time
Browse files Browse the repository at this point in the history
  • Loading branch information
rhino1998 committed Sep 23, 2024
1 parent 70a90a4 commit 3111bb1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
4 changes: 2 additions & 2 deletions configtype/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func (t *Time) MarshalJSON() ([]byte, error) {
}
}

func (t Time) Time(nowFunc func() time.Time) time.Time {
func (t Time) Time(now time.Time) time.Time {
switch t.typ {
case timeTypeFixed:
return t.time
case timeTypeRelative:
return nowFunc().Add(t.duration)
return now.Add(t.duration)
default:
return time.Time{}
}
Expand Down
11 changes: 4 additions & 7 deletions configtype/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ import (
)

func TestTime(t *testing.T) {
nowTime, _ := time.Parse(time.RFC3339Nano, time.RFC3339Nano)
now := func() time.Time {
return nowTime
}
now, _ := time.Parse(time.RFC3339Nano, time.RFC3339Nano)

cases := []struct {
give string
want time.Time
}{
{"1ns", nowTime.Add(1 * time.Nanosecond)},
{"20s", nowTime.Add(20 * time.Second)},
{"-50m30s", nowTime.Add(-50*time.Minute - 30*time.Second)},
{"1ns", now.Add(1 * time.Nanosecond)},
{"20s", now.Add(20 * time.Second)},
{"-50m30s", now.Add(-50*time.Minute - 30*time.Second)},
{"2021-09-01T00:00:00Z", time.Date(2021, 9, 1, 0, 0, 0, 0, time.UTC)},
{"2021-09-01T00:00:00.123Z", time.Date(2021, 9, 1, 0, 0, 0, 123000000, time.UTC)},
{"2021-09-01T00:00:00.123456Z", time.Date(2021, 9, 1, 0, 0, 0, 123456000, time.UTC)},
Expand Down

0 comments on commit 3111bb1

Please sign in to comment.