github.com/leanovate/gopter@v0.2.9/gen/time.go (about)

     1  package gen
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/leanovate/gopter"
     7  )
     8  
     9  // Time generates an arbitrary time.Time within year [0, 9999]
    10  func Time() gopter.Gen {
    11  	return func(genParams *gopter.GenParameters) *gopter.GenResult {
    12  		sec := genParams.Rng.Int63n(253402214400) // Ensure year in [0, 9999]
    13  		usec := genParams.Rng.Int63n(1000000000)
    14  
    15  		return gopter.NewGenResult(time.Unix(sec, usec), TimeShrinker)
    16  	}
    17  }
    18  
    19  // AnyTime generates an arbitrary time.Time struct (might be way out of bounds of any reason)
    20  func AnyTime() gopter.Gen {
    21  	return func(genParams *gopter.GenParameters) *gopter.GenResult {
    22  		sec := genParams.NextInt64()
    23  		usec := genParams.NextInt64()
    24  
    25  		return gopter.NewGenResult(time.Unix(sec, usec), TimeShrinker)
    26  	}
    27  }
    28  
    29  // TimeRange generates an arbitrary time.Time with a range
    30  // from defines the start of the time range
    31  // duration defines the overall duration of the time range
    32  func TimeRange(from time.Time, duration time.Duration) gopter.Gen {
    33  	return func(genParams *gopter.GenParameters) *gopter.GenResult {
    34  		v := from.Add(time.Duration(genParams.Rng.Int63n(int64(duration))))
    35  		return gopter.NewGenResult(v, TimeShrinker)
    36  	}
    37  }