1 /* 2 * Make.org Core API 3 * Copyright (C) 2018 Make.org 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Affero General Public License for more details. 14 * 15 * You should have received a copy of the GNU Affero General Public License 16 * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package org.make.core.technical.generator 21 22 import java.time.temporal.{ChronoField, TemporalAmount} 23 import java.time.{Instant, LocalDate, ZoneOffset, ZonedDateTime} 24 25 import org.make.core.{DateHelper, Validation} 26 import org.scalacheck.Gen 27 28 trait DateGenerators { 29 30 /** 31 * Creates a date generator which will generate dates in years close to current year 32 * 33 * Example: 34 * `genDateWithOffset(lowerOffset = Duration.of(-3L, ChronoUnit.YEARS), upperOffset = Duration.of(6L, ChronoUnit.MONTHS))` 35 * will generate a date between now minus 3 years and now plus 6 months 36 * 37 * @param lowerOffset the minimum temporal amount to offset 38 * @param upperOffset the maximum temporal amount to offset 39 * @param fromDate the date to apply offsets to 40 * @return a generator with dates constrained 41 */ 42 def genDateWithOffset( 43 lowerOffset: TemporalAmount, 44 upperOffset: TemporalAmount, 45 fromDate: ZonedDateTime = DateHelper.now() 46 ): Gen[ZonedDateTime] = { 47 val lowerBound: Long = fromDate.plus(lowerOffset).toInstant.toEpochMilli 48 val upperBound: Long = fromDate.plus(upperOffset).toInstant.toEpochMilli 49 50 Gen.choose(lowerBound, upperBound).map(offset => Instant.ofEpochMilli(offset).atZone(ZoneOffset.UTC)) 51 } 52 53 lazy val genBirthDate: Gen[LocalDate] = { 54 for { 55 age <- Gen.choose(Validation.minAgeWithLegalConsent, Validation.maxAge) 56 } yield { 57 val currentYear: Int = DateHelper.now().get(ChronoField.YEAR) 58 LocalDate.parse(s"${currentYear - age}-01-01") 59 } 60 } 61 62 }
| Line | Stmt Id | Pos | Tree | Symbol | Tests | Code |
|---|---|---|---|---|---|---|
| 47 | 3311 | 1756 - 1805 | Apply | java.time.Instant.toEpochMilli | org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest | fromDate.plus(lowerOffset).toInstant().toEpochMilli() |
| 48 | 2246 | 1833 - 1882 | Apply | java.time.Instant.toEpochMilli | org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest | fromDate.plus(upperOffset).toInstant().toEpochMilli() |
| 50 | 3524 | 1973 - 1987 | Select | java.time.ZoneOffset.UTC | org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest | java.time.ZoneOffset.UTC |
| 50 | 596 | 1888 - 1989 | Apply | org.scalacheck.Gen.map | org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest | org.scalacheck.Gen.choose[Long](lowerBound, upperBound)(Gen.this.Choose.chooseLong).map[java.time.ZonedDateTime](((offset: Long) => java.time.Instant.ofEpochMilli(offset).atZone(java.time.ZoneOffset.UTC))) |
| 50 | 5482 | 1898 - 1898 | Select | org.scalacheck.Gen.Choose.chooseLong | org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest | Gen.this.Choose.chooseLong |
| 50 | 1444 | 1937 - 1988 | Apply | java.time.Instant.atZone | org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest | java.time.Instant.ofEpochMilli(offset).atZone(java.time.ZoneOffset.UTC) |