Skip to content

Instantly share code, notes, and snippets.

@lamdor
Created July 31, 2012 21:31
Show Gist options
  • Select an option

  • Save lamdor/3220744 to your computer and use it in GitHub Desktop.

Select an option

Save lamdor/3220744 to your computer and use it in GitHub Desktop.
implicit val arbitraryDate: Arbitrary[DateTime] = Arbitrary {
val gmt = DateTimeZone.UTC
for {
year <- Gen.choose(1970, 2050)
month <- Gen.choose(1, 12)
day <- Gen.choose(1, 28) // TODO: there is a better way to get the number of days in a month -- see: http://www.exampledepot.com/egs/java.util/GetDaysInMonth.html
hour <- Gen.choose(0, 23)
minute <- Gen.choose(0, 59)
second <- Gen.choose(0, 59)
millis <- Gen.choose(0, 999)
} yield new DateTime(year, month, day, hour, minute, second, millis, gmt)
}
def arbitraryDatesWithinDayRange(dayRange: Seq[Double]) = Arbitrary {
for {
since <- Arbitrary.arbitrary[DateTime]
daysAfter <- Gen.oneOf(dayRange)
} yield Pair(since, since.plusMinutes((daysAfter * 24 * 60).toInt))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment