java.time.ZoneId Scala Examples
The following examples show how to use java.time.ZoneId.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: FundamentalsParser.scala From YahooFinanceScala with MIT License | 6 votes |
package openquant.yahoofinance.impl import java.time.format.DateTimeFormatter import java.time.{LocalDate, ZoneId, ZonedDateTime} import com.github.tototoshi.csv._ import openquant.yahoofinance.Fundamentals import scala.io.Source object FundamentalsParser extends Function1[String, Vector[Fundamentals]] { def apply(content: String): Vector[Fundamentals] = { val csvReader = CSVReader.open(Source.fromString(content)) val fundamentals: Vector[Fundamentals] = csvReader.toStream.map { fields ⇒ parseCSVLine(fields.toVector) }.toVector fundamentals } private def parseCSVLine(field: Vector[String]): Fundamentals = { require(field.length >= 2, "number of fields") val name = field(1) if (name == "N/A") Fundamentals( looksValid = false, symbol = field(0), name = name ) else Fundamentals( looksValid = true, symbol = field(0), name = name ) } }
Example 2
Source File: Encodings.scala From quill with Apache License 2.0 | 5 votes |
package io.getquill.context.cassandra.encoding import java.time.{ Instant, LocalDate, ZonedDateTime, ZoneId } import java.util.Date import com.datastax.driver.core.{ LocalDate => CasLocalDate } import io.getquill.context.cassandra.CassandraContext trait Encodings extends CassandraMapperConversions with CassandraTypes { this: CassandraContext[_] => protected val zoneId = ZoneId.systemDefault implicit val encodeJava8LocalDate: MappedEncoding[LocalDate, CasLocalDate] = MappedEncoding(ld => CasLocalDate.fromYearMonthDay(ld.getYear, ld.getMonthValue, ld.getDayOfMonth)) implicit val decodeJava8LocalDate: MappedEncoding[CasLocalDate, LocalDate] = MappedEncoding(ld => LocalDate.of(ld.getYear, ld.getMonth, ld.getDay)) implicit val encodeJava8Instant: MappedEncoding[Instant, Date] = MappedEncoding(Date.from) implicit val decodeJava8Instant: MappedEncoding[Date, Instant] = MappedEncoding(_.toInstant) implicit val encodeJava8ZonedDateTime: MappedEncoding[ZonedDateTime, Date] = MappedEncoding(zdt => Date.from(zdt.toInstant)) implicit val decodeJava8ZonedDateTime: MappedEncoding[Date, ZonedDateTime] = MappedEncoding(d => ZonedDateTime.ofInstant(d.toInstant, zoneId)) }
Example 3
Source File: SimpleTestUsernamePasswordAuthenticator.scala From asura with MIT License | 5 votes |
package asura.app.api.auth import java.time.{LocalDate, ZoneId} import java.util.Date import org.pac4j.core.context.WebContext import org.pac4j.core.credentials.UsernamePasswordCredentials import org.pac4j.core.credentials.authenticator.Authenticator import org.pac4j.core.exception.CredentialsException import org.pac4j.core.profile.CommonProfile import org.pac4j.core.util.{CommonHelper, Pac4jConstants} import org.pac4j.jwt.config.signature.SecretSignatureConfiguration import org.pac4j.jwt.profile.JwtGenerator import play.api.Configuration class SimpleTestUsernamePasswordAuthenticator(configuration: Configuration) extends Authenticator[UsernamePasswordCredentials] { override def validate(credentials: UsernamePasswordCredentials, context: WebContext): Unit = { if (credentials == null) throw new CredentialsException("No credential") val username = credentials.getUsername val password = credentials.getPassword if (CommonHelper.isBlank(username)) throw new CredentialsException("Username cannot be blank") if (CommonHelper.isBlank(password)) throw new CredentialsException("Password cannot be blank") if (CommonHelper.areNotEquals(username, password)) throw new CredentialsException("Username : '" + username + "' does not match password") val profile = new CommonProfile() profile.setId(username) profile.addAttribute(Pac4jConstants.USERNAME, username) val jwtGenerator = new JwtGenerator[CommonProfile](new SecretSignatureConfiguration(configuration.get[String]("asura.jwt.secret"))) val tomorrow = LocalDate.now().plusDays(1).atStartOfDay() jwtGenerator.setExpirationTime(Date.from(tomorrow.atZone(ZoneId.systemDefault()).toInstant())) val token = jwtGenerator.generate(profile) profile.addAttribute("token", token) credentials.setUserProfile(profile) } }
Example 4
Source File: LdapAuthenticator.scala From asura with MIT License | 5 votes |
package asura.app.api.auth import java.time.{Duration, LocalDate, ZoneId} import java.util.Date import org.ldaptive._ import org.ldaptive.auth.{Authenticator, BindAuthenticationHandler, SearchDnResolver} import org.pac4j.core.context.WebContext import org.pac4j.core.credentials.UsernamePasswordCredentials import org.pac4j.core.profile.CommonProfile import org.pac4j.jwt.config.signature.SecretSignatureConfiguration import org.pac4j.jwt.profile.JwtGenerator import org.pac4j.ldap.profile.service.LdapProfileService import play.api.Configuration object LdapAuthenticator { def apply(configuration: Configuration): LdapProfileService = { val connConfig = new ConnectionConfig() connConfig.setConnectTimeout(Duration.ofMillis(configuration.get[Long]("asura.ldap.connectionTimeout"))) connConfig.setResponseTimeout(Duration.ofMillis(configuration.get[Long]("asura.ldap.responseTimeout"))) connConfig.setLdapUrl(configuration.get[String]("asura.ldap.url")) connConfig.setConnectionInitializer(new BindConnectionInitializer(configuration.get[String]("asura.ldap.bindDn"), new Credential(configuration.get[String]("asura.ldap.password")))) val connFactory = new DefaultConnectionFactory(connConfig) val handler = new BindAuthenticationHandler(connFactory) val dnResolver = new SearchDnResolver(connFactory) dnResolver.setBaseDn(configuration.get[String]("asura.ldap.searchbase")) dnResolver.setSubtreeSearch(true) dnResolver.setUserFilter(configuration.get[String]("asura.ldap.userFilter")) val authenticator = new Authenticator() authenticator.setDnResolver(dnResolver) authenticator.setAuthenticationHandler(handler) new CustomLdapProfileService(configuration, connFactory, authenticator, configuration.get[String]("asura.ldap.searchbase")) } class CustomLdapProfileService( configuration: Configuration, connectionFactory: ConnectionFactory, authenticator: Authenticator, usersDn: String) extends LdapProfileService(connectionFactory, authenticator, usersDn) { this.setIdAttribute(configuration.get[String]("asura.ldap.userIdAttr")) this.setAttributes(s"${configuration.get[String]("asura.ldap.userRealNameAttr")},${configuration.get[String]("asura.ldap.userEmailAttr")}") override def validate(credentials: UsernamePasswordCredentials, context: WebContext): Unit = { super.validate(credentials, context) val jwtGenerator = new JwtGenerator[CommonProfile](new SecretSignatureConfiguration(configuration.get[String]("asura.jwt.secret"))) val tomorrow = LocalDate.now().plusDays(1).atStartOfDay().plusHours(3) jwtGenerator.setExpirationTime(Date.from(tomorrow.atZone(ZoneId.systemDefault()).toInstant())) val profile = credentials.getUserProfile val token = jwtGenerator.generate(profile) profile.addAttribute("token", token) } } }
Example 5
Source File: Constants.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.core.domain.utils import java.time.ZoneId import gospeak.libs.scala.Extensions._ import gospeak.libs.scala.domain.{EmailAddress, Logo, Url} object Constants { val defaultZoneId: ZoneId = ZoneId.of("Europe/Paris") object Gospeak { val name = "Gospeak" val url: Url = Url.from("https://gospeak.io").get val logo: Logo = Logo(Url.from("https://gospeak.io/logo.png").get) val logoText: Logo = Logo(Url.from("https://res.cloudinary.com/gospeak/image/upload/gospeak/logo-text.svg").get) val contactEmail: EmailAddress.Contact = EmailAddress.Contact(EmailAddress.from("[email protected]").get, Some("Gospeak")) val noreplyEmail: EmailAddress.Contact = EmailAddress.Contact(EmailAddress.from("[email protected]").get, Some("Gospeak")) val twitter: Url.Twitter = Url.Twitter.from("https://twitter.com/gospeak_io").get val linkedIn: Url.LinkedIn = Url.LinkedIn.from("https://www.linkedin.com/company/gospeak").get } object Placeholders { val groupLogo = "https://res.cloudinary.com/gospeak/image/upload/placeholders/group-logo.png" // FIXME find better image val eventLogo = "https://res.cloudinary.com/gospeak/image/upload/placeholders/group-logo.png" // FIXME find better image val unknownUser = "https://res.cloudinary.com/gospeak/image/upload/placeholders/unknown-user.png" val unknownPartner = "https://res.cloudinary.com/gospeak/image/upload/placeholders/unknown-user.png" // FIXME find better image val noVenueForEvent = "https://res.cloudinary.com/gospeak/image/upload/placeholders/no-venue-for-event.png" val videoCover = "/assets/web/img/placeholders/video-cover.jpg" } object Emoji { // https://coolsymbol.com/emojis/emoji-for-copy-and-paste.html // https://unicode.org/emoji/charts/full-emoji-list.html val sparkles = "✨" val partyPopper = "\uD83C\uDF89" val nerdFace = "\uD83E\uDD13" val grinningFace = "\uD83D\uDE00" val rocket = "\uD83D\uDE80" val speakingHead = "\uD83D\uDDE3" val speechBalloon = "\uD83D\uDCAC" val directHit = "\uD83C\uDFAF" val loudSpeaker = "\uD83D\uDCE2" val studioMicrophone = "\uD83C\uDF99" val clapperBoard = "\uD83C\uDFAC" val calendar = "\uD83D\uDCC6" val gospeak: String = sparkles val user: String = nerdFace val talk: String = speechBalloon val group: String = directHit val cfp: String = loudSpeaker val proposal: String = studioMicrophone val event: String = calendar val video: String = clapperBoard } object Slack { val defaultBotName = "Gospeak bot" val defaultBotAvatar = "" } val devMessage: String = """Hi! Nice to meet you ❤ |Did you know Gospeak is an open source project done in Scala FP: https://github.com/gospeak-io/gospeak ? |If you like it, give us a cheer message on twitter @gospeak_io or in our issues ;) |""".stripMargin }
Example 6
Source File: JavaTimeClock.scala From aecor with MIT License | 5 votes |
package aecor.util import java.time.{ Instant, ZoneId } import cats.effect.Sync class JavaTimeClock[F[_]](underlying: java.time.Clock)(implicit F: Sync[F]) extends Clock[F] { override def zone: F[ZoneId] = F.delay(underlying.getZone) override def instant: F[Instant] = F.delay(underlying.instant()) } object JavaTimeClock { def apply[F[_]: Sync](underlying: java.time.Clock): Clock[F] = new JavaTimeClock[F](underlying) def systemDefault[F[_]: Sync]: Clock[F] = apply(java.time.Clock.systemDefaultZone()) def systemUTC[F[_]: Sync]: Clock[F] = apply(java.time.Clock.systemUTC()) }
Example 7
Source File: StateClock.scala From aecor with MIT License | 5 votes |
package aecor.testkit import java.time.temporal.TemporalAmount import java.time.{ Instant, ZoneId } import aecor.util.Clock import cats.mtl.MonadState import monocle.Lens class StateClock[F[_]: MonadState[*[_], S], S](zoneId: ZoneId, S: Lens[S, Instant]) extends Clock[F] { private val F = S.transformMonadState(MonadState[F, S]) override def zone: F[ZoneId] = F.monad.pure(zoneId) override def instant: F[Instant] = F.get def tick(temporalAmount: TemporalAmount): F[Unit] = F.modify(_.plus(temporalAmount)) } object StateClock { def apply[F[_], S](zoneId: ZoneId, S: Lens[S, Instant])(implicit F0: MonadState[F, S]): StateClock[F, S] = new StateClock[F, S](zoneId, S) }
Example 8
Source File: AWSSigningJestClientFactory.scala From haystack-traces with Apache License 2.0 | 5 votes |
package com.expedia.www.haystack.trace.commons.clients.es import java.time.{LocalDateTime, ZoneId} import com.expedia.www.haystack.trace.commons.config.entities.AWSRequestSigningConfiguration import com.google.common.base.Supplier import io.searchbox.client.JestClientFactory import org.apache.http.impl.client.HttpClientBuilder import org.apache.http.impl.nio.client.HttpAsyncClientBuilder import org.slf4j.LoggerFactory import vc.inreach.aws.request.{AWSSigner, AWSSigningRequestInterceptor} import com.amazonaws.auth.AWSCredentialsProvider import com.amazonaws.auth.BasicAWSCredentials import com.amazonaws.auth.DefaultAWSCredentialsProviderChain import com.amazonaws.internal.StaticCredentialsProvider class AWSSigningJestClientFactory(awsRequestSigningConfig: AWSRequestSigningConfiguration) extends JestClientFactory { private val LOGGER = LoggerFactory.getLogger(classOf[AWSSigningJestClientFactory]) val awsSigner = new AWSSigner(getCredentialProvider, awsRequestSigningConfig.region, awsRequestSigningConfig.awsServiceName, new ClockSupplier) val requestInterceptor = new AWSSigningRequestInterceptor(awsSigner) override def configureHttpClient(builder: HttpClientBuilder): HttpClientBuilder = { builder.addInterceptorLast(requestInterceptor) } override def configureHttpClient(builder: HttpAsyncClientBuilder): HttpAsyncClientBuilder = { builder.addInterceptorLast(requestInterceptor) } def getCredentialProvider: AWSCredentialsProvider = { if (awsRequestSigningConfig.accessKey.isDefined) { LOGGER.info("using static aws credential provider with access and secret key for ES") new StaticCredentialsProvider( new BasicAWSCredentials(awsRequestSigningConfig.accessKey.get, awsRequestSigningConfig.secretKey.get)) } else { LOGGER.info("using default credential provider chain for ES") new DefaultAWSCredentialsProviderChain } } } class ClockSupplier extends Supplier[LocalDateTime] { override def get(): LocalDateTime = { LocalDateTime.now(ZoneId.of("UTC")) } }
Example 9
Source File: ResourceFSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import java.time.{Clock, Instant, ZoneId} import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Resources} import ch.epfl.bluebrain.nexus.iam.config.AppConfig.HttpConfig import ch.epfl.bluebrain.nexus.iam.config.Vocabulary._ import ch.epfl.bluebrain.nexus.iam.testsyntax._ import ch.epfl.bluebrain.nexus.iam.types.Identity.User import ch.epfl.bluebrain.nexus.rdf.implicits._ import io.circe.Printer import io.circe.syntax._ import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike //noinspection TypeAnnotation class ResourceFSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with Resources { "A ResourceMetadata" should { val user = User("mysubject", "myrealm") val user2 = User("mysubject2", "myrealm") implicit val http = HttpConfig("some", 8080, "v1", "http://nexus.example.com") val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) val instant = clock.instant() val id = url"http://example.com/id" val printer = Printer.spaces2.copy(dropNullValues = true) "be converted to Json correctly" when { "using multiple types" in { val json = jsonContentOf("/resources/write-response.json") val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList, nxv.Realm), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } "using a single type" in { val json = jsonContentOf("/resources/write-response-singletype.json") val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } "using no types" in { val json = jsonContentOf("/resources/write-response-notypes.json") val model = ResourceMetadata(id, 1L, Set(), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } } } }
Example 10
Source File: TimePeriod.scala From TransmogrifAI with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.salesforce.op.stages.impl.feature import java.time.temporal.WeekFields import java.time.{Instant, LocalDateTime, ZoneId} import com.salesforce.op.utils.date.DateTimeUtils import enumeratum.{Enum, EnumEntry} case class TimePeriodVal(value: Int, min: Int, max: Int) sealed abstract class TimePeriod(extractFn: LocalDateTime => TimePeriodVal) extends EnumEntry with Serializable { def extractTimePeriodVal(millis: Long): TimePeriodVal = extractFn( Instant .ofEpochMilli(millis) .atZone(ZoneId.of(DateTimeUtils.DefaultTimeZone.toString)).toLocalDateTime) def extractIntFromMillis(millis: Long): Int = extractTimePeriodVal(millis).value } object TimePeriod extends Enum[TimePeriod] { @transient val weekFields = WeekFields.of(java.time.DayOfWeek.MONDAY, 1) val values: Seq[TimePeriod] = findValues case object DayOfMonth extends TimePeriod(dt => TimePeriodVal(dt.getDayOfMonth, 1, 31)) case object DayOfWeek extends TimePeriod(dt => TimePeriodVal(dt.getDayOfWeek.getValue, 1, 7)) case object DayOfYear extends TimePeriod(dt => TimePeriodVal(dt.getDayOfYear, 1, 366)) case object HourOfDay extends TimePeriod(dt => TimePeriodVal(dt.getHour, 0, 24)) case object MonthOfYear extends TimePeriod(dt => TimePeriodVal(dt.getMonthValue, 1, 12)) case object WeekOfMonth extends TimePeriod(dt => TimePeriodVal(dt.get(weekFields.weekOfMonth()), 1, 6)) case object WeekOfYear extends TimePeriod(dt => TimePeriodVal(dt.get(weekFields.weekOfYear()), 1, 53)) }
Example 11
Source File: Encoders.scala From quill with Apache License 2.0 | 5 votes |
package io.getquill.context.async import java.time.{ LocalDate, LocalDateTime, LocalTime, OffsetDateTime, ZoneId, ZonedDateTime } import java.util.Date import org.joda.time.{ DateTime => JodaDateTime, DateTimeZone => JodaDateTimeZone, LocalTime => JodaLocalTime, LocalDate => JodaLocalDate, LocalDateTime => JodaLocalDateTime } trait Encoders { this: AsyncContext[_, _, _] => type Encoder[T] = AsyncEncoder[T] type EncoderSqlType = SqlTypes.SqlTypes case class AsyncEncoder[T](sqlType: DecoderSqlType)(implicit encoder: BaseEncoder[T]) extends BaseEncoder[T] { override def apply(index: Index, value: T, row: PrepareRow) = encoder.apply(index, value, row) } def encoder[T](sqlType: DecoderSqlType): Encoder[T] = encoder(identity[T], sqlType) def encoder[T](f: T => Any, sqlType: DecoderSqlType): Encoder[T] = AsyncEncoder[T](sqlType)(new BaseEncoder[T] { def apply(index: Index, value: T, row: PrepareRow) = row :+ f(value) }) implicit def mappedEncoder[I, O](implicit mapped: MappedEncoding[I, O], e: Encoder[O]): Encoder[I] = AsyncEncoder(e.sqlType)(new BaseEncoder[I] { def apply(index: Index, value: I, row: PrepareRow) = e(index, mapped.f(value), row) }) implicit def optionEncoder[T](implicit d: Encoder[T]): Encoder[Option[T]] = AsyncEncoder(d.sqlType)(new BaseEncoder[Option[T]] { def apply(index: Index, value: Option[T], row: PrepareRow) = { value match { case None => nullEncoder(index, null, row) case Some(v) => d(index, v, row) } } }) private[this] val nullEncoder: Encoder[Null] = encoder[Null](SqlTypes.NULL) implicit val stringEncoder: Encoder[String] = encoder[String](SqlTypes.VARCHAR) implicit val bigDecimalEncoder: Encoder[BigDecimal] = encoder[BigDecimal](SqlTypes.REAL) implicit val booleanEncoder: Encoder[Boolean] = encoder[Boolean](SqlTypes.BOOLEAN) implicit val byteEncoder: Encoder[Byte] = encoder[Byte](SqlTypes.TINYINT) implicit val shortEncoder: Encoder[Short] = encoder[Short](SqlTypes.SMALLINT) implicit val intEncoder: Encoder[Int] = encoder[Int](SqlTypes.INTEGER) implicit val longEncoder: Encoder[Long] = encoder[Long](SqlTypes.BIGINT) implicit val floatEncoder: Encoder[Float] = encoder[Float](SqlTypes.FLOAT) implicit val doubleEncoder: Encoder[Double] = encoder[Double](SqlTypes.DOUBLE) implicit val byteArrayEncoder: Encoder[Array[Byte]] = encoder[Array[Byte]](SqlTypes.VARBINARY) implicit val jodaDateTimeEncoder: Encoder[JodaDateTime] = encoder[JodaDateTime](SqlTypes.TIMESTAMP) implicit val jodaLocalDateEncoder: Encoder[JodaLocalDate] = encoder[JodaLocalDate](SqlTypes.DATE) implicit val jodaLocalDateTimeEncoder: Encoder[JodaLocalDateTime] = encoder[JodaLocalDateTime](SqlTypes.TIMESTAMP) implicit val dateEncoder: Encoder[Date] = encoder[Date]((d: Date) => new JodaLocalDateTime(d), SqlTypes.TIMESTAMP) implicit val encodeZonedDateTime: MappedEncoding[ZonedDateTime, JodaDateTime] = MappedEncoding(zdt => new JodaDateTime(zdt.toInstant.toEpochMilli, JodaDateTimeZone.forID(zdt.getZone.getId))) implicit val encodeOffsetDateTime: MappedEncoding[OffsetDateTime, JodaDateTime] = MappedEncoding(odt => new JodaDateTime(odt.toInstant.toEpochMilli, JodaDateTimeZone.forID(odt.getOffset.getId))) implicit val encodeLocalDate: MappedEncoding[LocalDate, JodaLocalDate] = MappedEncoding(ld => new JodaLocalDate(ld.getYear, ld.getMonthValue, ld.getDayOfMonth)) implicit val encodeLocalTime: MappedEncoding[LocalTime, JodaLocalTime] = MappedEncoding(lt => new JodaLocalTime(lt.getHour, lt.getMinute, lt.getSecond)) implicit val encodeLocalDateTime: MappedEncoding[LocalDateTime, JodaLocalDateTime] = MappedEncoding(ldt => new JodaLocalDateTime(ldt.atZone(ZoneId.systemDefault()).toInstant.toEpochMilli)) implicit val localDateEncoder: Encoder[LocalDate] = mappedEncoder(encodeLocalDate, jodaLocalDateEncoder) }
Example 12
Source File: JobRunId.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome package model import java.time.{Clock, ZoneId} import java.time.format.DateTimeFormatter import mesosphere.marathon.state.PathId case class JobRunId(jobId: JobId, value: String) { override def toString: String = s"${jobId.path.mkString(".")}.$value" def toPathId: PathId = jobId.toPathId / value } object JobRunId { val idFormat: DateTimeFormatter = DateTimeFormatter .ofPattern("yyyyMMddHHmmss") .withZone(ZoneId.systemDefault()) def apply(job: JobSpec): JobRunId = { val date = idFormat.format(Clock.systemUTC().instant()) val random = scala.util.Random.alphanumeric.take(5).mkString JobRunId(job.id, s"$date$random") } def apply(runSpecId: PathId): JobRunId = { JobRunId(JobId(runSpecId.parent), runSpecId.path.last) } }
Example 13
Source File: Encoders.scala From quill with Apache License 2.0 | 5 votes |
package io.getquill.context.jasync import java.time.{ LocalDate, LocalDateTime, LocalTime, OffsetDateTime, ZoneId, ZonedDateTime } import java.util.Date import org.joda.time.{ DateTime => JodaDateTime, DateTimeZone => JodaDateTimeZone, LocalTime => JodaLocalTime, LocalDate => JodaLocalDate, LocalDateTime => JodaLocalDateTime } trait Encoders { this: JAsyncContext[_, _, _] => type Encoder[T] = AsyncEncoder[T] type EncoderSqlType = SqlTypes.SqlTypes case class AsyncEncoder[T](sqlType: DecoderSqlType)(implicit encoder: BaseEncoder[T]) extends BaseEncoder[T] { override def apply(index: Index, value: T, row: PrepareRow) = encoder.apply(index, value, row) } def encoder[T](sqlType: DecoderSqlType): Encoder[T] = encoder(identity[T], sqlType) def encoder[T](f: T => Any, sqlType: DecoderSqlType): Encoder[T] = AsyncEncoder[T](sqlType)(new BaseEncoder[T] { def apply(index: Index, value: T, row: PrepareRow) = row :+ f(value) }) implicit def mappedEncoder[I, O](implicit mapped: MappedEncoding[I, O], e: Encoder[O]): Encoder[I] = AsyncEncoder(e.sqlType)(new BaseEncoder[I] { def apply(index: Index, value: I, row: PrepareRow) = e(index, mapped.f(value), row) }) implicit def optionEncoder[T](implicit d: Encoder[T]): Encoder[Option[T]] = AsyncEncoder(d.sqlType)(new BaseEncoder[Option[T]] { def apply(index: Index, value: Option[T], row: PrepareRow) = { value match { case None => nullEncoder(index, null, row) case Some(v) => d(index, v, row) } } }) private[this] val nullEncoder: Encoder[Null] = encoder[Null](SqlTypes.NULL) implicit val stringEncoder: Encoder[String] = encoder[String](SqlTypes.VARCHAR) implicit val bigDecimalEncoder: Encoder[BigDecimal] = encoder[BigDecimal]((bd: BigDecimal) => bd.bigDecimal, SqlTypes.REAL) implicit val booleanEncoder: Encoder[Boolean] = encoder[Boolean](SqlTypes.BOOLEAN) implicit val byteEncoder: Encoder[Byte] = encoder[Byte](SqlTypes.TINYINT) implicit val shortEncoder: Encoder[Short] = encoder[Short](SqlTypes.SMALLINT) implicit val intEncoder: Encoder[Int] = encoder[Int](SqlTypes.INTEGER) implicit val longEncoder: Encoder[Long] = encoder[Long](SqlTypes.BIGINT) implicit val floatEncoder: Encoder[Float] = encoder[Float](SqlTypes.FLOAT) implicit val doubleEncoder: Encoder[Double] = encoder[Double](SqlTypes.DOUBLE) implicit val byteArrayEncoder: Encoder[Array[Byte]] = encoder[Array[Byte]](SqlTypes.VARBINARY) implicit val jodaDateTimeEncoder: Encoder[JodaDateTime] = encoder[JodaDateTime](SqlTypes.TIMESTAMP) implicit val jodaLocalDateEncoder: Encoder[JodaLocalDate] = encoder[JodaLocalDate](SqlTypes.DATE) implicit val jodaLocalDateTimeEncoder: Encoder[JodaLocalDateTime] = encoder[JodaLocalDateTime](SqlTypes.TIMESTAMP) implicit val dateEncoder: Encoder[Date] = encoder[Date]((d: Date) => new JodaLocalDateTime(d), SqlTypes.TIMESTAMP) implicit val encodeZonedDateTime: MappedEncoding[ZonedDateTime, JodaDateTime] = MappedEncoding(zdt => new JodaDateTime(zdt.toInstant.toEpochMilli, JodaDateTimeZone.forID(zdt.getZone.getId))) implicit val encodeOffsetDateTime: MappedEncoding[OffsetDateTime, JodaDateTime] = MappedEncoding(odt => new JodaDateTime(odt.toInstant.toEpochMilli, JodaDateTimeZone.forID(odt.getOffset.getId))) implicit val encodeLocalDate: MappedEncoding[LocalDate, JodaLocalDate] = MappedEncoding(ld => new JodaLocalDate(ld.getYear, ld.getMonthValue, ld.getDayOfMonth)) implicit val encodeLocalTime: MappedEncoding[LocalTime, JodaLocalTime] = MappedEncoding(lt => new JodaLocalTime(lt.getHour, lt.getMinute, lt.getSecond)) implicit val encodeLocalDateTime: MappedEncoding[LocalDateTime, JodaLocalDateTime] = MappedEncoding(ldt => new JodaLocalDateTime(ldt.atZone(ZoneId.systemDefault()).toInstant.toEpochMilli)) implicit val localDateEncoder: Encoder[LocalDate] = mappedEncoder(encodeLocalDate, jodaLocalDateEncoder) }
Example 14
Source File: FinaglePostgresDecoders.scala From quill with Apache License 2.0 | 5 votes |
package io.getquill.context.finagle.postgres import java.nio.charset.Charset import java.time.{ LocalDate, LocalDateTime, ZoneId } import java.util.{ Date, UUID } import com.twitter.finagle.postgres.values.ValueDecoder import com.twitter.util.Return import com.twitter.util.Throw import com.twitter.util.Try import io.getquill.FinaglePostgresContext import io.getquill.util.Messages.fail import io.netty.buffer.ByteBuf trait FinaglePostgresDecoders { this: FinaglePostgresContext[_] => import ValueDecoder._ type Decoder[T] = FinaglePostgresDecoder[T] case class FinaglePostgresDecoder[T]( vd: ValueDecoder[T], default: Throwable => T = (e: Throwable) => fail(e.getMessage) ) extends BaseDecoder[T] { override def apply(index: Index, row: ResultRow): T = row.getTry[T](index)(vd) match { case Return(r) => r case Throw(e) => default(e) } def orElse[U](f: U => T)(implicit vdu: ValueDecoder[U]): FinaglePostgresDecoder[T] = { val mappedVd = vdu.map[T](f) FinaglePostgresDecoder[T]( new ValueDecoder[T] { def decodeText(recv: String, text: String): Try[T] = { val t = vd.decodeText(recv, text) if (t.isReturn) t else mappedVd.decodeText(recv, text) } def decodeBinary(recv: String, bytes: ByteBuf, charset: Charset): Try[T] = { val t = vd.decodeBinary(recv, bytes, charset) if (t.isReturn) t else mappedVd.decodeBinary(recv, bytes, charset) } } ) } } implicit def decoderDirectly[T](implicit vd: ValueDecoder[T]): Decoder[T] = FinaglePostgresDecoder(vd) def decoderMapped[U, T](f: U => T)(implicit vd: ValueDecoder[U]): Decoder[T] = FinaglePostgresDecoder(vd.map[T](f)) implicit def optionDecoder[T](implicit d: Decoder[T]): Decoder[Option[T]] = FinaglePostgresDecoder[Option[T]]( new ValueDecoder[Option[T]] { def decodeText(recv: String, text: String): Try[Option[T]] = Return(d.vd.decodeText(recv, text).toOption) def decodeBinary(recv: String, bytes: ByteBuf, charset: Charset): Try[Option[T]] = Return(d.vd.decodeBinary(recv, bytes, charset).toOption) }, _ => None ) implicit def mappedDecoder[I, O](implicit mapped: MappedEncoding[I, O], d: Decoder[I]): Decoder[O] = decoderMapped[I, O](mapped.f)(d.vd) implicit val stringDecoder: Decoder[String] = decoderDirectly[String] implicit val bigDecimalDecoder: Decoder[BigDecimal] = decoderDirectly[BigDecimal] implicit val booleanDecoder: Decoder[Boolean] = decoderDirectly[Boolean] implicit val shortDecoder: Decoder[Short] = decoderDirectly[Short] implicit val byteDecoder: Decoder[Byte] = decoderMapped[Short, Byte](_.toByte) implicit val intDecoder: Decoder[Int] = decoderDirectly[Int].orElse[Long](_.toInt) implicit val longDecoder: Decoder[Long] = decoderDirectly[Long].orElse[Int](_.toLong) implicit val floatDecoder: Decoder[Float] = decoderDirectly[Float].orElse[Double](_.toFloat) implicit val doubleDecoder: Decoder[Double] = decoderDirectly[Double] implicit val byteArrayDecoder: Decoder[Array[Byte]] = decoderDirectly[Array[Byte]] implicit val dateDecoder: Decoder[Date] = decoderMapped[LocalDateTime, Date](d => Date.from(d.atZone(ZoneId.systemDefault()).toInstant)) implicit val localDateDecoder: Decoder[LocalDate] = decoderDirectly[LocalDate].orElse[LocalDateTime](_.toLocalDate) implicit val localDateTimeDecoder: Decoder[LocalDateTime] = decoderDirectly[LocalDateTime].orElse[LocalDate](_.atStartOfDay) implicit val uuidDecoder: Decoder[UUID] = decoderDirectly[UUID] }
Example 15
Source File: AuditSerialiser.scala From play-auditing with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.audit.serialiser import play.api.libs.json.{JsString, JsValue, Json, Writes} import uk.gov.hmrc.play.audit.model.{DataCall, DataEvent, ExtendedDataEvent, MergedDataEvent} import java.time.{Instant, ZoneId} import java.time.format.DateTimeFormatter object DateWriter { // Datastream does not support default X offset (i.e. `Z` must be `+0000`) implicit def instantWrites = new Writes[Instant] { private val dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ") def writes(instant: Instant): JsValue = JsString(dateFormat.withZone(ZoneId.of("UTC")).format(instant)) } } trait AuditSerialiserLike { def serialise(event: DataEvent): JsValue def serialise(event: ExtendedDataEvent): JsValue def serialise(event: MergedDataEvent): JsValue } class AuditSerialiser extends AuditSerialiserLike { private implicit val dateWriter: Writes[Instant] = DateWriter.instantWrites private implicit val dataEventWriter: Writes[DataEvent] = Json.writes[DataEvent] private implicit val dataCallWriter: Writes[DataCall] = Json.writes[DataCall] private implicit val extendedDataEventWriter: Writes[ExtendedDataEvent] = Json.writes[ExtendedDataEvent] private implicit val mergedDataEventWriter: Writes[MergedDataEvent] = Json.writes[MergedDataEvent] override def serialise(event: DataEvent): JsValue = Json.toJson(event) override def serialise(event: ExtendedDataEvent): JsValue = Json.toJson(event) override def serialise(event: MergedDataEvent): JsValue = Json.toJson(event) } object AuditSerialiser extends AuditSerialiser
Example 16
Source File: InMemoryCommitCommitManager.scala From reactive-nakadi with MIT License | 5 votes |
package org.zalando.react.nakadi import java.time.{ZoneId, OffsetDateTime} import org.zalando.react.nakadi.commit.OffsetTracking import org.zalando.react.nakadi.commit.handlers.BaseCommitManager import scala.concurrent.Future object InMemoryCommitCommitManager extends BaseCommitManager { private val store = scala.collection.concurrent.TrieMap.empty[String, String] private def generateKey(group: String, eventType: String, partition: String) = s"$group-$eventType-$partition" private def generateValue(offset: String, leaseHolder: String, leaseCounter: Long) = s"$offset-$leaseHolder-$leaseCounter" def create(key: String, partitionId: String, checkpointId: String, leaseHolder: String) = { val value = generateValue(checkpointId, leaseHolder, 0) store.put(key, value) OffsetTracking( partitionId = partitionId, checkpointId = value.split("-")(0), leaseHolder = value.split("-")(1), leaseCounter = Option(1), leaseTimestamp =OffsetDateTime.now, leaseId = None ) } def update(key: String, value: String, partitionId: String) = { val count = value.split("-")(2).toLong val leaseCounter = count + 1 val offset = OffsetTracking( partitionId = partitionId, checkpointId = value.split("-")(0), leaseHolder = value.split("-")(1), leaseCounter = Option(leaseCounter), leaseTimestamp = OffsetDateTime.now, leaseId = None ) store.put(key, generateValue(offset.checkpointId, offset.leaseHolder, count)) offset } override def put(groupId: String, eventType: String, offset: OffsetTracking): Future[OffsetTracking] = Future.successful { val key = generateKey(groupId, eventType, offset.partitionId) store.get(key) .fold(create(key, offset.partitionId, offset.checkpointId, offset.leaseHolder))(update(key, _, offset.partitionId)) } override def get(groupId: String, eventType: String, partitionId: String): Future[Option[OffsetTracking]] = { Future.successful { val key = generateKey(groupId, eventType, partitionId) store.get(key).map { value => OffsetTracking( partitionId = partitionId, checkpointId = value.split("-")(0), leaseHolder = value.split("-")(1), leaseCounter = Option(1), leaseTimestamp = OffsetDateTime.now, leaseId = None ) } } } }
Example 17
Source File: package.scala From akka-persistence-cassandra with Apache License 2.0 | 5 votes |
package akka.persistence.cassandra import java.time.format.DateTimeFormatter import java.time.{ LocalDateTime, ZoneId, ZoneOffset } import java.util.UUID import akka.annotation.InternalApi import com.datastax.oss.driver.api.core.cql.AsyncResultSet import com.datastax.oss.driver.api.core.uuid.Uuids package object query { @InternalApi private[akka] def uuid(timestamp: Long): UUID = { def makeMsb(time: Long): Long = { // copied from Uuids.makeMsb // UUID v1 timestamp must be in 100-nanoseconds interval since 00:00:00.000 15 Oct 1582. val uuidEpoch = LocalDateTime.of(1582, 10, 15, 0, 0).atZone(ZoneId.of("GMT-0")).toInstant.toEpochMilli val timestamp = (time - uuidEpoch) * 10000 var msb = 0L msb |= (0X00000000FFFFFFFFL & timestamp) << 32 msb |= (0X0000FFFF00000000L & timestamp) >>> 16 msb |= (0X0FFF000000000000L & timestamp) >>> 48 msb |= 0X0000000000001000L // sets the version to 1. msb } val now = Uuids.timeBased() new UUID(makeMsb(timestamp), now.getLeastSignificantBits) } }
Example 18
Source File: SlickDaoSupport.scala From scala-ddd-base with MIT License | 5 votes |
package com.github.j5ik2o.dddbase.slick import java.time.{ Instant, ZoneId, ZonedDateTime } trait SlickDaoSupport { val profile: slick.jdbc.JdbcProfile import profile.api._ implicit val zonedDateTimeColumnType = MappedColumnType.base[ZonedDateTime, java.sql.Timestamp]( { zdt => new java.sql.Timestamp(zdt.toInstant.toEpochMilli) }, { ts => val instant = Instant.ofEpochMilli(ts.getTime) ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()) } ) trait Record trait SoftDeletableRecord extends Record { val status: String } abstract class TableBase[T](_tableTag: Tag, _tableName: String, _schemaName: Option[String] = None) extends Table[T](_tableTag, _schemaName, _tableName) trait SoftDeletableTableSupport[T] { this: TableBase[T] => def status: Rep[String] } }
Example 19
Source File: TimePrinter.scala From get-programming-with-scala with MIT License | 5 votes |
package org.example.time import java.time.format.DateTimeFormatter import java.time.{ZoneId, ZonedDateTime} class TimePrinter(formatter: DateTimeFormatter) { def now(country: String): String = { val timezone = countryToTimezone(country) val dateTime = currentDateTime(timezone) dateTimeToString(dateTime) } private def countryToTimezone(country: String): String = country.toLowerCase match { case "italy" => "Europe/Rome" case "uk" => "Europe/London" case "germany" => "Europe/Berlin" case "japan" => "Asia/Tokyo" case _ => val msg = s"Unknown timezone for country $country" throw new IllegalStateException(msg) } private def currentDateTime(timezone: String): ZonedDateTime = { val zoneId = ZoneId.of(timezone) ZonedDateTime.now(zoneId) } private def dateTimeToString(dateTime: ZonedDateTime): String = formatter.format(dateTime) }
Example 20
Source File: TimePrinter.scala From get-programming-with-scala with MIT License | 5 votes |
package org.example.time import java.time.format.DateTimeFormatter import java.time.{ZoneId, ZonedDateTime} class TimePrinter(formatter: DateTimeFormatter) { def now(timezone: String): String = { val dateTime = currentDateTime(timezone) dateTimeToString(dateTime) } private def currentDateTime(timezone: String): ZonedDateTime = { val zoneId = ZoneId.of(timezone) ZonedDateTime.now(zoneId) } private def dateTimeToString(dateTime: ZonedDateTime): String = formatter.format(dateTime) }
Example 21
Source File: LogApiController.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.operation.controller import java.time.{ OffsetDateTime, ZoneId } import java.util.Date import akka.actor.{ ActorRef, Props } import akka.stream.actor.ActorPublisher import akka.stream.actor.ActorPublisherMessage.{ Cancel, Request } import akka.stream.scaladsl.Source import ch.qos.logback.classic.spi.ILoggingEvent import akka.http.scaladsl.model.sse.ServerSentEvent import io.vamp.common.Namespace import io.vamp.common.akka._ import io.vamp.common.json.{ OffsetDateTimeSerializer, SerializationFormat } import org.json4s.native.Serialization._ import scala.concurrent.duration.FiniteDuration case class LogEvent(logger: String, level: String, message: String, timestamp: OffsetDateTime) trait LogApiController extends AbstractController { private val eventType = "log" def sourceLog(level: String, logger: Option[String], keepAlivePeriod: FiniteDuration)(implicit namespace: Namespace): Source[ServerSentEvent, ActorRef] = { Source.actorPublisher[ServerSentEvent](Props(new ActorPublisher[ServerSentEvent] { def receive: Receive = { case Request(_) ⇒ openLogStream(self, level, logger, { event ⇒ ServerSentEvent(write(encode(event))(SerializationFormat(OffsetDateTimeSerializer)), eventType) }) case Cancel ⇒ closeLogStream(self) case sse: ServerSentEvent if totalDemand > 0 ⇒ onNext(sse) case _ ⇒ } })).keepAlive(keepAlivePeriod, () ⇒ ServerSentEvent.heartbeat) } def openLogStream(to: ActorRef, level: String, logger: Option[String], encoder: (ILoggingEvent) ⇒ AnyRef)(implicit namespace: Namespace): Unit = { LogPublisherHub.subscribe(to, level, logger, encoder) } def closeLogStream(to: ActorRef): Unit = LogPublisherHub.unsubscribe(to) def encode(loggingEvent: ILoggingEvent) = LogEvent( loggingEvent.getLoggerName, loggingEvent.getLevel.toString, loggingEvent.getFormattedMessage, OffsetDateTime.ofInstant(new Date(loggingEvent.getTimeStamp).toInstant, ZoneId.of("UTC")) ) }
Example 22
Source File: ClientSpec.scala From izanami with Apache License 2.0 | 5 votes |
package izanami import java.time.ZoneId import akka.actor.ActorSystem import akka.testkit.TestKit import com.typesafe.config.ConfigFactory import izanami.IzanamiBackend.SseBackend import izanami.scaladsl.IzanamiClient class ClientSpec extends IzanamiSpec { "client config" should { "config" in { //#configure-client implicit val system = ActorSystem( "izanami-client", ConfigFactory.parseString(""" izanami-example.blocking-io-dispatcher { type = Dispatcher executor = "thread-pool-executor" thread-pool-executor { fixed-pool-size = 32 } throughput = 1 } """) ) val client = IzanamiClient( ClientConfig( host = "http://localhost:9000", clientId = Some("xxxx"), clientIdHeaderName = "Another-Client-Id-Header", clientSecret = Some("xxxx"), clientSecretHeaderName = "Another-Client-Id-Header", backend = SseBackend, pageSize = 50, zoneId = ZoneId.of("Europe/Paris"), dispatcher = "izanami-example.blocking-io-dispatcher" ) ) //#configure-client TestKit.shutdownActorSystem(system) } } }
Example 23
Source File: CronExpression.scala From cuttle with Apache License 2.0 | 5 votes |
package com.criteo.cuttle.cron import cron4s.Cron import cron4s.lib.javatime._ import java.time.{Duration, Instant, ZoneId, ZoneOffset} import java.time.temporal.ChronoUnit import io.circe.{Encoder, Json} import io.circe.syntax._ import java.time.ZoneOffset import scala.concurrent.duration._ case class CronExpression(cronExpression: String, tz: ZoneId = ZoneOffset.UTC) { // https://www.baeldung.com/cron-expressions // https://www.freeformatter.com/cron-expression-generator-quartz.html private val cronExpr = Cron.unsafeParse(cronExpression) private def toZonedDateTime(instant: Instant) = instant.atZone(tz) def nextEvent(): Option[ScheduledAt] = { val instant = Instant.now() cronExpr.next(toZonedDateTime(instant)).map { next => // add 1 second as between doesn't include the end of the interval val delay = Duration.between(instant, next).get(ChronoUnit.SECONDS).seconds.plus(1.second) ScheduledAt(next.toInstant, delay) } } } object CronExpression { implicit val encodeUser: Encoder[CronExpression] = new Encoder[CronExpression] { override def apply(cronExpression: CronExpression) = Json.obj("expression" -> cronExpression.cronExpression.asJson, "tz" -> cronExpression.tz.getId.asJson) } }
Example 24
Source File: Rfc3339UtilTest.scala From api-first-hand with MIT License | 5 votes |
package de.zalando.play.controllers import java.time.{ LocalDateTime, ZoneId, ZoneOffset, ZonedDateTime } import org.scalatest.{ FunSpec, MustMatchers } class Rfc3339UtilTest extends FunSpec with MustMatchers { val dtz = ZoneId.of("UTC") val offset = ZoneOffset.UTC //noinspection ScalaStyle val date = ZonedDateTime.of(LocalDateTime.ofEpochSecond(1451911387L, 0, offset), dtz) describe("Rfc3339UtilTest") { it("should parse RFC3339 DateTime") { Rfc3339Util.parseDateTime("2007-05-01T15:43:26-00:00").withZoneSameInstant(dtz).toString mustBe "2007-05-01T15:43:26Z[UTC]" Rfc3339Util.parseDateTime("2007-05-01T15:43:26+00:00").withZoneSameInstant(dtz).toString mustBe "2007-05-01T15:43:26Z[UTC]" Rfc3339Util.parseDateTime("2007-05-01T15:43:26.3452-01:00").withZoneSameInstant(dtz).toString mustBe "2007-05-01T16:43:26.345200Z[UTC]" Rfc3339Util.parseDateTime("2007-05-01T15:43:26.3452+01:00").withZoneSameInstant(dtz).toString mustBe "2007-05-01T14:43:26.345200Z[UTC]" Rfc3339Util.parseDateTime("2007-05-01T15:43:26.3452+00:00").withZoneSameInstant(dtz).toString mustBe "2007-05-01T15:43:26.345200Z[UTC]" } it("should parse RFC3339 Date") { Rfc3339Util.parseDate("2007-05-01").toString mustBe "2007-05-01" Rfc3339Util.parseDate("2008-05-01").toString mustBe "2008-05-01" Rfc3339Util.parseDate("2007-08-01").toString mustBe "2007-08-01" Rfc3339Util.parseDate("2007-05-08").toString mustBe "2007-05-08" } it("should write DateTime") { Rfc3339Util.writeDateTime(date) mustBe "2016-01-04T12:43:07.0000+0000" } it("should write Date") { Rfc3339Util.writeDate(date.toLocalDate) mustBe "2016-01-04" } } }
Example 25
Source File: Rfc3339Util.scala From api-first-hand with MIT License | 5 votes |
package de.zalando.play.controllers import java.time.format.{ DateTimeFormatter, DateTimeParseException } import java.time.{ LocalDate, ZoneId, ZonedDateTime } object Rfc3339Util { private val fullDate = DateTimeFormatter.ofPattern("yyyy-MM-dd") private val shortDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ") private val shortDTWithTicks = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'") private val fullDTWithTicks = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'") private val dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSZ") def parseDateTime(datestring: String): ZonedDateTime = if (datestring.endsWith("Z") || datestring.endsWith("z")) parseFull(datestring) else parseParts(datestring) def parseDate(datestring: String): LocalDate = LocalDate.parse(datestring, fullDate) def writeDate(date: LocalDate): String = fullDate.format(date) def writeDateTime(date: ZonedDateTime): String = dateTime.format(date) private def parseParts(datestring: String): ZonedDateTime = { //step one, split off the timezone. val sepChar = if (datestring.indexOf('+') > 0) '+' else '-' val firstpart = datestring.substring(0, datestring.lastIndexOf(sepChar.toInt)) val secondpart = datestring.substring(datestring.lastIndexOf(sepChar.toInt)) //step two, remove the colon from the timezone offset val thirdpart = secondpart.substring(0, secondpart.indexOf(':')) + secondpart.substring(secondpart.indexOf(':') + 1) val dstring = firstpart + thirdpart try { ZonedDateTime.parse(dstring, shortDateTime) } catch { case pe: DateTimeParseException => ZonedDateTime.parse(dstring, dateTime) } } private def parseFull(datestring: String): ZonedDateTime = { val z = ZoneId.systemDefault() try { ZonedDateTime.parse(datestring, shortDTWithTicks.withZone(z)) } catch { case p: DateTimeParseException => ZonedDateTime.parse(datestring, fullDTWithTicks.withZone(z)) } } }
Example 26
Source File: TaggingAdapterSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.persistence import java.time.{Clock, Instant, ZoneId} import akka.persistence.journal.Tagged import cats.syntax.show._ import ch.epfl.bluebrain.nexus.iam.client.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.config.Schemas._ import ch.epfl.bluebrain.nexus.kg.config.Vocabulary._ import ch.epfl.bluebrain.nexus.kg.persistence.TaggingAdapterSpec.Other import ch.epfl.bluebrain.nexus.kg.resources.Event._ import ch.epfl.bluebrain.nexus.kg.resources.{Id, OrganizationRef, Ref} import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import io.circe.Json import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class TaggingAdapterSpec extends AnyWordSpecLike with Matchers with Inspectors with TestHelper { "A TaggingAdapter" should { val clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) def genJson(): Json = Json.obj("key" -> Json.fromString(genString())) val adapter = new TaggingAdapter() val orgRef = OrganizationRef(genUUID) val id = Id(ProjectRef(genUUID), nxv.projects) val mapping = Map( Set( s"type=${nxv.Schema.value.show}", s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event" ) -> Created(id, orgRef, Ref(shaclSchemaUri), Set(nxv.Schema, nxv.Resource), genJson(), clock.instant(), Anonymous), Set( s"type=${nxv.Resolver.value.show}", s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event" ) -> Updated(id, orgRef, 1L, Set(nxv.Resource, nxv.Resolver), genJson(), clock.instant(), Anonymous), Set(s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event") -> Deprecated(id, orgRef, 1L, Set(nxv.Resource), clock.instant(), Anonymous), Set(s"project=${id.parent.id}", s"org=${orgRef.show}", "event") -> TagAdded(id, orgRef, 2L, 1L, "tag", clock.instant(), Anonymous) ) "set the appropriate tags" in { forAll(mapping.toList) { case (tags, ev) => adapter.toJournal(ev) shouldEqual Tagged(ev, tags) } } "return an empty manifest" in { adapter.manifest(Other(genString())) shouldEqual "" } } } object TaggingAdapterSpec { private[persistence] final case class Other(value: String) }
Example 27
Source File: SparqlLinkSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.indexing import java.time.{Clock, Instant, ZoneId} import ch.epfl.bluebrain.nexus.commons.sparql.client.SparqlResults.Binding import ch.epfl.bluebrain.nexus.kg.config.Schemas._ import ch.epfl.bluebrain.nexus.kg.config.Vocabulary.nxv import ch.epfl.bluebrain.nexus.kg.indexing.SparqlLink.{SparqlExternalLink, SparqlResourceLink} import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.Vocabulary._ import ch.epfl.bluebrain.nexus.rdf.implicits._ import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class SparqlLinkSpec extends AnyWordSpecLike with Matchers with OptionValues { "A SparqlLink" should { val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) val id = url"http://example.com/id" val property = url"http://example.com/friend" val property2 = url"http://example.com/friend2" val paths = List(property, property2) "build SparqlExternalLink from SPARQL response" in { val bindings = Map( "s" -> Binding("uri", id.asString), "paths" -> Binding("literal", s"${property.asString} ${property2.asString}") ) SparqlExternalLink(bindings).value shouldEqual SparqlExternalLink(id, paths) } "build SparqlResourceLink from SPARQL response" in { val self = url"http://127.0.0.1:8080/v1/resources/myorg/myproject/_/id" val project = url"http://127.0.0.1:8080/v1/projects/myorg/myproject/" val author = url"http://127.0.0.1:8080/v1/realms/myrealm/users/me" val bindings = Map( "s" -> Binding("uri", id.asString), "paths" -> Binding("literal", s"${property.asString} ${property2.asString}"), "_rev" -> Binding("literal", "1", datatype = Some(xsd.long.asString)), "_self" -> Binding("uri", self.asString), "_project" -> Binding("uri", project.asString), "types" -> Binding("literal", s"${nxv.Resolver.asString} ${nxv.Schema.asString}"), "_constrainedBy" -> Binding("uri", unconstrainedSchemaUri.asString), "_createdBy" -> Binding("uri", author.asString), "_updatedBy" -> Binding("uri", author.asString), "_createdAy" -> Binding("uri", author.asString), "_createdAt" -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)), "_updatedAt" -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)), "_deprecated" -> Binding("literal", "false", datatype = Some(xsd.boolean.asString)) ) SparqlResourceLink(bindings).value shouldEqual SparqlResourceLink( id, project, self, 1L, Set[AbsoluteIri](nxv.Schema, nxv.Resolver), false, clock.instant(), clock.instant(), author, author, unconstrainedRef, paths ) } } }
Example 28
Source File: ArchiveCacheSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.archives import java.time.{Clock, Instant, ZoneId} import cats.effect.{IO, Timer} import ch.epfl.bluebrain.nexus.admin.client.types.Project import ch.epfl.bluebrain.nexus.commons.test.ActorSystemFixture import ch.epfl.bluebrain.nexus.commons.test.io.IOOptionValues import ch.epfl.bluebrain.nexus.iam.client.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.archives.Archive.{File, Resource, ResourceDescription} import ch.epfl.bluebrain.nexus.kg.config.Settings import ch.epfl.bluebrain.nexus.kg.resources.Id import ch.epfl.bluebrain.nexus.kg.resources.syntax._ import org.scalatest.concurrent.Eventually import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class ArchiveCacheSpec extends ActorSystemFixture("ArchiveCacheSpec", true) with TestHelper with AnyWordSpecLike with Matchers with IOOptionValues with Eventually { override implicit def patienceConfig: PatienceConfig = PatienceConfig(10.second, 50.milliseconds) private val appConfig = Settings(system).appConfig private implicit val config = appConfig.copy(archives = appConfig.archives.copy(cacheInvalidateAfter = 500.millis, maxResources = 100)) private implicit val timer: Timer[IO] = IO.timer(system.dispatcher) private val cache: ArchiveCache[IO] = ArchiveCache[IO].unsafeToFuture().futureValue private implicit val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault()) private val instant = clock.instant() def randomProject() = { val instant = Instant.EPOCH // format: off Project(genIri, genString(), genString(), None, genIri, genIri, Map.empty, genUUID, genUUID, 1L, false, instant, genIri, instant, genIri) // format: on } "An archive cache" should { "write and read an Archive" in { val resId = Id(randomProject().ref, genIri) val resource1 = Resource(genIri, randomProject(), None, None, originalSource = true, None) val file1 = File(genIri, randomProject(), None, None, None) val archive = Archive(resId, instant, Anonymous, Set(resource1, file1)) val _ = cache.put(archive).value.some cache.get(archive.resId).value.some shouldEqual archive } "read a non existing resource" in { val resId = Id(randomProject().ref, genIri) cache.get(resId).value.ioValue shouldEqual None } "read after timeout" in { val resId = Id(randomProject().ref, genIri) val set = Set[ResourceDescription](Resource(genIri, randomProject(), None, None, originalSource = true, None)) val archive = Archive(resId, instant, Anonymous, set) val _ = cache.put(archive).value.some val time = System.currentTimeMillis() cache.get(resId).value.some shouldEqual archive eventually { cache.get(resId).value.ioValue shouldEqual None } val diff = System.currentTimeMillis() - time diff should be > config.archives.cacheInvalidateAfter.toMillis diff should be < config.archives.cacheInvalidateAfter.toMillis + 300 } } }
Example 29
Source File: TarFlowSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.archives import java.nio.file.Files import java.time.{Clock, Instant, ZoneId} import akka.actor.ActorSystem import akka.stream.scaladsl.FileIO import akka.testkit.TestKit import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.storage.digestSink import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class TarFlowSpec extends TestKit(ActorSystem("TarFlowSpec")) with AnyWordSpecLike with Matchers with TestHelper with ScalaFutures { private implicit val ec = system.dispatcher private implicit val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault()) override implicit def patienceConfig: PatienceConfig = PatienceConfig(55.second, 150.milliseconds) "A TarFlow" should { "tar a bunch of sources" in { val digest = "3fef41c5afe7a7ee11ee9d556a564fb57784cc5247b24c6ca70783f396fa158a1c7952504d3e1aa441de20cf065d740eec454c6ffb7fbc4b6351b950ee51c886" val elems = 500 val contents = List.tabulate(2) { i => val content = (i until (i + elems)).toList.mkString(",") + "\n" ArchiveSource(content.length.toLong, s"some/path$i/$i.txt", produce(content)) } val path = Files.createTempFile("test", ".tar") TarFlow.write(contents).runWith(FileIO.toPath(path)).futureValue FileIO.fromPath(path).runWith(digestSink("SHA-512")).futureValue.value shouldEqual digest Files.delete(path) } } }
Example 30
Source File: ISO8601DateTime.scala From toketi-iothubreact with MIT License | 5 votes |
// Copyright (c) Microsoft. All rights reserved. package F_SendMessageToDevice import java.time.format.DateTimeFormatter import java.time.{ZoneId, ZonedDateTime} import scala.util.matching.Regex case class ISO8601DateTime(text: String) { private lazy val pattern1: Regex = """(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2}).(\d{1,3})Z""".r private lazy val pattern2: Regex = """(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})Z""".r private lazy val pattern3: Regex = """(\d{4})-(\d{1,2})-(\d{1,2})""".r private lazy val format : DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") private lazy val zone : ZoneId = ZoneId.of("UTC") private lazy val zonedDateTime: ZonedDateTime = { text match { case pattern1(y, m, d, h, i, s, n) ⇒ val ni = n.toInt val nanos = if (ni > 99) ni * 1000000 else if (ni > 9) ni * 10000000 else ni * 100000000 ZonedDateTime.of(y.toInt, m.toInt, d.toInt, h.toInt, i.toInt, s.toInt, nanos, zone) case pattern2(y, m, d, h, i, s) ⇒ ZonedDateTime.of(y.toInt, m.toInt, d.toInt, h.toInt, i.toInt, s.toInt, 0, zone) case pattern3(y, m, d) ⇒ ZonedDateTime.of(y.toInt, m.toInt, d.toInt, 0, 0, 0, 0, zone) case null ⇒ null case _ ⇒ throw new Exception(s"wrong date time format: $text") } } override def toString: String = if (zonedDateTime == null) "" else zonedDateTime.format(format) }
Example 31
Source File: CronGenerator.scala From lemon-schedule with GNU General Public License v2.0 | 5 votes |
package com.gabry.job.utils import java.time.temporal.ChronoField import java.time.{Instant, ZoneId, ZonedDateTime} import java.util.Locale import com.cronutils.descriptor.CronDescriptor import com.cronutils.model.CronType import com.cronutils.model.definition.CronDefinitionBuilder import com.cronutils.model.time.ExecutionTime import com.cronutils.parser.CronParser def isValid(cronExpression:String):Boolean = { try{ parser.parse(cronExpression) true }catch{ case ex:Exception => false } } }
Example 32
Source File: package.scala From modelmatrix with Apache License 2.0 | 5 votes |
package com.collective.modelmatrix import java.time.ZoneId import java.time.format.{FormatStyle, DateTimeFormatter} import java.util.Locale import com.bethecoder.ascii_table.{ASCIITableHeader, ASCIITable} import com.collective.modelmatrix.transform.{Index, Top, Identity, Transform} import scala.language.implicitConversions package object cli { val timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) .withLocale(Locale.US) .withZone(ZoneId.systemDefault()) implicit def stringToASCIITableHeader(s: String): ASCIITableHeader = new ASCIITableHeader(s) implicit class StringOps(val s: String) extends AnyVal { def dataLeftAligned: ASCIITableHeader = new ASCIITableHeader(s, -1) } import scalaz.stream._ import scalaz.concurrent.Task implicit class ConcurrentProcess[O](val process: Process[Task, O]) { def concurrently[O2](concurrencyLevel: Int) (f: Channel[Task, O, O2]): Process[Task, O2] = { val actions = process. zipWith(f)((data, f) => f(data)) val nestedActions = actions.map(Process.eval) merge.mergeN(concurrencyLevel)(nestedActions) } } }
Example 33
Source File: ModelMatrixUDF.scala From modelmatrix with Apache License 2.0 | 5 votes |
package com.collective.modelmatrix import java.time.ZoneId import java.{lang => jl} import org.apache.spark.sql.UDFRegistration trait ModelMatrixUDF { private val dayOfWeek: (jl.Long, String) => jl.Integer = { case (ts, zoneId) if ts != null => ModelMatrixFunctions.dayOfWeek(ts, ZoneId.of(zoneId)) case _ => null } private val hourOfDay: (jl.Long, String) => jl.Integer = { case (ts, zoneId) if ts != null => ModelMatrixFunctions.hourOfDay(ts, ZoneId.of(zoneId)) case _ => null } private val nvlString: (String, String) => String = ModelMatrixFunctions.nvl[String] private val nvl: (jl.Double, jl.Double) => jl.Double = ModelMatrixFunctions.nvl[jl.Double] private val strToEpochMilli: String => jl.Long = { case s if s != null => s.toLong case _ => null } private val concat: (String, String, String) => String = { case (sep, s1, s2) if sep != null && s1 != null && s2 != null => ModelMatrixFunctions.concat(sep, s1, s2) case _ => null } private val log: jl.Double => jl.Double = { case d if d != null => math.log(d).asInstanceOf[jl.Double] case _ => null } private val greatest: (jl.Double, jl.Double) => jl.Double = { case (l, r) if l != null && r != null => math.max(l, r).asInstanceOf[jl.Double] case _ => null } protected def registerUDF(udf: UDFRegistration): Unit = { udf.register("strToEpochMilli", strToEpochMilli) udf.register("concat", concat) udf.register("day_of_week", dayOfWeek) udf.register("hour_of_day", hourOfDay) udf.register("nvl_str", nvlString) udf.register("nvl", nvl) udf.register("log", log) udf.register("greatest", greatest) } }
Example 34
Source File: ComponentSupport.scala From akka-ddd-cqrs-es-example with MIT License | 5 votes |
package com.github.j5ik2o.bank.adaptor.dao import java.time.{ Instant, ZoneId, ZonedDateTime } trait ComponentSupport { val profile: slick.jdbc.JdbcProfile import profile.api._ implicit val zonedDateTimeColumnType = MappedColumnType.base[ZonedDateTime, java.sql.Timestamp]( { zdt => new java.sql.Timestamp(zdt.toInstant.toEpochMilli) }, { ts => val instant = Instant.ofEpochMilli(ts.getTime) ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()) } ) trait DaoSupport[Id, Entity] trait Record { val id: Long } trait SoftDeletableRecord extends Record { val status: String } abstract class TableBase[T](_tableTag: Tag, _tableName: String, _schemaName: Option[String] = None) extends Table[T](_tableTag, _schemaName, _tableName) { def id: Rep[Long] = column[Long]("id", O.PrimaryKey, O.AutoInc) } trait SoftDeletableTableSupport[T] { this: TableBase[T] => def status: Rep[String] } }
Example 35
Source File: InMemoryStore.scala From slab with Apache License 2.0 | 5 votes |
package com.criteo.slab.lib import java.time.format.{DateTimeFormatter, FormatStyle} import java.time.temporal.ChronoUnit import java.time.{Instant, ZoneId} import java.util.concurrent.{Executors, TimeUnit} import com.criteo.slab.core.{Codec, Context, Store} import com.criteo.slab.lib.Values.Slo import org.slf4j.{Logger, LoggerFactory} import scala.collection.concurrent.TrieMap import scala.concurrent.Future import scala.util.Try class InMemoryStore( val expiryDays: Int = 30 ) extends Store[Any] { private val logger = LoggerFactory.getLogger(this.getClass) private val cache = TrieMap.empty[(String, Long), Any] private val scheduler = Executors.newSingleThreadScheduledExecutor() scheduler.scheduleAtFixedRate(InMemoryStore.createCleaner(cache, expiryDays, logger), 1, 1, TimeUnit.HOURS) logger.info(s"InMemoryStore started, entries expire in $expiryDays days") sys.addShutdownHook { logger.info(s"Shutting down...") scheduler.shutdown() } override def upload[T](id: String, context: Context, v: T)(implicit codec: Codec[T, Any]): Future[Unit] = { logger.debug(s"Uploading $id") Future.successful { cache.putIfAbsent((id, context.when.toEpochMilli), codec.encode(v)) logger.info(s"Store updated, size: ${cache.size}") } } override def uploadSlo(id: String, context: Context, slo: Slo)(implicit codec: Codec[Slo, Any]): Future[Unit] = { upload[Slo](id, context, slo) } def fetchSloHistory(id: String, from: Instant, until: Instant)(implicit codec: Codec[Slo, Any]): Future[Seq[(Long, Slo)]] = { fetchHistory[Slo](id, from, until)(codec) } override def fetch[T](id: String, context: Context)(implicit codec: Codec[T, Any]): Future[Option[T]] = { logger.debug(s"Fetching $id") Future.successful { cache.get((id, context.when.toEpochMilli)) map { v => codec.decode(v).get } } } override def fetchHistory[T]( id: String, from: Instant, until: Instant )(implicit ev: Codec[T, Any]): Future[Seq[(Long, T)]] = { logger.debug(s"Fetching the history of $id from ${format(from)} until ${format(until)}, cache size: ${cache.size}") Future.successful { cache.withFilter { case ((_id, ts), _) => _id == id && ts >= from.toEpochMilli && ts <= until.toEpochMilli }.map { case ((_, ts), repr) => (ts, ev.decode(repr).get) }.toList } } private def format(i: Instant) = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL) .withZone(ZoneId.systemDefault) .format(i) } object InMemoryStore { implicit def codec[T] = new Codec[T, Any] { override def encode(v: T): Any = v override def decode(v: Any): Try[T] = Try(v.asInstanceOf[T]) } def createCleaner(cache: TrieMap[(String, Long), Any], expiryDays: Int, logger: Logger): Runnable = { object C extends Runnable { override def run(): Unit = { val expired = cache.filterKeys(_._2 <= Instant.now.minus(expiryDays, ChronoUnit.DAYS).toEpochMilli).keys logger.debug(s"${expired.size} out of ${cache.size} entries have expired, cleaning up...") cache --= expired } } C } }
Example 36
Source File: ScheduleSpecTest.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome.jobspec.impl import java.time.{Instant, ZoneId} import dcos.metronome.model.{ConcurrencyPolicy, CronSpec, ScheduleSpec} import dcos.metronome.utils.test.Mockito import org.scalatest.{FunSuite, GivenWhenThen, Matchers} import scala.concurrent.duration._ class ScheduleSpecTest extends FunSuite with Matchers with Mockito with GivenWhenThen { test("nextExecution when close to daylight savings") { val schedule = ScheduleSpec( id = "default", cron = CronSpec("55 23 * * *"), timeZone = ZoneId.of("Europe/Rome"), startingDeadline = 900.seconds, concurrencyPolicy = ConcurrencyPolicy.Allow, enabled = true ) Given("a schedule that was last run at 22:55") val lastScheduledAt = Instant.parse("2019-03-30T22:55:00.000Z") When("we are now close to midnight and compute the next scheduled time") val now = Instant.parse("2019-03-30T23:54:59.000Z") val nextTime = schedule.nextExecution(lastScheduledAt) // 60 secs is the smallest unit of reschedule time for cron val inSeconds = Math.max(java.time.Duration.between(now, nextTime).getSeconds, 60) println(s"now is $now, nextScheduleIn = $inSeconds seconds, next run is scheduled for: $nextTime") Then("The next run should be scheduled on the 31st") val expected = Instant.parse("2019-03-31T21:55:00Z") nextTime shouldEqual expected } }
Example 37
Source File: SettableClock.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome import java.time.{Clock, Instant, LocalDateTime, ZoneOffset, ZoneId, Duration} import scala.concurrent.duration.FiniteDuration object SettableClock { private val defaultJavaClock = Clock.fixed(LocalDateTime.of(2015, 4, 9, 12, 30, 0).toInstant(ZoneOffset.UTC), ZoneOffset.UTC) def ofNow() = new SettableClock(Clock.fixed(Instant.now(), ZoneOffset.UTC)) } class SettableClock(private[this] var clock: Clock = SettableClock.defaultJavaClock) extends Clock { private[this] var subscribers: List[() => Unit] = Nil def onChange(fn: () => Unit): Unit = synchronized { subscribers = fn :: subscribers } override def getZone: ZoneId = clock.getZone override def instant(): Instant = clock.instant() override def withZone(zoneId: ZoneId): Clock = new SettableClock(clock.withZone(zoneId)) def +=(duration: FiniteDuration): Unit = plus(duration) def plus(duration: FiniteDuration): this.type = plus(Duration.ofMillis(duration.toMillis)) def plus(duration: Duration): this.type = { clock = Clock.offset(clock, duration) subscribers.foreach(_()) this } def at(instant: Instant): this.type = { clock = Clock.fixed(instant, clock.getZone) subscribers.foreach(_()) this } }
Example 38
Source File: ScheduleSpec.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome package model import java.time.{Clock, Instant, ZoneId, ZonedDateTime} import com.wix.accord.Validator import com.wix.accord.dsl._ import scala.concurrent.duration._ case class ScheduleSpec( id: String, cron: CronSpec, timeZone: ZoneId = ScheduleSpec.DefaultTimeZone, startingDeadline: Duration = ScheduleSpec.DefaultStartingDeadline, concurrencyPolicy: ConcurrencyPolicy = ScheduleSpec.DefaultConcurrencyPolicy, enabled: Boolean = ScheduleSpec.DefaultEnabled ) { def clock: Clock = ScheduleSpec.DefaultClock def nextExecution(after: Instant): Instant = { val localAfter = ZonedDateTime.ofInstant(after, timeZone) val localNext = cron.nextExecution(localAfter) localNext.toInstant } def nextExecution(): Instant = nextExecution(clock.instant()) } object ScheduleSpec { val DefaultTimeZone = ZoneId.of("UTC") val DefaultStartingDeadline = 15.minutes val DefaultConcurrencyPolicy = ConcurrencyPolicy.Allow val DefaultEnabled = true val DefaultClock = Clock.systemUTC() implicit lazy val validScheduleSpec: Validator[ScheduleSpec] = validator[ScheduleSpec] { spec => spec.startingDeadline >= 1.minute } }
Example 39
Source File: ZonedDateTimeEqualityTest.scala From dtc with Apache License 2.0 | 5 votes |
package dtc.tests import java.time.{Instant, ZoneId, ZonedDateTime} import org.scalatest.matchers.should.Matchers import org.scalatest.funsuite.AnyFunSuiteLike class ZonedDateTimeEqualityTest extends AnyFunSuiteLike with Matchers { private val instant = 1431231233567L private val x = ZonedDateTime.ofInstant(Instant.ofEpochMilli(instant), ZoneId.of("Canada/Pacific")) private val y = ZonedDateTime.ofInstant(Instant.ofEpochMilli(instant), ZoneId.of("America/Vancouver")) test("strict equality") { dtc.instances.zonedDateTime.zonedDateTimeWithStrictEquality.eqv(x, y) shouldBe false } test("cross-zone equality") { dtc.instances.zonedDateTime.zonedDateTimeWithCrossZoneEquality.eqv(x, y) shouldBe true } }
Example 40
Source File: ExportEndpointTest.scala From temperature-machine with Apache License 2.0 | 5 votes |
package bad.robot.temperature.server import java.time.ZoneId import java.time.format.DateTimeFormatter import java.time.format.FormatStyle.SHORT import java.util.Locale._ import cats.effect.IO import org.http4s.Method.GET import org.http4s.Status.Ok import org.http4s.implicits._ import org.http4s.{Request, Uri} import org.specs2.mutable.Specification import scalaz.syntax.either._ class ExportEndpointTest extends Specification { sequential "convert json to csv" >> { val exampleJson = """ |[ | { | "label": "bedroom1-sensor-1", | "data": [ | { | "x": 1507709610000, | "y": "NaN" | }, | { | "x": 1507709640000, | "y": "+2.2062500000E01" | }, | { | "x": 1507709680000, | "y": "+2.2262500000E01" | } | ] | } |] """.stripMargin val expectedCsv = """"Sensor","Time","Temperature","Difference" |"bedroom1-sensor-1","11/10/17 08:13","NaN","0" |"bedroom1-sensor-1","11/10/17 08:14","22.0625","NaN" |"bedroom1-sensor-1","11/10/17 08:14","22.2625","0.20"""".stripMargin val UkDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(SHORT).withLocale(UK).withZone(ZoneId.of("GMT")) val request = Request[IO](GET, Uri.uri("/temperatures.csv")) val service = ExportEndpoint(exampleJson.right, UkDateTimeFormatter) val response = service.orNotFound.run(request).unsafeRunSync() response.as[String].unsafeRunSync must_== expectedCsv response.status must_== Ok } }
Example 41
Source File: TemperaturesTest.scala From temperature-machine with Apache License 2.0 | 5 votes |
package bad.robot.temperature.server import java.time.{Clock, Instant, ZoneId} import bad.robot.temperature._ import bad.robot.temperature.rrd.{Host, Seconds} import org.specs2.mutable.Specification class TemperaturesTest extends Specification { "Filter out old temperatures" >> { val measurement1 = Measurement(Host("lounge"), Seconds(0), List( SensorReading("28-00000dfg34ca", Temperature(31.1)), SensorReading("28-00000f33fdc3", Temperature(32.8)) )) val measurement2 = Measurement(Host("bedroom"), Seconds(120), List( SensorReading("28-00000f3554ds", Temperature(21.1)), SensorReading("28-000003dd3433", Temperature(22.8)) )) val temperatures = CurrentTemperatures(fixedClock(Instant.ofEpochSecond(300))) temperatures.updateWith(measurement1) temperatures.updateWith(measurement2) val expected = Map( Host("bedroom", None) -> Measurement(Host("bedroom"), Seconds(120), List( SensorReading("28-00000f3554ds", Temperature(21.1)), SensorReading("28-000003dd3433", Temperature(22.8)) )) ) temperatures.all must_== expected } "Filter out old temperatures when averaging" >> { val measurement1 = Measurement(Host("lounge"), Seconds(0), List( SensorReading("28-00000dfg34ca", Temperature(31.1)), SensorReading("28-00000f33fdc3", Temperature(32.8)) )) val measurement2 = Measurement(Host("bedroom"), Seconds(60), List( SensorReading("28-00000f3554ds", Temperature(21.1)), SensorReading("28-000003dd3433", Temperature(22.8)) )) val temperatures = CurrentTemperatures(fixedClock(Instant.ofEpochSecond(300))) temperatures.updateWith(measurement1) temperatures.updateWith(measurement2) val expected = Map( Host("bedroom", None) -> Measurement(Host("bedroom"), Seconds(60), List( SensorReading("Average", Temperature(21.950000000000003)) )) ) temperatures.average must_== expected } def fixedClock(instant: Instant = Instant.now) = Clock.fixed(instant, ZoneId.systemDefault()) }
Example 42
Source File: LogTimestampFormatter.scala From airframe with Apache License 2.0 | 5 votes |
package wvlet.log import java.time.format.{DateTimeFormatterBuilder, SignStyle} import java.time.{Instant, ZoneId, ZonedDateTime} import java.util.Locale object LogTimestampFormatter { import java.time.temporal.ChronoField._ val systemZone = ZoneId.systemDefault().normalized() val noSpaceTimestampFormat = new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral('-') .appendValue(MONTH_OF_YEAR, 2) .appendLiteral('-') .appendValue(DAY_OF_MONTH, 2) .appendLiteral('T') .appendValue(HOUR_OF_DAY, 2) .appendLiteral(':') .appendValue(MINUTE_OF_HOUR, 2) .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 2) .appendLiteral('.') .appendValue(MILLI_OF_SECOND, 3) .appendOffset("+HHMM", "Z") .toFormatter(Locale.US) val humanReadableTimestampFormatter = new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral('-') .appendValue(MONTH_OF_YEAR, 2) .appendLiteral('-') .appendValue(DAY_OF_MONTH, 2) .appendLiteral(' ') .appendValue(HOUR_OF_DAY, 2) .appendLiteral(':') .appendValue(MINUTE_OF_HOUR, 2) .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 2) .appendLiteral('.') .appendValue(MILLI_OF_SECOND, 3) .appendOffset("+HHMM", "Z") .toFormatter(Locale.US) def formatTimestamp(timeMillis: Long): String = { val timestamp = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), systemZone) humanReadableTimestampFormatter.format(timestamp) } def formatTimestampWithNoSpaace(timeMillis: Long): String = { val timestamp = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), systemZone) noSpaceTimestampFormat.format(timestamp) } }
Example 43
Source File: package.scala From zio with Apache License 2.0 | 5 votes |
package zio import java.time.{ DateTimeException, Instant, OffsetDateTime, ZoneId } import java.util.concurrent.TimeUnit import zio.duration.Duration package object clock { type Clock = Has[Clock.Service] object Clock extends PlatformSpecific with Serializable { trait Service extends Serializable { def currentTime(unit: TimeUnit): UIO[Long] def currentDateTime: IO[DateTimeException, OffsetDateTime] def nanoTime: UIO[Long] def sleep(duration: Duration): UIO[Unit] } object Service { val live: Service = new Service { def currentTime(unit: TimeUnit): UIO[Long] = IO.effectTotal(System.currentTimeMillis).map(l => unit.convert(l, TimeUnit.MILLISECONDS)) val nanoTime: UIO[Long] = IO.effectTotal(System.nanoTime) def sleep(duration: Duration): UIO[Unit] = UIO.effectAsyncInterrupt { cb => val canceler = globalScheduler.schedule(() => cb(UIO.unit), duration) Left(UIO.effectTotal(canceler())) } def currentDateTime: IO[DateTimeException, OffsetDateTime] = { val dateTime = for { millis <- currentTime(TimeUnit.MILLISECONDS) zone <- ZIO(ZoneId.systemDefault) instant <- ZIO(Instant.ofEpochMilli(millis)) offsetDateTime <- ZIO(OffsetDateTime.ofInstant(instant, zone)) } yield offsetDateTime dateTime.refineToOrDie[DateTimeException] } } } val any: ZLayer[Clock, Nothing, Clock] = ZLayer.requires[Clock] val live: Layer[Nothing, Clock] = ZLayer.succeed(Service.live) } def sleep(duration: => Duration): URIO[Clock, Unit] = ZIO.accessM(_.get.sleep(duration)) }
Example 44
Source File: BqResultSetTest.scala From scalikejdbc-bigquery with Apache License 2.0 | 5 votes |
package scalikejdbc.bigquery import java.time.{LocalDate, LocalTime, ZoneId, ZonedDateTime} import com.google.cloud.bigquery._ import org.scalatest.flatspec.AnyFlatSpec class BqResultSetTest extends AnyFlatSpec { it should "be able to instantiate from null Schema" in { val tableResult = MockUtil.tableResultFromSeq(Nil, null) new BqResultSet(tableResult) } it should "correctly traversable" in { val row1 = Seq(BqParameter.String("first")).map(MockUtil.fieldValueFromParameter(_)) val row2 = Seq(BqParameter.String("second")).map(MockUtil.fieldValueFromParameter(_)) val row3 = Seq(BqParameter.String("third")).map(MockUtil.fieldValueFromParameter(_)) val schema = Schema.of(Field.of("name", LegacySQLTypeName.STRING)); val queryResult = MockUtil.tableResultFromSeq(Seq(row1, row2, row3), schema) val resultSet = new BqResultSet(queryResult) assert(resultSet.next()) assert(resultSet.next()) assert(resultSet.next()) assert(!resultSet.next()) } it should "correctly get value" in { val row = Seq( BqParameter.Int64(42L), BqParameter.Float64(3.14159), BqParameter.Bool(true), BqParameter.String("hello"), BqParameter.Bytes(Array[Byte](104, 101, 108, 108, 111)), BqParameter.Date(LocalDate.of(2017, 3, 22)), // BqParameter.DateTime BqParameter.Time(LocalTime.of(19, 58, 0, 0)), BqParameter.Timestamp(ZonedDateTime.of(2017, 3, 22, 19, 58, 0, 0, ZoneId.of("Asia/Tokyo"))) ).map(MockUtil.fieldValueFromParameter(_)) val fields = Seq( Field.of("int64_column", LegacySQLTypeName.INTEGER), Field.of("float64_column", LegacySQLTypeName.FLOAT), Field.of("bool_column", LegacySQLTypeName.BOOLEAN), Field.of("string_column", LegacySQLTypeName.STRING), Field.of("bytes_column", LegacySQLTypeName.BYTES), Field.of("date_column", LegacySQLTypeName.STRING), Field.of("time_column", LegacySQLTypeName.STRING), Field.of("timestamp_column", LegacySQLTypeName.TIMESTAMP) ) val schema = Schema.of(fields: _*) val queryResult = MockUtil.tableResultFromSeq(Seq(row), schema) val resultSet = new BqResultSet(queryResult) assert(resultSet.next()) // int64 assert(resultSet.getInt(0) == 42) assert(resultSet.getInt("int64_column") == 42) // float64 assert(resultSet.getDouble(1) == 3.14159) assert(resultSet.getDouble("float64_column") == 3.14159) // bool assert(resultSet.getBoolean(2) == true) assert(resultSet.getBoolean("bool_column") == true) // string assert(resultSet.getString(3) == "hello") assert(resultSet.getString("string_column") == "hello") // bytes assert(resultSet.getBytes(4).sameElements(Array[Byte](104, 101, 108, 108, 111))) assert(resultSet.getBytes("bytes_column").sameElements(Array[Byte](104, 101, 108, 108, 111))) // date assert(resultSet.getDate(5).toLocalDate == LocalDate.of(2017, 3, 22)) assert(resultSet.getDate("date_column").toLocalDate == LocalDate.of(2017, 3, 22)) // time assert(resultSet.getTime(6).toLocalTime == LocalTime.of(19, 58, 0, 0)) assert(resultSet.getTime("time_column").toLocalTime == LocalTime.of(19, 58, 0, 0)) // timestamp assert(resultSet.getTimestamp(7).toInstant == ZonedDateTime.of(2017, 3, 22, 19, 58, 0, 0, ZoneId.of("Asia/Tokyo")).toInstant) assert(resultSet.getTimestamp("timestamp_column").toInstant == ZonedDateTime.of(2017, 3, 22, 19, 58, 0, 0, ZoneId.of("Asia/Tokyo")).toInstant) } }
Example 45
Source File: QueryRequestBuilder.scala From scalikejdbc-bigquery with Apache License 2.0 | 5 votes |
package scalikejdbc import java.time.ZoneId import com.google.cloud.bigquery.{QueryJobConfiguration, QueryParameterValue} import scalikejdbc.bigquery.{BqParameter, BqPreparedStatement, Format} import scala.collection.JavaConverters._ import scala.language.reflectiveCalls object QueryRequestBuilder { private val LocalDateEpoch = java.time.LocalDate.ofEpochDay(0) def apply(statement: SQLSyntax): QueryJobConfiguration.Builder = { val builder = QueryJobConfiguration.newBuilder(statement.value) val ps = new BqPreparedStatement // almost same implementation as scalikejdbc.StatementExecutor statement.rawParameters.zipWithIndex.foreach { case (param, index) => param match { case binder: ParameterBinder => binder(ps, index) case p: BigDecimal => ps.setBigDecimal(index, p.bigDecimal) case p: BigInt => ps.setBigDecimal(index, new java.math.BigDecimal(p.bigInteger)) case p: Boolean => ps.setBoolean(index, p) case p: Byte => ps.setByte(index, p) case p: java.sql.Date => ps.setDate(index, p) case p: Double => ps.setDouble(index, p) case p: Float => ps.setFloat(index, p) case p: Int => ps.setInt(index, p) case p: Long => ps.setLong(index, p) case p: Short => ps.setShort(index, p) case p: String => ps.setString(index, p) case p: java.sql.Time => ps.setTime(index, p) case p: java.sql.Timestamp => ps.setTimestamp(index, p) case p: java.util.Date => ps.setTimestamp(index, p.toSqlTimestamp) case p: java.time.ZonedDateTime => ps.setTimestamp(index, java.sql.Timestamp.from(p.toInstant)) case p: java.time.OffsetDateTime => ps.setTimestamp(index, java.sql.Timestamp.from(p.toInstant)) case p: java.time.Instant => ps.setTimestamp(index, java.sql.Timestamp.from(p)) case p: java.time.LocalDateTime => ps.setTimestamp(index, java.sql.Timestamp.valueOf(p)) case p: java.time.LocalDate => ps.setDate(index, java.sql.Date.valueOf(p)) case p: java.time.LocalTime => val millis = p.atDate(LocalDateEpoch).atZone(java.time.ZoneId.systemDefault).toInstant.toEpochMilli val time = new java.sql.Time(millis) ps.setTime(index, time) case p => param.getClass.getCanonicalName match { case "org.joda.time.DateTime" => val t = p.asInstanceOf[ {def toDate: java.util.Date}].toDate.toSqlTimestamp ps.setTimestamp(index, t) case "org.joda.time.LocalDateTime" => val t = p.asInstanceOf[ {def toDate: java.util.Date}].toDate.toSqlTimestamp ps.setTimestamp(index, t) case "org.joda.time.LocalDate" => val t = p.asInstanceOf[ {def toDate: java.util.Date}].toDate.toSqlDate ps.setDate(index, t) case "org.joda.time.LocalTime" => val millis = p.asInstanceOf[ {def toDateTimeToday: {def getMillis: Long}}].toDateTimeToday.getMillis ps.setTime(index, new java.sql.Time(millis)) case _ => throw new UnsupportedOperationException( s"unsupported parameter type. index: ${index}, parameter : ${param}, class: ${param.getClass}") } } } val parameters = ps.parameters.toList .sortBy { case (parameterIndex, _) => parameterIndex } .map { case (_, parameter) => parameter match { case BqParameter.Int64(value) => QueryParameterValue.int64(value) case BqParameter.Float64(value) => QueryParameterValue.float64(value) case BqParameter.Bool(value) => QueryParameterValue.bool(value) case BqParameter.String(value) => QueryParameterValue.string(value) case BqParameter.Bytes(value) => QueryParameterValue.bytes(value) case BqParameter.Date(value) => QueryParameterValue.date(value.format(Format.date)) case BqParameter.DateTime(value) => QueryParameterValue.dateTime(value.format(Format.dateTime)) case BqParameter.Time(value) => QueryParameterValue.time(value.format(Format.time)) case BqParameter.Timestamp(value) => QueryParameterValue.timestamp(value.withZoneSameInstant(ZoneId.of("UTC")).format(Format.timestamp)) } }.asJava builder.setPositionalParameters(parameters) } }
Example 46
Source File: DateFormatter.scala From XSQL with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.catalyst.util import java.time.{Instant, ZoneId} import java.util.Locale import org.apache.spark.sql.catalyst.util.DateTimeUtils.instantToDays sealed trait DateFormatter extends Serializable { def parse(s: String): Int // returns days since epoch def format(days: Int): String } class Iso8601DateFormatter( pattern: String, locale: Locale) extends DateFormatter with DateTimeFormatterHelper { @transient private lazy val formatter = getOrCreateFormatter(pattern, locale) private val UTC = ZoneId.of("UTC") private def toInstant(s: String): Instant = { val temporalAccessor = formatter.parse(s) toInstantWithZoneId(temporalAccessor, UTC) } override def parse(s: String): Int = instantToDays(toInstant(s)) override def format(days: Int): String = { val instant = Instant.ofEpochSecond(days * DateTimeUtils.SECONDS_PER_DAY) formatter.withZone(UTC).format(instant) } } object DateFormatter { val defaultPattern: String = "yyyy-MM-dd" val defaultLocale: Locale = Locale.US def apply(format: String, locale: Locale): DateFormatter = { new Iso8601DateFormatter(format, locale) } def apply(format: String): DateFormatter = apply(format, defaultLocale) def apply(): DateFormatter = apply(defaultPattern) }
Example 47
Source File: SlowConsumerDropsElementsOnFastProducer.scala From akka_streams_tutorial with MIT License | 5 votes |
package sample.stream import java.time.{Instant, ZoneId, ZonedDateTime} import akka.NotUsed import akka.actor.ActorSystem import akka.stream.scaladsl._ import akka.stream.{DelayOverflowStrategy, ThrottleMode} import scala.concurrent.duration._ import scala.util.Failure case class SourceEvent(id: Integer) case class DomainEvent(id: Integer, timeDate: ZonedDateTime) object SlowConsumerDropsElementsOnFastProducer extends App { implicit val system = ActorSystem("SlowConsumerDropsElementsOnFastProducer") implicit val ec = system.dispatcher val fastSource: Source[SourceEvent, NotUsed] = Source(1 to 500) .throttle(10, 1.second, 1, ThrottleMode.shaping) .map { i => println(s"Producing event: $i") SourceEvent(i) } val droppyStream: Flow[SourceEvent, SourceEvent, NotUsed] = //Conflate is "rate aware", it combines/aggregates elements from upstream while downstream backpressures //The reducer function here takes the freshest element. This in a simple dropping operation. Flow[SourceEvent] .conflate((lastEvent, newEvent) => newEvent) val enrichWithTimestamp: Flow[SourceEvent, DomainEvent, NotUsed] = Flow[SourceEvent] .map { e => val instant = Instant.ofEpochMilli(System.currentTimeMillis()) val zonedDateTimeUTC: ZonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC")) DomainEvent(e.id, zonedDateTimeUTC) } val terminationHook: Flow[DomainEvent, DomainEvent, Unit] = Flow[DomainEvent] .watchTermination() { (_, done) => done.onComplete { case Failure(err) => println(s"Flow failed: $err") case _ => system.terminate(); println(s"Flow terminated") } } val slowSink: Sink[DomainEvent, NotUsed] = Flow[DomainEvent] //.buffer(100, OverflowStrategy.backpressure) .delay(10.seconds, DelayOverflowStrategy.backpressure) .to(Sink.foreach(e => println(s"Reached Sink: $e"))) fastSource .via(droppyStream) .via(enrichWithTimestamp) .via(terminationHook) .runWith(slowSink) }
Example 48
Source File: TweetExample.scala From akka_streams_tutorial with MIT License | 5 votes |
package sample.stream import java.time.{Instant, ZoneId} import akka.NotUsed import akka.actor.{ActorSystem, Cancellable} import akka.stream.DelayOverflowStrategy import akka.stream.scaladsl.{Flow, MergePrioritized, Sink, Source} import org.apache.commons.lang3.exception.ExceptionUtils import org.slf4j.{Logger, LoggerFactory} import scala.concurrent.duration._ import scala.util.{Failure, Success} object TweetExample extends App { implicit val system = ActorSystem("TweetExample") implicit val ec = system.dispatcher val logger: Logger = LoggerFactory.getLogger(this.getClass) final case class Author(handle: String) final case class Hashtag(name: String) final case class Tweet(author: Author, timestamp: Long, body: String) { def hashtags: Set[Hashtag] = body.split(" ").collect { case t if t.startsWith("#") => Hashtag(t) }.toSet override def toString = { val localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime s"$localDateTime - ${author.handle} tweeted: ${body.take(5)}..." } } val akkaTag = Hashtag("#akka") val tweetsLowPrio: Source[Tweet, Cancellable] = Source.tick(1.second, 200.millis, NotUsed).map(_ => Tweet(Author("LowPrio"), System.currentTimeMillis, "#other #akka aBody")) val tweetsHighPrio: Source[Tweet, Cancellable] = Source.tick(2.second, 1.second, NotUsed).map(_ => Tweet(Author("HighPrio"), System.currentTimeMillis, "#akka #other aBody")) val tweetsVeryHighPrio: Source[Tweet, Cancellable] = Source.tick(2.second, 1.second, NotUsed).map(_ => Tweet(Author("VeryHighPrio"), System.currentTimeMillis, "#akka #other aBody")) val limitedTweets: Source[Tweet, NotUsed] = Source.combine(tweetsLowPrio, tweetsHighPrio, tweetsVeryHighPrio)(_ => MergePrioritized(List(1, 10, 100))).take(20) val processingFlow = Flow[Tweet] .filter(_.hashtags.contains(akkaTag)) .wireTap(each => logger.info(s"$each")) val slowDownstream = Flow[Tweet] .delay(5.seconds, DelayOverflowStrategy.backpressure) val processedTweets = limitedTweets .via(processingFlow) .via(slowDownstream) .runWith(Sink.seq) processedTweets.onComplete { case Success(results) => logger.info(s"Successfully processed: ${results.size} tweets") system.terminate() case Failure(exception) => logger.info(s"The stream failed with: ${ExceptionUtils.getRootCause(exception)}") system.terminate() } }
Example 49
Source File: ScheduleSpec.scala From hyperion with Apache License 2.0 | 5 votes |
package com.krux.hyperion import java.time.format.DateTimeFormatter import java.time.{DayOfWeek, ZoneId, ZonedDateTime} import org.joda.time.{DateTime, DateTimeZone} import org.scalatest.WordSpec class ScheduleSpec extends WordSpec{ "`with` function of DayOfWeek" should { "shift the date to the expected" in { val dt = ZonedDateTime.parse("2019-11-20T00:00:00Z") assert( dt.`with`(DayOfWeek.of(1)) === ZonedDateTime.parse("2019-11-18T00:00:00Z") && dt.`with`(DayOfWeek.of(2)) === ZonedDateTime.parse("2019-11-19T00:00:00Z") && dt.`with`(DayOfWeek.of(3)) === ZonedDateTime.parse("2019-11-20T00:00:00Z") && dt.`with`(DayOfWeek.of(4)) === ZonedDateTime.parse("2019-11-21T00:00:00Z") && dt.`with`(DayOfWeek.of(5)) === ZonedDateTime.parse("2019-11-22T00:00:00Z") && dt.`with`(DayOfWeek.of(6)) === ZonedDateTime.parse("2019-11-23T00:00:00Z") && dt.`with`(DayOfWeek.of(7)) === ZonedDateTime.parse("2019-11-24T00:00:00Z") ) } } "`with` function of DayOfWeek" should { "shift the date same as withDayOfWeek in Joda time" in { val dateTimeFormatStr = "yyyy-MM-dd'T'HH:mm:ss" val datetimeFormat = DateTimeFormatter.ofPattern(dateTimeFormatStr) val javaDt = ZonedDateTime.parse("2019-11-20T00:00:00Z").withZoneSameInstant(ZoneId.of("UTC")) val jodaDt = new DateTime("2019-11-20T00:00:00Z").withZone(DateTimeZone.UTC) assert( javaDt.`with`(DayOfWeek.of(1)).format(datetimeFormat) === jodaDt.withDayOfWeek(1).toString(dateTimeFormatStr) && javaDt.`with`(DayOfWeek.of(2)).format(datetimeFormat) === jodaDt.withDayOfWeek(2).toString(dateTimeFormatStr) && javaDt.`with`(DayOfWeek.of(3)).format(datetimeFormat) === jodaDt.withDayOfWeek(3).toString(dateTimeFormatStr) && javaDt.`with`(DayOfWeek.of(4)).format(datetimeFormat) === jodaDt.withDayOfWeek(4).toString(dateTimeFormatStr) && javaDt.`with`(DayOfWeek.of(5)).format(datetimeFormat) === jodaDt.withDayOfWeek(5).toString(dateTimeFormatStr) && javaDt.`with`(DayOfWeek.of(6)).format(datetimeFormat) === jodaDt.withDayOfWeek(6).toString(dateTimeFormatStr) && javaDt.`with`(DayOfWeek.of(7)).format(datetimeFormat) === jodaDt.withDayOfWeek(7).toString(dateTimeFormatStr) ) } } "withZoneSameLocal" should { "be consistent with toDateTime in joda time" in { val dateTimeFormatStr = "yyyy-MM-dd'T'HH:mm:ss" val datetimeFormat = DateTimeFormatter.ofPattern( dateTimeFormatStr) val javaDt = ZonedDateTime.parse("2019-11-18T00:00:00Z").withZoneSameLocal(ZoneId.of("UTC")) val jodaDt = new DateTime("2019-11-18T00:00:00Z").toDateTime(DateTimeZone.UTC) assert(javaDt.format(datetimeFormat) === jodaDt.toString(dateTimeFormatStr)) } } }
Example 50
Source File: constants.scala From Converter with GNU General Public License v3.0 | 5 votes |
package org.scalablytyped.converter.internal import java.net.URI import java.time.ZoneId import java.time.format.DateTimeFormatter import java.util.Locale import org.scalablytyped.converter.internal.environment.OpSystem import scala.io.Codec object constants { val defaultCacheFolder: os.Path = environment.OS match { case OpSystem.MAC => os.home / "Library" / "Caches" / "ScalablyTyped" case OpSystem.WINDOWS => os.home / "AppData" / "Local" / "ScalablyTyped" case OpSystem.LINUX => os.home / ".cache" / "scalablytyped" case OpSystem.UNKNOWN => os.home / ".cache" / "scalablytyped" // By default, Linux cache folder } val defaultLocalPublishFolder: os.Path = os.home / ".ivy2" / "local" val DefinitelyTypedRepo = new URI("https://github.com/DefinitelyTyped/DefinitelyTyped.git") val ConverterRepo = new URI("https://github.com/ScalablyTyped/Converter.git") val isCi = sys.env.get("CIRCLECI").isDefined val TimeZone = ZoneId.of("UTC") val Utf8 = Codec.UTF8.charSet val DateTimePattern = DateTimeFormatter ofPattern "yyyyMMddhhmm" withLocale Locale.ENGLISH withZone TimeZone }
Example 51
Source File: InteroperabilityWithJava.scala From LearningScala with Apache License 2.0 | 5 votes |
package _990_random import java.time.ZoneId import scala.collection.JavaConverters._ object InteroperabilityWithJava extends App { val javaToScalaList: List[String] = ZoneId.getAvailableZoneIds.asScala.toList val javaToScalaSet: Set[String] = ZoneId.getAvailableZoneIds.asScala.toSet val scalaToJavaList: java.util.List[Int] = List(1, 2, 3).asJava val scalaToJavaCollectionList: java.util.Collection[Int] = List(1, 2, 3).asJavaCollection val scalaToJavaSet: java.util.Set[Int] = Set(1, 2, 3, 4).asJava val scalaToJavaCollectionSet: java.util.Collection[Int] = Set(1, 2, 3, 4).asJavaCollection def printTypeAndContent(value: Any): Unit = println(value.getClass.getSimpleName + " ==> " + value) printTypeAndContent(javaToScalaList) printTypeAndContent(javaToScalaSet) printTypeAndContent(scalaToJavaList) printTypeAndContent(scalaToJavaCollectionList) printTypeAndContent(scalaToJavaSet) printTypeAndContent(scalaToJavaCollectionSet) }
Example 52
Source File: Status.scala From daf-semantics with Apache License 2.0 | 5 votes |
package it.almawave.kb.http.endpoints import java.time.LocalTime import io.swagger.annotations.Api import javax.ws.rs.Path import javax.ws.rs.GET import javax.ws.rs.Produces import io.swagger.annotations.ApiOperation import javax.ws.rs.core.MediaType import org.slf4j.LoggerFactory import javax.ws.rs.core.Context import javax.ws.rs.core.UriInfo import javax.ws.rs.core.Request import it.almawave.linkeddata.kb.utils.JSONHelper import java.time.LocalDateTime import java.time.ZonedDateTime import java.time.format.DateTimeFormatter import java.util.Locale import java.time.ZoneId @Api(tags = Array("catalog")) @Path("/status") class Status { private val logger = LoggerFactory.getLogger(this.getClass) @Context var uriInfo: UriInfo = null @GET @Produces(Array(MediaType.APPLICATION_JSON)) @ApiOperation(nickname = "status", value = "endpoint status") def status() = { val base_uri = uriInfo.getBaseUri val msg = s"the service is running at ${base_uri}" logger.info(msg) val _now = now() StatusMsg(_now._1, _now._2, msg) } def now() = { val zdt = ZonedDateTime.now(ZoneId.of("+1")) val dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ") (zdt.format(dtf), zdt) } } case class StatusMsg( now: String, dateTime: ZonedDateTime, msg: String )
Example 53
Source File: InformativeTestStart.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.it.test import java.time.{LocalDateTime, ZoneId} import com.wavesplatform.dex.it.api.BaseContainersKit import mouse.any._ import org.scalatest.{Args, Status, Suite} import scala.util.{Failure, Success} trait InformativeTestStart extends Suite { self: BaseContainersKit => override protected def runTest(testName: String, args: Args): Status = { def print(text: String): Unit = writeGlobalLog(s"---------- [${LocalDateTime.now(ZoneId.of("UTC"))}] $text ----------") print(s"Test '$testName' started") super.runTest(testName, args) unsafeTap { _.whenCompleted { case Success(r) => print(s"Test '$testName' ${if (r) "succeeded" else "failed"}") case Failure(e) => print(s"Test '$testName' failed with exception '${e.getClass.getSimpleName}'") } } } protected def writeGlobalLog(x: String): Unit = { log.debug(x) knownContainers.get().foreach { _.printDebugMessage(x) } } }
Example 54
Source File: LogicalTypeProxy.scala From embulk-output-s3_parquet with MIT License | 5 votes |
package org.embulk.output.s3_parquet.parquet import java.time.ZoneId import java.util.Locale import org.apache.parquet.io.api.RecordConsumer import org.apache.parquet.schema.LogicalTypeAnnotation.TimeUnit import org.apache.parquet.schema.LogicalTypeAnnotation.TimeUnit.MILLIS import org.apache.parquet.schema.PrimitiveType import org.embulk.config.ConfigException import org.embulk.output.s3_parquet.catalog.GlueDataType import org.embulk.spi.Column import org.embulk.spi.time.{Timestamp, TimestampFormatter} import org.msgpack.value.Value object LogicalTypeProxy { private val DEFAULT_SCALE: Int = 0 private val DEFAULT_BID_WIDTH: Int = 64 private val DEFAULT_IS_SIGNED: Boolean = true private val DEFAULT_IS_ADJUSTED_TO_UTC: Boolean = true private val DEFAULT_TIME_UNIT: TimeUnit = MILLIS private val DEFAULT_TIME_ZONE: ZoneId = ZoneId.of("UTC") } case class LogicalTypeProxy( name: String, scale: Option[Int] = None, precision: Option[Int] = None, bitWidth: Option[Int] = None, isSigned: Option[Boolean] = None, isAdjustedToUtc: Option[Boolean] = None, timeUnit: Option[TimeUnit] = None, timeZone: Option[ZoneId] = None ) extends ParquetColumnType { private def getScale: Int = scale.getOrElse(LogicalTypeProxy.DEFAULT_SCALE) private def getPrecision: Int = precision.getOrElse { throw new ConfigException("\"precision\" must be set.") } private def getBidWith: Int = bitWidth.getOrElse(LogicalTypeProxy.DEFAULT_BID_WIDTH) private def getIsSigned: Boolean = isSigned.getOrElse(LogicalTypeProxy.DEFAULT_IS_SIGNED) private def getIsAdjustedToUtc: Boolean = isAdjustedToUtc.getOrElse(LogicalTypeProxy.DEFAULT_IS_ADJUSTED_TO_UTC) private def getTimeUnit: TimeUnit = timeUnit.getOrElse(LogicalTypeProxy.DEFAULT_TIME_UNIT) private def getTimeZone: ZoneId = timeZone.getOrElse(LogicalTypeProxy.DEFAULT_TIME_ZONE) lazy val logicalType: ParquetColumnType = { name.toUpperCase(Locale.ENGLISH) match { case "INT" => IntLogicalType(getBidWith, getIsSigned) case "TIMESTAMP" => TimestampLogicalType(getIsAdjustedToUtc, getTimeUnit, getTimeZone) case "TIME" => TimeLogicalType(getIsAdjustedToUtc, getTimeUnit, getTimeZone) case "DECIMAL" => DecimalLogicalType(getScale, getPrecision) case "DATE" => DateLogicalType case "JSON" => JsonLogicalType case _ => throw new ConfigException(s"Unsupported logical_type.name: $name.") } } override def primitiveType(column: Column): PrimitiveType = logicalType.primitiveType(column) override def glueDataType(column: Column): GlueDataType = logicalType.glueDataType(column) override def consumeBoolean(consumer: RecordConsumer, v: Boolean): Unit = logicalType.consumeBoolean(consumer, v) override def consumeString(consumer: RecordConsumer, v: String): Unit = logicalType.consumeString(consumer, v) override def consumeLong(consumer: RecordConsumer, v: Long): Unit = logicalType.consumeLong(consumer, v) override def consumeDouble(consumer: RecordConsumer, v: Double): Unit = logicalType.consumeDouble(consumer, v) override def consumeTimestamp( consumer: RecordConsumer, v: Timestamp, formatter: TimestampFormatter ): Unit = logicalType.consumeTimestamp(consumer, v, formatter) override def consumeJson(consumer: RecordConsumer, v: Value): Unit = logicalType.consumeJson(consumer, v) }
Example 55
Source File: TimestampLogicalType.scala From embulk-output-s3_parquet with MIT License | 5 votes |
package org.embulk.output.s3_parquet.parquet import java.time.ZoneId import org.apache.parquet.io.api.RecordConsumer import org.apache.parquet.schema.{LogicalTypeAnnotation, PrimitiveType, Types} import org.apache.parquet.schema.LogicalTypeAnnotation.TimeUnit import org.apache.parquet.schema.LogicalTypeAnnotation.TimeUnit.{ MICROS, MILLIS, NANOS } import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName import org.embulk.config.ConfigException import org.embulk.output.s3_parquet.catalog.GlueDataType import org.embulk.spi.`type`.{ BooleanType, DoubleType, JsonType, LongType, StringType, TimestampType } import org.embulk.spi.time.{Timestamp, TimestampFormatter} import org.embulk.spi.Column import org.msgpack.value.Value import org.slf4j.{Logger, LoggerFactory} case class TimestampLogicalType( isAdjustedToUtc: Boolean, timeUnit: TimeUnit, timeZone: ZoneId ) extends ParquetColumnType { private val logger: Logger = LoggerFactory.getLogger(classOf[TimestampLogicalType]) override def primitiveType(column: Column): PrimitiveType = column.getType match { case _: LongType | _: TimestampType => Types .optional(PrimitiveTypeName.INT64) .as(LogicalTypeAnnotation.timestampType(isAdjustedToUtc, timeUnit)) .named(column.getName) case _: BooleanType | _: DoubleType | _: StringType | _: JsonType | _ => throw new ConfigException(s"Unsupported column type: ${column.getName}") } override def glueDataType(column: Column): GlueDataType = column.getType match { case _: LongType | _: TimestampType => timeUnit match { case MILLIS => GlueDataType.TIMESTAMP case MICROS | NANOS => warningWhenConvertingTimestampToGlueType(GlueDataType.BIGINT) GlueDataType.BIGINT } case _: BooleanType | _: DoubleType | _: StringType | _: JsonType | _ => throw new ConfigException(s"Unsupported column type: ${column.getName}") } override def consumeBoolean(consumer: RecordConsumer, v: Boolean): Unit = throw newUnsupportedMethodException("consumeBoolean") override def consumeString(consumer: RecordConsumer, v: String): Unit = throw newUnsupportedMethodException("consumeString") override def consumeLong(consumer: RecordConsumer, v: Long): Unit = consumer.addLong(v) override def consumeDouble(consumer: RecordConsumer, v: Double): Unit = throw newUnsupportedMethodException("consumeDouble") override def consumeTimestamp( consumer: RecordConsumer, v: Timestamp, formatter: TimestampFormatter ): Unit = timeUnit match { case MILLIS => consumer.addLong(v.toEpochMilli) case MICROS => consumer.addLong(v.getEpochSecond * 1_000_000L + (v.getNano / 1_000L)) case NANOS => consumer.addLong(v.getEpochSecond * 1_000_000_000L + v.getNano) } override def consumeJson(consumer: RecordConsumer, v: Value): Unit = throw newUnsupportedMethodException("consumeJson") private def warningWhenConvertingTimestampToGlueType( glueType: GlueDataType ): Unit = logger.warn( s"timestamp(isAdjustedToUtc = $isAdjustedToUtc, timeUnit = $timeUnit) is converted" + s" to Glue ${glueType.name} but this is not represented correctly, because Glue" + s" does not support time type. Please use `catalog.column_options` to define the type." ) }
Example 56
Source File: ClockProviderSpec.scala From chronoscala with MIT License | 5 votes |
package jp.ne.opt.chronoscala import java.time.{Clock, ZoneId} import jp.ne.opt.chronoscala.Imports._ import org.scalatest.BeforeAndAfter import org.scalatest.flatspec.AnyFlatSpec class ClockProviderSpec extends AnyFlatSpec with BeforeAndAfter { after { ClockProvider.setCurrentClockSystem() } it should "set current clock" in { ClockProvider.setCurrentClock(Clock.fixed(Instant.ofEpochMilli(0L), ZoneId.of("UTC"))) assert(Instant.now() == Instant.ofEpochMilli(0L)) assert(LocalDate.now() == LocalDate.parse("1970-01-01")) assert(LocalDateTime.now() == LocalDateTime.parse("1970-01-01T00:00:00.000")) assert(LocalTime.now() == LocalTime.parse("00:00:00.000")) assert(ZonedDateTime.now() == ZonedDateTime.parse("1970-01-01T00:00:00.000+00:00[UTC]")) assert(OffsetDateTime.now() == OffsetDateTime.parse("1970-01-01T00:00:00.000+00:00")) } }
Example 57
Source File: QuoteParser.scala From YahooFinanceScala with MIT License | 5 votes |
package openquant.yahoofinance.impl import java.time.format.DateTimeFormatter import java.time.{LocalDate, ZoneId, ZonedDateTime} import com.github.tototoshi.csv._ import openquant.yahoofinance.Quote import scala.io.Source class QuoteParser { private[this] val df = DateTimeFormatter.ofPattern("yyyy-MM-dd") private[this] val zoneId = ZoneId.of("America/New_York") def parse(content: String): Vector[Quote] = { val csvReader = CSVReader.open(Source.fromString(content)) val quotes: Vector[Quote] = csvReader.toStream.drop(1).map { fields ⇒ parseCSVLine(fields.toVector) }.toVector quotes } private def parseCSVLine(field: Vector[String]): Quote = { require(field.length >= 7) Quote( parseDate(field(0)), BigDecimal(field(1)), BigDecimal(field(4)), BigDecimal(field(2)), BigDecimal(field(3)), BigDecimal(field(5)), BigDecimal(field(6)) ) } private def parseDate(date: String): ZonedDateTime = { LocalDate.parse(date, df).atStartOfDay().atZone(zoneId) } } object QuoteParser { def apply() = new QuoteParser }
Example 58
Source File: ArrayOfZonedDateTimesBenchmark.scala From jsoniter-scala with MIT License | 5 votes |
package com.github.plokhotnyuk.jsoniter_scala.benchmark import java.nio.charset.StandardCharsets.UTF_8 import java.time.{ZoneId, _} import org.openjdk.jmh.annotations.{Param, Setup} import scala.jdk.CollectionConverters._ abstract class ArrayOfZonedDateTimesBenchmark extends CommonParams { val zoneIds: Array[ZoneId] = (ZoneId.getAvailableZoneIds.asScala.take(100).map(ZoneId.of) ++ (1 to 7).map(i => ZoneId.of(s"+0$i:00")) ++ (1 to 7).map(i => ZoneId.of(s"UT+0$i:00")) ++ (1 to 7).map(i => ZoneId.of(s"UTC+0$i:00")) ++ (1 to 7).map(i => ZoneId.of(s"GMT+0$i:00"))).toArray @Param(Array("1", "10", "100", "1000", "10000", "100000", "1000000")) var size: Int = 1000 var obj: Array[ZonedDateTime] = _ var jsonString: String = _ var jsonBytes: Array[Byte] = _ var preallocatedBuf: Array[Byte] = _ @Setup def setup(): Unit = { obj = (1 to size).map { i => val n = Math.abs(i * 1498724053) ZonedDateTime.of(LocalDateTime.of(LocalDate.ofEpochDay(i), LocalTime.ofNanoOfDay(((n % 86000) | 0x1) * 1000000000L + (i % 4 match { case 0 => 0 case 1 => ((n % 1000) | 0x1) * 1000000 case 2 => ((n % 1000000) | 0x1) * 1000 case 3 => (n | 0x1) % 1000000000 }))), zoneIds(i % zoneIds.length)) }.toArray jsonString = obj.mkString("[\"", "\",\"", "\"]") jsonBytes = jsonString.getBytes(UTF_8) preallocatedBuf = new Array[Byte](jsonBytes.length + 100) } }
Example 59
Source File: Util.scala From devbox with Apache License 2.0 | 5 votes |
package devbox.common import java.time.ZoneId import java.time.format.{DateTimeFormatter, FormatStyle} object Util { val blockSize = 4 * 1024 * 1024 def gzip(bytes: Array[Byte]): Array[Byte] = { val boas = new java.io.ByteArrayOutputStream val gzipped = new java.util.zip.GZIPOutputStream(boas) gzipped.write(bytes) gzipped.close() boas.toByteArray } def gunzip(bytes: Array[Byte]): Array[Byte] = { val bais = new java.io.ByteArrayInputStream(bytes) val gunzipped = new java.util.zip.GZIPInputStream(bais) val baos = new java.io.ByteArrayOutputStream os.Internals.transfer(gunzipped, baos) baos.toByteArray } implicit val permsetRw: upickle.default.ReadWriter[os.PermSet] = upickle.default.readwriter[String].bimap[os.PermSet]( _.toString(), os.PermSet.fromString ) implicit val relpathRw: upickle.default.ReadWriter[os.RelPath] = upickle.default.readwriter[String].bimap[os.RelPath]( _.toString(), os.RelPath(_) ) implicit val subpathRw: upickle.default.ReadWriter[os.SubPath] = upickle.default.readwriter[String].bimap[os.SubPath]( _.toString(), os.SubPath(_) ) def autoclose[T <: AutoCloseable, V](x: T)(f: T => V) = { try f(x) finally x.close() } val timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) .withZone(ZoneId.systemDefault()) def formatInt(number: Long) = { val numberFormat = java.text.NumberFormat.getNumberInstance(java.util.Locale.US) numberFormat.format(number) } val bytesFormatter = new java.text.DecimalFormat("#,##0.#") def readableBytesSize(size: Long): String = { if (size <= 0) return "0" val units = Array[String]("B", "kB", "MB", "GB", "TB") val digitGroups = (Math.log10(size) / Math.log10(1024)).toInt bytesFormatter.format(size / Math.pow(1024, digitGroups)) + " " + units(digitGroups) } def joinMaps[K, V](left: Map[K, Set[V]], right: Map[K, Set[V]]) = { (left.keySet ++ right.keySet) .map{k => (k, left.getOrElse(k, Set()) ++ right.getOrElse(k, Set()))} .toMap } def joinMaps2[K, Z](left: Map[K, PathMap[Z]], right: Map[K, PathMap[Z]]) = { (left.keySet ++ right.keySet) .map{k => ( k, left.getOrElse( k, new PathMap[Z]()).withPaths(right.getOrElse(k, new PathMap[Z]()).walkValues() ) ) } .toMap } def joinMaps3[K](left: Map[K, PathSet], right: Map[K, PathSet]) = { (left.keySet ++ right.keySet) .map{k => ( k, left.getOrElse(k, new PathSet()).withPaths(right.getOrElse(k, new PathSet()).walk())) } .toMap } def sentryCapture(e: Throwable): Unit = { io.sentry.Sentry.getContext().addTag("whoami", System.getProperty("user.name")) io.sentry.Sentry.capture(e) } }
Example 60
Source File: ISO8601DateTime.scala From toketi-iothubreact with MIT License | 5 votes |
// Copyright (c) Microsoft. All rights reserved. package B_PrintTemperature import java.time.format.DateTimeFormatter import java.time.{ZoneId, ZonedDateTime} import scala.util.matching.Regex case class ISO8601DateTime(text: String) { private lazy val pattern1: Regex = """(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2}).(\d{1,3})Z""".r private lazy val pattern2: Regex = """(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})Z""".r private lazy val pattern3: Regex = """(\d{4})-(\d{1,2})-(\d{1,2})""".r private lazy val format : DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") private lazy val zone : ZoneId = ZoneId.of("UTC") private lazy val zonedDateTime: ZonedDateTime = { text match { case pattern1(y, m, d, h, i, s, n) ⇒ val ni = n.toInt val nanos = if (ni > 99) ni * 1000000 else if (ni > 9) ni * 10000000 else ni * 100000000 ZonedDateTime.of(y.toInt, m.toInt, d.toInt, h.toInt, i.toInt, s.toInt, nanos, zone) case pattern2(y, m, d, h, i, s) ⇒ ZonedDateTime.of(y.toInt, m.toInt, d.toInt, h.toInt, i.toInt, s.toInt, 0, zone) case pattern3(y, m, d) ⇒ ZonedDateTime.of(y.toInt, m.toInt, d.toInt, 0, 0, 0, 0, zone) case null ⇒ null case _ ⇒ throw new Exception(s"wrong date time format: $text") } } override def toString: String = if (zonedDateTime == null) "" else zonedDateTime.format(format) }
Example 61
Source File: ISO8601DateTime.scala From toketi-iothubreact with MIT License | 5 votes |
// Copyright (c) Microsoft. All rights reserved. package A_APIUSage import java.time.format.DateTimeFormatter import java.time.{ZoneId, ZonedDateTime} import scala.util.matching.Regex case class ISO8601DateTime(text: String) { private lazy val pattern1: Regex = """(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2}).(\d{1,3})Z""".r private lazy val pattern2: Regex = """(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})Z""".r private lazy val pattern3: Regex = """(\d{4})-(\d{1,2})-(\d{1,2})""".r private lazy val format : DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") private lazy val zone : ZoneId = ZoneId.of("UTC") private lazy val zonedDateTime: ZonedDateTime = { text match { case pattern1(y, m, d, h, i, s, n) ⇒ val ni = n.toInt val nanos = if (ni > 99) ni * 1000000 else if (ni > 9) ni * 10000000 else ni * 100000000 ZonedDateTime.of(y.toInt, m.toInt, d.toInt, h.toInt, i.toInt, s.toInt, nanos, zone) case pattern2(y, m, d, h, i, s) ⇒ ZonedDateTime.of(y.toInt, m.toInt, d.toInt, h.toInt, i.toInt, s.toInt, 0, zone) case pattern3(y, m, d) ⇒ ZonedDateTime.of(y.toInt, m.toInt, d.toInt, 0, 0, 0, 0, zone) case null ⇒ null case _ ⇒ throw new Exception(s"wrong date time format: $text") } } override def toString: String = if (zonedDateTime == null) "" else zonedDateTime.format(format) }
Example 62
Source File: ResourceFSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import java.time.{Clock, Instant, ZoneId} import ch.epfl.bluebrain.nexus.util.{EitherValues, Resources} import ch.epfl.bluebrain.nexus.iam.testsyntax._ import ch.epfl.bluebrain.nexus.iam.types.Identity.User import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.service.config.ServiceConfig.HttpConfig import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv import io.circe.Printer import io.circe.syntax._ import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike //noinspection TypeAnnotation class ResourceFSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with Resources { "A ResourceMetadata" should { val user = User("mysubject", "myrealm") val user2 = User("mysubject2", "myrealm") implicit val http = HttpConfig("some", 8080, "v1", "http://nexus.example.com") val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) val instant = clock.instant() val id = url"http://example.com/id" val printer = Printer.spaces2.copy(dropNullValues = true) "be converted to Json correctly" when { "using multiple types" in { val json = jsonContentOf("/resources/write-response.json") val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList.value, nxv.Realm.value), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } "using a single type" in { val json = jsonContentOf("/resources/write-response-singletype.json") val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList.value), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } "using no types" in { val json = jsonContentOf("/resources/write-response-notypes.json") val model = ResourceMetadata(id, 1L, Set(), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } } } }
Example 63
Source File: TaggingAdapterSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.persistence import java.time.{Clock, Instant, ZoneId} import akka.persistence.journal.Tagged import cats.syntax.show._ import ch.epfl.bluebrain.nexus.iam.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.config.Schemas._ import ch.epfl.bluebrain.nexus.kg.persistence.TaggingAdapterSpec.Other import ch.epfl.bluebrain.nexus.kg.resources.Event._ import ch.epfl.bluebrain.nexus.kg.resources.{Id, OrganizationRef, Ref} import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv import io.circe.Json import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class TaggingAdapterSpec extends AnyWordSpecLike with Matchers with Inspectors with TestHelper { "A TaggingAdapter" should { val clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) def genJson(): Json = Json.obj("key" -> Json.fromString(genString())) val adapter = new TaggingAdapter() val orgRef = OrganizationRef(genUUID) val id = Id(ProjectRef(genUUID), nxv.projects.value) val mapping = Map( Set( s"type=${nxv.Schema.value.show}", s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event" ) -> Created( id, orgRef, Ref(shaclSchemaUri), Set(nxv.Schema.value, nxv.Resource.value), genJson(), clock.instant(), Anonymous ), Set( s"type=${nxv.Resolver.value.show}", s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event" ) -> Updated(id, orgRef, 1L, Set(nxv.Resource.value, nxv.Resolver.value), genJson(), clock.instant(), Anonymous), Set(s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event") -> Deprecated(id, orgRef, 1L, Set(nxv.Resource.value), clock.instant(), Anonymous), Set(s"project=${id.parent.id}", s"org=${orgRef.show}", "event") -> TagAdded(id, orgRef, 2L, 1L, "tag", clock.instant(), Anonymous) ) "set the appropriate tags" in { forAll(mapping.toList) { case (tags, ev) => adapter.toJournal(ev) shouldEqual Tagged(ev, tags) } } "return an empty manifest" in { adapter.manifest(Other(genString())) shouldEqual "" } } } object TaggingAdapterSpec { final private[persistence] case class Other(value: String) }
Example 64
Source File: SparqlLinkSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.indexing import java.time.{Clock, Instant, ZoneId} import ch.epfl.bluebrain.nexus.commons.sparql.client.SparqlResults.Binding import ch.epfl.bluebrain.nexus.kg.config.Schemas._ import ch.epfl.bluebrain.nexus.kg.indexing.SparqlLink.{SparqlExternalLink, SparqlResourceLink} import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.Vocabulary._ import ch.epfl.bluebrain.nexus.rdf.implicits._ import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv class SparqlLinkSpec extends AnyWordSpecLike with Matchers with OptionValues { "A SparqlLink" should { val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) val id = url"http://example.com/id" val property = url"http://example.com/friend" val property2 = url"http://example.com/friend2" val paths = List(property, property2) "build SparqlExternalLink from SPARQL response" in { val bindings = Map( "s" -> Binding("uri", id.asString), "paths" -> Binding("literal", s"${property.asString} ${property2.asString}") ) SparqlExternalLink(bindings).value shouldEqual SparqlExternalLink(id, paths) } "build SparqlResourceLink from SPARQL response" in { val self = url"http://127.0.0.1:8080/v1/resources/myorg/myproject/_/id" val project = url"http://127.0.0.1:8080/v1/projects/myorg/myproject/" val author = url"http://127.0.0.1:8080/v1/realms/myrealm/users/me" val bindings = Map( "s" -> Binding("uri", id.asString), "paths" -> Binding("literal", s"${property.asString} ${property2.asString}"), "_rev" -> Binding("literal", "1", datatype = Some(xsd.long.asString)), "_self" -> Binding("uri", self.asString), "_project" -> Binding("uri", project.asString), "types" -> Binding("literal", s"${nxv.Resolver.value.asString} ${nxv.Schema.value.asString}"), "_constrainedBy" -> Binding("uri", unconstrainedSchemaUri.asString), "_createdBy" -> Binding("uri", author.asString), "_updatedBy" -> Binding("uri", author.asString), "_createdAy" -> Binding("uri", author.asString), "_createdAt" -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)), "_updatedAt" -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)), "_deprecated" -> Binding("literal", "false", datatype = Some(xsd.boolean.asString)) ) SparqlResourceLink(bindings).value shouldEqual SparqlResourceLink( id, project, self, 1L, Set[AbsoluteIri](nxv.Schema.value, nxv.Resolver.value), false, clock.instant(), clock.instant(), author, author, unconstrainedRef, paths ) } } }
Example 65
Source File: ArchiveCacheSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.archives import java.time.{Clock, Instant, ZoneId} import cats.effect.{IO, Timer} import ch.epfl.bluebrain.nexus.admin.client.types.Project import ch.epfl.bluebrain.nexus.commons.test.ActorSystemFixture import ch.epfl.bluebrain.nexus.commons.test.io.IOOptionValues import ch.epfl.bluebrain.nexus.iam.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.archives.Archive.{File, Resource, ResourceDescription} import ch.epfl.bluebrain.nexus.kg.resources.Id import ch.epfl.bluebrain.nexus.kg.resources.syntax._ import ch.epfl.bluebrain.nexus.service.config.Settings import org.scalatest.concurrent.Eventually import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class ArchiveCacheSpec extends ActorSystemFixture("ArchiveCacheSpec", true) with TestHelper with AnyWordSpecLike with Matchers with IOOptionValues with Eventually { implicit override def patienceConfig: PatienceConfig = PatienceConfig(10.second, 50.milliseconds) private val appConfig = Settings(system).serviceConfig implicit private val config = appConfig.copy(kg = appConfig.kg.copy(archives = appConfig.kg.archives.copy(cacheInvalidateAfter = 500.millis, maxResources = 100)) ) implicit private val timer: Timer[IO] = IO.timer(system.dispatcher) implicit private val archivesCfg = config.kg.archives private val cache: ArchiveCache[IO] = ArchiveCache[IO].unsafeToFuture().futureValue implicit private val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault()) private val instant = clock.instant() def randomProject() = { val instant = Instant.EPOCH // format: off Project(genIri, genString(), genString(), None, genIri, genIri, Map.empty, genUUID, genUUID, 1L, false, instant, genIri, instant, genIri) // format: on } "An archive cache" should { "write and read an Archive" in { val resId = Id(randomProject().ref, genIri) val resource1 = Resource(genIri, randomProject(), None, None, originalSource = true, None) val file1 = File(genIri, randomProject(), None, None, None) val archive = Archive(resId, instant, Anonymous, Set(resource1, file1)) val _ = cache.put(archive).value.some cache.get(archive.resId).value.some shouldEqual archive } "read a non existing resource" in { val resId = Id(randomProject().ref, genIri) cache.get(resId).value.ioValue shouldEqual None } "read after timeout" in { val resId = Id(randomProject().ref, genIri) val set = Set[ResourceDescription](Resource(genIri, randomProject(), None, None, originalSource = true, None)) val archive = Archive(resId, instant, Anonymous, set) val _ = cache.put(archive).value.some val time = System.currentTimeMillis() cache.get(resId).value.some shouldEqual archive eventually { cache.get(resId).value.ioValue shouldEqual None } val diff = System.currentTimeMillis() - time diff should be > config.kg.archives.cacheInvalidateAfter.toMillis diff should be < config.kg.archives.cacheInvalidateAfter.toMillis + 300 } } }
Example 66
Source File: TarFlowSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.archives import java.nio.file.Files import java.time.{Clock, Instant, ZoneId} import akka.actor.ActorSystem import akka.stream.scaladsl.FileIO import akka.testkit.TestKit import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.storage.digestSink import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class TarFlowSpec extends TestKit(ActorSystem("TarFlowSpec")) with AnyWordSpecLike with Matchers with TestHelper with ScalaFutures { implicit private val ec = system.dispatcher implicit private val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault()) implicit override def patienceConfig: PatienceConfig = PatienceConfig(55.second, 150.milliseconds) "A TarFlow" should { "tar a bunch of sources" in { val digest = "3fef41c5afe7a7ee11ee9d556a564fb57784cc5247b24c6ca70783f396fa158a1c7952504d3e1aa441de20cf065d740eec454c6ffb7fbc4b6351b950ee51c886" val elems = 500 val contents = List.tabulate(2) { i => val content = (i until (i + elems)).toList.mkString(",") + "\n" ArchiveSource(content.length.toLong, s"some/path$i/$i.txt", produce(content)) } val path = Files.createTempFile("test", ".tar") TarFlow.write(contents).runWith(FileIO.toPath(path)).futureValue FileIO.fromPath(path).runWith(digestSink("SHA-512")).futureValue.value shouldEqual digest Files.delete(path) } } }
Example 67
Source File: DateFormatter.scala From delta with Apache License 2.0 | 5 votes |
sealed trait DateFormatter extends Serializable { def parse(s: String): Int // returns days since epoch def format(days: Int): String } class Iso8601DateFormatter( pattern: String, locale: Locale) extends DateFormatter with DateTimeFormatterHelper { @transient private lazy val formatter = getOrCreateFormatter(pattern, locale) private val UTC = ZoneId.of("UTC") private def toInstant(s: String): Instant = { val temporalAccessor = formatter.parse(s) toInstantWithZoneId(temporalAccessor, UTC) } override def parse(s: String): Int = instantToDays(toInstant(s)) override def format(days: Int): String = { val instant = Instant.ofEpochSecond(days * DateTimeUtils.SECONDS_PER_DAY) formatter.withZone(UTC).format(instant) } } object DateFormatter { val defaultPattern: String = "yyyy-MM-dd" val defaultLocale: Locale = Locale.US def apply(format: String, locale: Locale): DateFormatter = { new Iso8601DateFormatter(format, locale) } def apply(format: String): DateFormatter = apply(format, defaultLocale) def apply(): DateFormatter = apply(defaultPattern) }
Example 68
Source File: ArrayOfZoneIdsReading.scala From jsoniter-scala with MIT License | 5 votes |
package com.github.plokhotnyuk.jsoniter_scala.benchmark import java.nio.charset.StandardCharsets.UTF_8 import java.time.ZoneId import com.avsystem.commons.serialization.json._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.AVSystemCodecs._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.BorerJsonEncodersDecoders._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.JacksonSerDesers._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.JsoniterScalaCodecs._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.SprayFormats._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.UPickleReaderWriters._ import com.github.plokhotnyuk.jsoniter_scala.core._ import io.circe.parser._ import org.openjdk.jmh.annotations.Benchmark import play.api.libs.json.Json import spray.json._ class ArrayOfZoneIdsReading extends ArrayOfZoneIdsBenchmark { @Benchmark def avSystemGenCodec(): Array[ZoneId] = JsonStringInput.read[Array[ZoneId]](new String(jsonBytes, UTF_8)) @Benchmark def borer(): Array[ZoneId] = io.bullet.borer.Json.decode(jsonBytes).to[Array[ZoneId]].value @Benchmark def circe(): Array[ZoneId] = decode[Array[ZoneId]](new String(jsonBytes, UTF_8)).fold(throw _, identity) @Benchmark def jacksonScala(): Array[ZoneId] = jacksonMapper.readValue[Array[ZoneId]](jsonBytes) @Benchmark def jsoniterScala(): Array[ZoneId] = readFromArray[Array[ZoneId]](jsonBytes) @Benchmark def playJson(): Array[ZoneId] = Json.parse(jsonBytes).as[Array[ZoneId]] @Benchmark def sprayJson(): Array[ZoneId] = JsonParser(jsonBytes).convertTo[Array[ZoneId]] @Benchmark def uPickle(): Array[ZoneId] = read[Array[ZoneId]](jsonBytes) }
Example 69
Source File: DateTimeIndexUtilsSuite.scala From spark-timeseries with Apache License 2.0 | 5 votes |
package com.cloudera.sparkts import java.time.{ZonedDateTime, ZoneId} import com.cloudera.sparkts.DateTimeIndex._ import org.scalatest.{FunSuite, ShouldMatchers} class DateTimeIndexUtilsSuite extends FunSuite with ShouldMatchers { val UTC = ZoneId.of("Z") test("non-overlapping sorted") { val index1: DateTimeIndex = uniform(dt("2015-04-10"), 5, new DayFrequency(2), UTC) val index2: DateTimeIndex = uniform(dt("2015-05-10"), 5, new DayFrequency(2), UTC) val index3: DateTimeIndex = irregular(Array( dt("2015-06-10"), dt("2015-06-13"), dt("2015-06-15"), dt("2015-06-20"), dt("2015-06-25") ), UTC) DateTimeIndexUtils.union(Array(index1, index2, index3), UTC) should be ( hybrid(Array(index1, index2, index3))) } test("non-overlapping non-sorted") { val index1: DateTimeIndex = uniform(dt("2015-04-10"), 5, new DayFrequency(2), UTC) val index2: DateTimeIndex = uniform(dt("2015-05-10"), 5, new DayFrequency(2), UTC) val index3: DateTimeIndex = irregular(Array( dt("2015-06-10"), dt("2015-06-13"), dt("2015-06-15"), dt("2015-06-20"), dt("2015-06-25") ), UTC) DateTimeIndexUtils.union(Array(index3, index1, index2), UTC) should be ( hybrid(Array(index1, index2, index3))) } test("overlapping uniform and irregular") { val index1: DateTimeIndex = uniform(dt("2015-04-10"), 5, new DayFrequency(2), UTC) val index2: DateTimeIndex = uniform(dt("2015-05-10"), 5, new DayFrequency(2), UTC) val index3: DateTimeIndex = irregular(Array( dt("2015-04-09"), dt("2015-04-11"), dt("2015-05-01"), dt("2015-05-10"), dt("2015-06-25") ), UTC) DateTimeIndexUtils.union(Array(index3, index1, index2), UTC) should be ( hybrid(Array( irregular(Array( dt("2015-04-09"), dt("2015-04-10"), dt("2015-04-11")), UTC), uniform(dt("2015-04-12"), 4, new DayFrequency(2), UTC), irregular(Array(dt("2015-05-01"), dt("2015-05-10")), UTC), uniform(dt("2015-05-12"), 4, new DayFrequency(2), UTC), irregular(Array(dt("2015-06-25")), UTC) ))) } def dt(dt: String, zone: ZoneId = UTC): ZonedDateTime = { val splits = dt.split("-").map(_.toInt) ZonedDateTime.of(splits(0), splits(1), splits(2), 0, 0, 0, 0, zone) } }
Example 70
Source File: Timer.scala From Adenium with Apache License 2.0 | 5 votes |
package com.adenium.utils import java.time.{Instant, ZoneId} import java.time.format.DateTimeFormatter import java.util.Locale import com.adenium.utils.May._ object Timer { def currentMillis: String = Instant.now().toEpochMilli.toString ////////////////////////////////////////////////// def Timer[A] ( f : => A) : ( A, Long) = { val s = System.currentTimeMillis val r = f val e = System.currentTimeMillis ( r, e-s ) } def TimeLog[A]( f : => A)(msg: String): A = { val ( r, t) = Timer( f) Logger.logWarning( s"[ Time spent ] $t in $msg") r } def UnitTimer( f : => Unit) : Long = { val ( _, t) = Timer( f) t } }
Example 71
Source File: BlockStampTest.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.core.protocol import java.time.{ZoneId, ZonedDateTime} import org.bitcoins.core.protocol.BlockStamp.{BlockHash, BlockHeight, BlockTime} import org.bitcoins.crypto.DoubleSha256DigestBE import org.bitcoins.testkit.util.BitcoinSUnitTest import scala.util.Success class BlockStampTest extends BitcoinSUnitTest { it must "format and parse its string representation" in { val blockHeight = BlockHeight(0xc0de) assert(blockHeight.mkString == "49374") assert(BlockStamp.fromString(blockHeight.mkString) == blockHeight) val hex = "000102030405060708090a0b0c0d0e0f00112233445566778899aabbccddeeff" val blockHash = BlockHash(DoubleSha256DigestBE.fromHex(hex)) assert(blockHash.mkString == hex) assert(BlockStamp.fromString(blockHash.mkString) == blockHash) val hex1 = DoubleSha256DigestBE.empty.hex val blockHash1 = BlockHash(DoubleSha256DigestBE.fromHex(hex1)) assert(blockHash1.mkString == hex1) assert(BlockStamp.fromString(blockHash1.mkString) == blockHash1) val time = ZonedDateTime.of(2089, 4, 15, 16, 25, 37, 0, ZoneId.of("UTC")) val blockTime = BlockTime(time) assert(blockTime.mkString == "2089-04-15T16:25:37Z") assert(BlockStamp.fromString(blockTime.mkString) == blockTime) } }
Example 72
Source File: OfferResponseSpec.scala From scala-stellar-sdk with Apache License 2.0 | 5 votes |
package stellar.sdk.model.response import java.time.ZoneId import java.time.format.DateTimeFormatter import java.util.Locale import org.json4s.NoTypeHints import org.json4s.native.JsonMethods.parse import org.json4s.native.Serialization import org.specs2.mutable.Specification import stellar.sdk.model.{Amount, Asset, NonNativeAsset} import stellar.sdk.ArbitraryInput class OfferResponseSpec extends Specification with ArbitraryInput { implicit val formats = Serialization.formats(NoTypeHints) + OfferRespDeserializer private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneId.of("UTC")) "an offer response document" should { "parse to an offer response" >> prop { or: OfferResponse => val json = s""" |{ | "_links": { | "self": { | "href": "https://horizon-testnet.stellar.org/offers/101542" | }, | "offer_maker": { | "href": "https://horizon-testnet.stellar.org/accounts/GCXYKQF35XWATRB6AWDDV2Y322IFU2ACYYN5M2YB44IBWAIITQ4RYPXK" | } | }, | "id": ${or.id}, | "paging_token": "101542", | "seller": "${or.seller.accountId}", | "selling": { | ${assetJson(or.selling.asset)} | }, | "buying": { | ${assetJson(or.buying)} | }, | "amount": "${amountString(or.selling)}", | "price_r": { | "n": ${or.price.n}, | "d": ${or.price.d} | }, | "price": "3.0300000", | "last_modified_ledger": ${or.lastModifiedLedger}, | "last_modified_time": "${formatter.format(or.lastModifiedTime)}" |} | """.stripMargin parse(json).extract[OfferResponse] mustEqual or } } def assetJson(asset: Asset) = asset match { case nn: NonNativeAsset => s""" |"asset_type": "${nn.typeString}", |"asset_code": "${nn.code}", |"asset_issuer": "${nn.issuer.accountId}" """.stripMargin.trim case _ => """"asset_type": "native"""" } def amountString(a: Amount): String = "%.7f".formatLocal(Locale.ROOT, a.units / math.pow(10, 7)) }
Example 73
Source File: LedgerResponseSpec.scala From scala-stellar-sdk with Apache License 2.0 | 5 votes |
package stellar.sdk.model.response import java.time.ZoneId import java.time.format.DateTimeFormatter import org.json4s.NoTypeHints import org.json4s.native.JsonMethods.parse import org.json4s.native.Serialization import org.specs2.mutable.Specification import stellar.sdk.ArbitraryInput class LedgerResponseSpec extends Specification with ArbitraryInput { val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneId.of("UTC")) implicit val formats = Serialization.formats(NoTypeHints) + LedgerRespDeserializer "a ledger response document" should { "parse to a ledger response" >> prop { lr: LedgerResponse => val json = s""" |{ | "_links": { | "self": { | "href": "http://horizon-testnet.stellar.org/ledgers/11" | }, | "transactions": { | "href": "http://horizon-testnet.stellar.org/ledgers/11/transactions{?cursor,limit,order}", | "templated": true | }, | "operations": { | "href": "http://horizon-testnet.stellar.org/ledgers/11/operations{?cursor,limit,order}", | "templated": true | }, | "payments": { | "href": "http://horizon-testnet.stellar.org/ledgers/11/payments{?cursor,limit,order}", | "templated": true | }, | "effects": { | "href": "http://horizon-testnet.stellar.org/ledgers/11/effects{?cursor,limit,order}", | "templated": true | } | }, | "id": "${lr.id}", | "paging_token": "47244640256", | "hash": "${lr.hash}", | ${lr.previousHash.map(h => s""""prev_hash": "$h",""").getOrElse("")} | "sequence": ${lr.sequence}, | "successful_transaction_count": ${lr.successTransactionCount}, | "failed_transaction_count": ${lr.failureTransactionCount}, | "operation_count": ${lr.operationCount}, | "closed_at": "${formatter.format(lr.closedAt)}", | "total_coins": "${lr.totalCoins.toDisplayUnits}", | "fee_pool": "${lr.feePool.toDisplayUnits}", | "base_fee_in_stroops": ${lr.baseFee.units}, | "base_reserve_in_stroops": ${lr.baseReserve.units}, | "max_tx_set_size": ${lr.maxTxSetSize}, | "protocol_version": 4 |} """.stripMargin parse(json).extract[LedgerResponse] must beLike { case actual: LedgerResponse => actual.copy(closedAt = lr.closedAt) mustEqual lr actual.closedAt.toInstant.toEpochMilli mustEqual lr.closedAt.toInstant.toEpochMilli } } "calculate transaction count as sum of failed and successful transactions" >> prop { lr: LedgerResponse => lr.transactionCount mustEqual lr.failureTransactionCount + lr.successTransactionCount } } }
Example 74
Source File: ParquetFiberDataLoader.scala From OAP with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.execution.datasources.oap.io import java.io.IOException import java.time.ZoneId import org.apache.hadoop.conf.Configuration import org.apache.parquet.hadoop.ParquetFiberDataReader import org.apache.parquet.hadoop.api.InitContext import org.apache.parquet.hadoop.utils.Collections3 import org.apache.spark.sql.execution.datasources.oap.filecache.FiberCache import org.apache.spark.sql.execution.datasources.parquet.{ParquetReadSupportWrapper, VectorizedColumnReader} import org.apache.spark.sql.execution.vectorized.OnHeapColumnVector import org.apache.spark.sql.oap.OapRuntime import org.apache.spark.sql.types._ private[oap] case class ParquetFiberDataLoader( configuration: Configuration, reader: ParquetFiberDataReader, blockId: Int) { @throws[IOException] def loadSingleColumn: FiberCache = { val footer = reader.getFooter val fileSchema = footer.getFileMetaData.getSchema val fileMetadata = footer.getFileMetaData.getKeyValueMetaData val readContext = new ParquetReadSupportWrapper() .init(new InitContext(configuration, Collections3.toSetMultiMap(fileMetadata), fileSchema)) val requestedSchema = readContext.getRequestedSchema val sparkRequestedSchemaString = configuration.get(ParquetReadSupportWrapper.SPARK_ROW_REQUESTED_SCHEMA) val sparkSchema = StructType.fromString(sparkRequestedSchemaString) assert(sparkSchema.length == 1, s"Only can get single column every time " + s"by loadSingleColumn, the columns = ${sparkSchema.mkString}") val dataType = sparkSchema.fields(0).dataType // Notes: rowIds is IntegerType in oap index. val rowCount = reader.getFooter.getBlocks.get(blockId).getRowCount.toInt val columnDescriptor = requestedSchema.getColumns.get(0) val originalType = requestedSchema.asGroupType.getFields.get(0).getOriginalType val blockMetaData = footer.getBlocks.get(blockId) val fiberData = reader.readFiberData(blockMetaData, columnDescriptor) val columnReader = new VectorizedColumnReader(columnDescriptor, originalType, fiberData.getPageReader(columnDescriptor), ZoneId.systemDefault, true) if (OapRuntime.getOrCreate.fiberCacheManager.dataCacheCompressEnable) { ParquetDataFiberCompressedWriter.dumpToCache( columnReader, rowCount, dataType) } else { val column = new OnHeapColumnVector(rowCount, dataType) columnReader.readBatch(rowCount, column) ParquetDataFiberWriter.dumpToCache( column.asInstanceOf[OnHeapColumnVector], rowCount) } } }
Example 75
Source File: VectorizedFilePartitionReaderHandler.scala From OAP with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.execution.datasources.v2 import com.intel.sparkColumnarPlugin.datasource.VectorizedParquetArrowReader import java.net.URI import java.time.ZoneId import org.apache.hadoop.mapreduce._ import org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl import org.apache.hadoop.fs.Path import org.apache.spark.sql.connector.read.{InputPartition, PartitionReaderFactory, PartitionReader} import org.apache.spark.sql.execution.datasources.{FilePartition, PartitionedFile} import org.apache.spark.sql.execution.datasources.v2.PartitionedFileReader import org.apache.spark.sql.execution.datasources.v2.parquet.ParquetPartitionReaderFactory import org.apache.spark.sql.execution.datasources.v2.FilePartitionReader import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector} object VectorizedFilePartitionReaderHandler { def get( inputPartition: InputPartition, parquetReaderFactory: ParquetPartitionReaderFactory) : FilePartitionReader[ColumnarBatch] = { val iter: Iterator[PartitionedFileReader[ColumnarBatch]] = inputPartition.asInstanceOf[FilePartition].files.toIterator.map { file => val filePath = new Path(new URI(file.filePath)) val split = new org.apache.parquet.hadoop.ParquetInputSplit( filePath, file.start, file.start + file.length, file.length, Array.empty, null) //val timestampConversion: Boolean = sqlConf.isParquetINT96TimestampConversion val capacity = 4096 //partitionReaderFactory.createColumnarReader(inputPartition) val dataSchema = parquetReaderFactory.dataSchema val readDataSchema = parquetReaderFactory.readDataSchema val conf = parquetReaderFactory.broadcastedConf.value.value val attemptId = new TaskAttemptID(new TaskID(new JobID(), TaskType.MAP, 0), 0) val hadoopAttemptContext = new TaskAttemptContextImpl(conf, attemptId) val vectorizedReader = new VectorizedParquetArrowReader(split.getPath().toString(), null, false, capacity, dataSchema, readDataSchema) vectorizedReader.initialize(split, hadoopAttemptContext) val partitionReader = new PartitionReader[ColumnarBatch] { override def next(): Boolean = vectorizedReader.nextKeyValue() override def get(): ColumnarBatch = vectorizedReader.getCurrentValue.asInstanceOf[ColumnarBatch] override def close(): Unit = vectorizedReader.close() } PartitionedFileReader(file, partitionReader) } new FilePartitionReader[ColumnarBatch](iter) } }