Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zio-test Gen.offsetDateTime.genOffset might fail with invalid bounds #8051

Closed
DaniRey opened this issue Apr 17, 2023 · 2 comments
Closed

zio-test Gen.offsetDateTime.genOffset might fail with invalid bounds #8051

DaniRey opened this issue Apr 17, 2023 · 2 comments

Comments

@DaniRey
Copy link
Contributor

DaniRey commented Apr 17, 2023

The offset of the minLocalDate might be bigger then the offset of the maxLocalDate.

image

I suggest the following solution

  /**
   * A generator of `java.time.OffsetDateTime` values inside the specified
   * range: [min, max]. Shrinks toward min.
   */
  final def offsetDateTime(min: OffsetDateTime, max: OffsetDateTime)(implicit
                                                                     trace: Trace
  ): Gen[Any, OffsetDateTime] = {

    def genLocalDateTime(min: OffsetDateTime, max: OffsetDateTime): Gen[Any, LocalDateTime] = {
      val minInst = min.toInstant
      val maxInst = max.toInstant
      instant(minInst, maxInst).map(_.atOffset(utc).toLocalDateTime)
    }

    def genOffset(min: OffsetDateTime, max: OffsetDateTime, actual: LocalDateTime): Gen[Any, ZoneOffset] = {
      val minLocalDate = min.atZoneSimilarLocal(utc).toLocalDate
      val maxLocalDate = max.atZoneSimilarLocal(utc).toLocalDate
      val actualLocalDate = actual.toLocalDate
      val zoneOffsetMin = -18 * 3600
      val zoneOffsetMax = 18 * 3600
      val minOffsetSeconds = if (minLocalDate == actualLocalDate) min.getOffset.getTotalSeconds else zoneOffsetMin
      val maxOffsetSeconds = if (maxLocalDate == actualLocalDate) max.getOffset.getTotalSeconds else zoneOffsetMax
      if (minOffsetSeconds > maxOffsetSeconds) {
        val secondsAfterMin = (min.toInstant.toEpochMilli - actual.toInstant(utc).toEpochMilli) / 1000
        val secondsBeforeMax = (max.toInstant.toEpochMilli - actual.toInstant(utc).toEpochMilli) / 1000
        Gen.int(Math.max(secondsAfterMin.toInt, zoneOffsetMin), Math.min(secondsBeforeMax.toInt, zoneOffsetMax)).map(ZoneOffset.ofTotalSeconds)
      } else {
        Gen.int(minOffsetSeconds, maxOffsetSeconds).map(ZoneOffset.ofTotalSeconds)
      }
    }

    for {
      localDateTime <- genLocalDateTime(min, max)
      offset <- genOffset(min, max, localDateTime)
    } yield OffsetDateTime.of(localDateTime, offset)
  }

Which is basically the following addition

      if (minOffsetSeconds > maxOffsetSeconds) {
        val secondsAfterMin = (min.toInstant.toEpochMilli - actual.toInstant(utc).toEpochMilli) / 1000
        val secondsBeforeMax = (max.toInstant.toEpochMilli - actual.toInstant(utc).toEpochMilli) / 1000
        Gen.int(Math.max(secondsAfterMin.toInt, zoneOffsetMin), Math.min(secondsBeforeMax.toInt, zoneOffsetMax)).map(ZoneOffset.ofTotalSeconds)
      }
@DaniRey
Copy link
Contributor Author

DaniRey commented Apr 17, 2023

For anyone finding this issue before it is resolved.
This might be a good workaround Gen.zonedDateTime(min, max).map(_.toOffsetDateTime)

@adamgfraser
Copy link
Contributor

@DaniRey Thanks for reporting! Pull requests welcome!

DaniRey added a commit to DaniRey/zio that referenced this issue Apr 17, 2023
Handle special case where the offset of min is bigger then the offset of max in Gen.offsetDateTime(min, max)
DaniRey added a commit to DaniRey/zio that referenced this issue Apr 17, 2023
Handle special case where the offset of min is bigger then the offset of max in Gen.offsetDateTime(min, max)
DaniRey added a commit to DaniRey/zio that referenced this issue Apr 17, 2023
Handle special case where the offset of min is bigger then the offset of max in Gen.offsetDateTime(min, max)
DaniRey added a commit to DaniRey/zio that referenced this issue Apr 18, 2023
Handle special case where the offset of min is bigger then the offset of max in Gen.offsetDateTime(min, max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants