Skip to content

Commit 669a7d7

Browse files
authored
Merge pull request #830 from armanbilge/js-time
2 parents 58d940f + ab6fdef commit 669a7d7

File tree

4 files changed

+675
-0
lines changed

4 files changed

+675
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*-------------------------------------------------------------------------*\
2+
** ScalaCheck **
3+
** Copyright (c) 2007-2021 Rickard Nilsson. All rights reserved. **
4+
** http://www.scalacheck.org **
5+
** **
6+
** This software is released under the terms of the Revised BSD License. **
7+
** There is NO WARRANTY. See the file LICENSE for the full text. **
8+
\*------------------------------------------------------------------------ */
9+
10+
package org.scalacheck.time
11+
12+
import org.scalacheck._
13+
import java.time._
14+
15+
/** [[Arbitrary]] instances for `java.time` types.
16+
*
17+
* @note [[Arbitrary]] instances for `java.time` types which are Java enum
18+
* types are provided by ScalaCheck's general Java enum support.
19+
*/
20+
private[scalacheck] trait JavaTimeArbitrary {
21+
22+
// Duration
23+
24+
// Java duration values are conceptually infinite, thus they do not expose
25+
// Duration.MAX/Duration.MIN values, but in practice they are finite,
26+
// restricted by their underlying representation a long and an int.
27+
28+
implicit final lazy val arbJavaDuration: Arbitrary[Duration] = {
29+
val minJavaDuration = Duration.ofSeconds(Long.MinValue)
30+
val maxJavaDuration = Duration.ofSeconds(Long.MaxValue, 999999999L)
31+
Arbitrary(Gen.choose(minJavaDuration, maxJavaDuration))
32+
}
33+
34+
// Instant
35+
36+
implicit final lazy val arbInstant: Arbitrary[Instant] =
37+
Arbitrary(Gen.choose(Instant.MIN, Instant.MAX))
38+
39+
// Year
40+
41+
implicit final lazy val arbYear: Arbitrary[Year] =
42+
Arbitrary(Gen.choose(Year.of(Year.MIN_VALUE), Year.of(Year.MAX_VALUE)))
43+
44+
// LocalDate
45+
46+
implicit final lazy val arbLocalDate: Arbitrary[LocalDate] =
47+
Arbitrary(Gen.choose(LocalDate.MIN, LocalDate.MAX))
48+
49+
// LocalTime
50+
51+
implicit final lazy val arbLocalTime: Arbitrary[LocalTime] =
52+
Arbitrary(Gen.choose(LocalTime.MIN, LocalTime.MAX))
53+
54+
// LocalDateTime
55+
56+
implicit final lazy val arbLocalDateTime: Arbitrary[LocalDateTime] =
57+
Arbitrary(Gen.choose(LocalDateTime.MIN, LocalDateTime.MAX))
58+
59+
// MonthDay
60+
61+
implicit final lazy val arbMonthDay: Arbitrary[MonthDay] =
62+
Arbitrary(Gen.choose(MonthDay.of(Month.JANUARY, 1), MonthDay.of(Month.DECEMBER, 31)))
63+
64+
// ZoneOffset
65+
66+
implicit final lazy val arbZoneOffset: Arbitrary[ZoneOffset] =
67+
Arbitrary(
68+
Gen.oneOf(
69+
Gen.oneOf(ZoneOffset.MAX, ZoneOffset.MIN, ZoneOffset.UTC),
70+
Gen.choose(ZoneOffset.MAX, ZoneOffset.MIN) // These look flipped, but they are not.
71+
)
72+
)
73+
74+
// ZoneId
75+
76+
/** ''Technically'' the available zone ids can change at runtime, so we store
77+
* an immutable snapshot in time here. We avoid going through the
78+
* scala/java collection converters to avoid having to deal with the scala
79+
* 2.13 changes and adding a dependency on the collection compatibility
80+
* library.
81+
*/
82+
private final lazy val availableZoneIds: Set[ZoneId] =
83+
ZoneId.getAvailableZoneIds.toArray(Array.empty[String]).toSet.map((value: String) => ZoneId.of(value))
84+
85+
// ZoneIds by themselves do not describe an offset from UTC (ZoneOffset
86+
// does), so there isn't a meaningful way to define a choose as they can not
87+
// be reasonably ordered.
88+
89+
implicit final lazy val arbZoneId: Arbitrary[ZoneId] =
90+
Arbitrary(Gen.oneOf(Gen.oneOf(availableZoneIds), arbZoneOffset.arbitrary))
91+
92+
// OffsetTime
93+
94+
implicit final lazy val arbOffsetTime: Arbitrary[OffsetTime] =
95+
Arbitrary(Gen.choose(OffsetTime.MIN, OffsetTime.MAX))
96+
97+
// OffsetDateTime
98+
99+
implicit final lazy val arbOffsetDateTime: Arbitrary[OffsetDateTime] =
100+
Arbitrary(Gen.choose(OffsetDateTime.MIN, OffsetDateTime.MAX))
101+
102+
// Period
103+
104+
implicit final lazy val arbPeriod: Arbitrary[Period] =
105+
Arbitrary(
106+
for {
107+
years <- Arbitrary.arbitrary[Int]
108+
months <- Arbitrary.arbitrary[Int]
109+
days <- Arbitrary.arbitrary[Int]
110+
} yield Period.of(years, months, days))
111+
112+
// YearMonth
113+
114+
implicit final lazy val arbYearMonth: Arbitrary[YearMonth] =
115+
Arbitrary(Gen.choose(YearMonth.of(Year.MIN_VALUE, Month.JANUARY), YearMonth.of(Year.MAX_VALUE, Month.DECEMBER)))
116+
117+
// ZonedDateTime
118+
119+
implicit final lazy val arbZonedDateTime: Arbitrary[ZonedDateTime] =
120+
// The ZoneOffset's here look flipped but they are
121+
// not. ZonedDateTime.of(LocalDateTime.MIN, ZoneOffset.MAX) is _older_
122+
// than ZonedDateTime.of(LocalDateTime, ZoneOffset.MIN).
123+
Arbitrary(Gen.choose(ZonedDateTime.of(LocalDateTime.MIN, ZoneOffset.MAX), ZonedDateTime.of(LocalDateTime.MAX, ZoneOffset.MIN)))
124+
}

0 commit comments

Comments
 (0)