Skip to content

Commit 96abc29

Browse files
committed
javatime.kt: fix toSeconds()
1 parent a2ba85b commit 96abc29

File tree

1 file changed

+12
-8
lines changed
  • firebase-dataconnect/testutil/src/main/kotlin/com/google/firebase/dataconnect/testutil/property/arbitrary

1 file changed

+12
-8
lines changed

firebase-dataconnect/testutil/src/main/kotlin/com/google/firebase/dataconnect/testutil/property/arbitrary/javatime.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ sealed interface TimeOffset {
199199
}
200200

201201
fun toSeconds(): Int {
202-
val absValue = hours + (minutes * 60)
202+
val absValue = (hours * SECONDS_PER_HOUR) + (minutes * SECONDS_PER_MINUTE)
203203
return when (sign) {
204204
Sign.Positive -> absValue
205205
Sign.Negative -> -absValue
@@ -221,14 +221,17 @@ sealed interface TimeOffset {
221221
companion object {
222222
val validHours = 0..18
223223
val validMinutes = 0..59
224-
val maxSeconds: Int = 18 * 60
224+
val maxSeconds: Int = 18 * 60 * 60
225+
226+
private const val SECONDS_PER_MINUTE = 60
227+
private const val SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE
225228

226229
fun forSeconds(seconds: Int, sign: Sign): HhMm {
227230
require(seconds in 0..maxSeconds) {
228231
"invalid seconds: $seconds (must be between 0 and $maxSeconds, inclusive)"
229232
}
230-
val hours = seconds / 60
231-
val minutes = seconds - (hours * 60)
233+
val hours = seconds / SECONDS_PER_HOUR
234+
val minutes = (seconds - (hours * SECONDS_PER_HOUR)) / SECONDS_PER_MINUTE
232235
return HhMm(hours = hours, minutes = minutes, sign = sign)
233236
}
234237
}
@@ -279,7 +282,7 @@ object JavaTimeArbs {
279282
"internal error gh98nqedss: " +
280283
"invalid numSecondsBelowMaxEpochSecond: $numSecondsBelowMaxEpochSecond"
281284
}
282-
val maxTimeZoneOffset =
285+
val minTimeZoneOffset =
283286
if (numSecondsBelowMaxEpochSecond >= TimeOffset.HhMm.maxSeconds) {
284287
null
285288
} else {
@@ -294,7 +297,7 @@ object JavaTimeArbs {
294297
"internal error mje6a4mrbm: " +
295298
"invalid numSecondsAboveMinEpochSecond: $numSecondsAboveMinEpochSecond"
296299
}
297-
val minTimeZoneOffset =
300+
val maxTimeZoneOffset =
298301
if (numSecondsAboveMinEpochSecond >= TimeOffset.HhMm.maxSeconds) {
299302
null
300303
} else {
@@ -323,8 +326,8 @@ object JavaTimeArbs {
323326
"internal error weppxzqj2y: " +
324327
"instant.epochSecond out of range by " +
325328
"${validEpochSecondRange.first - instant.epochSecond}: ${instant.epochSecond} (" +
326-
"validEpochSecondRange.first=${validEpochSecondRange.first}, "
327-
"year=$year, month=$month, day=$day, " +
329+
"validEpochSecondRange.first=${validEpochSecondRange.first}, " +
330+
"year=$year, month=$month, day=$day, " +
328331
"hour=$hour, minute=$minute, second=$second, " +
329332
"nanosecond=$nanosecond timeOffset=$timeOffset, " +
330333
"minTimeZoneOffset=$minTimeZoneOffset, maxTimeZoneOffset=$maxTimeZoneOffset)"
@@ -335,6 +338,7 @@ object JavaTimeArbs {
335338
"${instant.epochSecond - validEpochSecondRange.last}: ${instant.epochSecond} (" +
336339
"validEpochSecondRange.last=${validEpochSecondRange.last}, " +
337340
"year=$year, month=$month, day=$day, " +
341+
"hour=$hour, minute=$minute, second=$second, " +
338342
"nanosecond=$nanosecond timeOffset=$timeOffset, " +
339343
"minTimeZoneOffset=$minTimeZoneOffset, maxTimeZoneOffset=$maxTimeZoneOffset)"
340344
}

0 commit comments

Comments
 (0)