Skip to content

Commit

Permalink
fix range initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
furusax0621 committed Dec 24, 2022
1 parent 57cbf07 commit 9467fee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
4 changes: 1 addition & 3 deletions internal/getters/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ type RandomDateInRange struct {
}

func (r *RandomDateInRange) Value() interface{} {
rand.Seed(time.Now().UnixNano())
var randomSeconds int64
randomSeconds = rand.Int63n(oneYear) + rand.Int63n(100)
randomSeconds := rand.Int63n(oneYear) + rand.Int63n(100)
d := time.Now().Add(-1 * time.Duration(randomSeconds) * time.Second)
return d
}
Expand Down
12 changes: 6 additions & 6 deletions internal/getters/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

type RandomDateTimeInRange struct {
name string
min string
max string
allowNull bool
}

// Value returns a random time.Time in the range specified by the New method
func (r *RandomDateTimeInRange) Value() interface{} {
rand.Seed(time.Now().UnixNano())
randomSeconds := rand.Int63n(oneYear)
d := time.Now().Add(-1 * time.Duration(randomSeconds) * time.Second)
return d
Expand All @@ -32,15 +32,15 @@ func (r *RandomDateTimeInRange) Quote() string {
}

// NewRandomDateTimeInRange returns a new random date in the specified range
func NewRandomDateTimeInRange(name string, min, max string, allowNull bool) *RandomDateInRange {
func NewRandomDateTimeInRange(name string, min, max string, allowNull bool) *RandomDateTimeInRange {
if min == "" {
t := time.Now().Add(-1 * time.Duration(oneYear) * time.Second)
min = t.Format("2006-01-02")
min = t.Format("2006-01-02 15:03:04")
}
return &RandomDateInRange{name, min, max, allowNull}
return &RandomDateTimeInRange{name, min, max, allowNull}
}

// NewRandomDateTime returns a new random datetime between Now() and Now() - 1 year
func NewRandomDateTime(name string, allowNull bool) *RandomDateInRange {
return &RandomDateInRange{name, "", "", allowNull}
func NewRandomDateTime(name string, allowNull bool) *RandomDateTimeInRange {
return &RandomDateTimeInRange{name, "", "", allowNull}
}
9 changes: 9 additions & 0 deletions internal/getters/getters.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package getters

import (
"math/rand"
"time"
)

// All types defined here satisfy the Getter interface
// type Getter interface {
// Value() interface{}
Expand All @@ -12,3 +17,7 @@ const (
oneYear = int64(60 * 60 * 24 * 365)
NULL = "NULL"
)

func init() {
rand.Seed(time.Now().UnixMicro())
}

0 comments on commit 9467fee

Please sign in to comment.