java.time.format.DateTimeFormatter Scala Examples
The following examples show how to use java.time.format.DateTimeFormatter.
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: CalendarFormats.scala From sjson-new with Apache License 2.0 | 6 votes |
package sjsonnew import java.util.{ Calendar, GregorianCalendar } import java.time._ import java.time.format.DateTimeFormatter trait CalendarFormats { self: IsoFormats => private val utc: ZoneId = ZoneId.of("UTC") implicit val localDateTimeStringIso: IsoString[LocalDateTime] = { IsoString.iso[LocalDateTime]( (ld: LocalDateTime) => { val datetimefmt = DateTimeFormatter.ISO_LOCAL_DATE_TIME ld.format(datetimefmt) }, (s: String) => { val datetimefmt = DateTimeFormatter.ISO_LOCAL_DATE_TIME LocalDateTime.parse(s, datetimefmt) }) } }
Example 2
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 3
Source File: Sessionize.scala From ml-in-scala with The Unlicense | 5 votes |
package org.akozlov.chapter06 import java.io._ import java.time.ZoneOffset import java.time.LocalDateTime import java.time.format.DateTimeFormatter import org.apache.spark.{SparkConf,SparkContext} import org.apache.spark.storage.StorageLevel object Sessionize extends App { val sc = new SparkContext("local[8]", "Sessionize", new SparkConf()) val checkoutPattern = ".*>checkout.*".r.pattern // a basic page view structure case class PageView(ts: String, path: String) extends Serializable with Ordered[PageView] { override def toString: String = { s"($ts #$path)" } def compare(other: PageView) = ts compare other.ts } // represent a session case class Session[A <: PageView](id: String, visits: Seq[A]) extends Serializable { override def toString: String = { val vsts = visits.mkString("[", ",", "]") s"($id -> $vsts)" } } def toEpochSeconds(str: String) = { LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toEpochSecond(ZoneOffset.UTC) } val sessions = sc.textFile("data/clickstream") .map(line => {val parts = line.split("\t"); (parts(4), new PageView(parts(0), parts(20)))}) .groupByKey.map(x => { new Session(x._1, x._2.toSeq.sorted) } ) .cache // sessions.take(100).foreach(println) def findAllCheckoutSessions(s: Session[PageView]) = { s.visits.tails.filter { _ match { case PageView(ts1, "mycompanycom>homepage") :: PageView(ts2, page) :: tail if (page != "mycompanycom>homepage" ) => true; case _ => false } } .foldLeft(Seq[Session[PageView]]()) { case (r, x) => { x.find(y => checkoutPattern.matcher(y.path).matches) match { case Some(checkout) if (toEpochSeconds(checkout.ts) > toEpochSeconds(x.head.ts) + 60) => r.:+(new Session(s.id, x.slice(0, x.indexOf(checkout)))) case _ => r } } } } val prodLandingSessions = sessions.flatMap(findAllCheckoutSessions) prodLandingSessions.collect.foreach(println) sc.stop() }
Example 4
Source File: MistLoggingSpec.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.core.logging import java.time.format.DateTimeFormatter import java.time.{LocalDateTime, ZoneOffset} import org.scalatest.{FunSpec, Matchers} class MistLoggingSpec extends FunSpec with Matchers { describe("levels") { it("should restore level from int") { Level.fromInt(1) shouldBe Level.Debug Level.fromInt(2) shouldBe Level.Info Level.fromInt(3) shouldBe Level.Warn Level.fromInt(4) shouldBe Level.Error } } describe("log event") { it("should have correct format") { val ts = LocalDateTime.now(ZoneOffset.UTC) val e = LogEvent.mkInfo("job-id", "Message", ts.toInstant(ZoneOffset.UTC).toEpochMilli) val expectedDate = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(ts) val expected = s"INFO $expectedDate [job-id] Message" e.mkString shouldBe expected } it("should have stack traces") { val ts = LocalDateTime.now(ZoneOffset.UTC) val error = new RuntimeException("Test error") val e = LogEvent.mkError("job-id", "Error", Some(error), ts.toInstant(ZoneOffset.UTC).toEpochMilli) val expectedDate = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(ts) val expected = s"""ERROR $expectedDate [job-id] Error |java.lang.RuntimeException: Test error""".stripMargin e.mkString should startWith(expected) } } }
Example 5
Source File: LogEvent.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.core.logging import java.io.{PrintWriter, StringWriter} import java.time.format.DateTimeFormatter import java.time._ case class LogEvent( from: String, message: String, timeStamp: Long, level: Int, errTrace: Option[String]) { def mkString: String = { val date = formatDate val error = errTrace.map(s => "\n" + s).getOrElse("") s"${typedLevel.name} $date [$from] $message$error" } private def formatDate: String = { val inst = Instant.ofEpochMilli(timeStamp) val date = LocalDateTime.ofInstant(inst, ZoneOffset.UTC) DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(date) } def typedLevel: Level = Level.fromInt(level) } object LogEvent { def mkDebug(from: String, message: String, ts: Long = mkTimestamp): LogEvent = nonFatal(Level.Debug, from, message, ts) def mkInfo(from: String, message: String, ts: Long = mkTimestamp): LogEvent = nonFatal(Level.Info, from, message, ts) def mkWarn(from: String, message: String, ts: Long = mkTimestamp): LogEvent = nonFatal(Level.Warn, from, message, ts) def mkError(from: String, message: String, t: Throwable): LogEvent = mkError(from, message, Some(t)) def mkError(from: String, message: String, optT: Option[Throwable] = None, ts: Long = mkTimestamp): LogEvent = { val errTrace = optT.map(ex => { val writer = new StringWriter() ex.printStackTrace(new PrintWriter(writer)) writer.toString }) LogEvent(from, message, ts, Level.Error.value, errTrace) } def nonFatal(level: Level, from: String, message: String, ts: Long): LogEvent = LogEvent(from, message, ts, level.value, None) private def mkTimestamp: Long = LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.UTC).toEpochMilli }
Example 6
Source File: GithubIssue389.scala From avro4s with Apache License 2.0 | 5 votes |
package com.sksamuel.avro4s.github import java.time.OffsetDateTime import java.time.format.DateTimeFormatter import com.sksamuel.avro4s.{AvroSchema, Decoder, Encoder} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec class GithubIssue389 extends AnyWordSpec with Matchers { "OffsetDateTime" must { val NOW = OffsetDateTime.now() val MAX = OffsetDateTime.MAX val MIN = OffsetDateTime.MIN "generate a schema with a logical type backed by a string" in { val schema = AvroSchema[OffsetDateTime] val expected = new org.apache.avro.Schema.Parser().parse(this.getClass.getResourceAsStream("/github/github_389.json")) schema shouldBe expected } "encode to an iso formatted String" in { def testEncode(datetime: OffsetDateTime): Unit = { val encoded = Encoder[OffsetDateTime].encode(datetime) encoded shouldBe datetime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) } testEncode(NOW) testEncode(MAX) testEncode(MIN) } "decode an iso formatted String to an equivalent OffsetDatetime object" in { def testDecode(datetime: OffsetDateTime): Unit = { val dateTimeString = datetime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) val decoder = Decoder[OffsetDateTime].decode(dateTimeString) decoder shouldBe datetime } testDecode(NOW) testDecode(MAX) testDecode(MIN) } "round trip encode and decode into an equivalent object" in { def testRoundTrip(datetime: OffsetDateTime): Unit = { val encoded = Encoder[OffsetDateTime].encode(datetime) val decoded = Decoder[OffsetDateTime].decode(encoded) decoded shouldBe datetime } testRoundTrip(NOW) testRoundTrip(MAX) testRoundTrip(MIN) } } }
Example 7
Source File: DiscoveryUtilsSpec.scala From odsc-west-streaming-trends with GNU General Public License v3.0 | 5 votes |
package com.twilio.open.streaming.trend.discovery import java.time.Instant import java.time.format.DateTimeFormatter import com.twilio.open.streaming.trend.discovery.protocol.{CallEvent, Dimensions} import org.scalatest.{FlatSpec, Matchers} class DiscoveryUtilsSpec extends FlatSpec with Matchers { // example using java serialization with case class "DiscoveryUtils" should " serialize and deserialize a CallEvent object" in { val eventTime = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2018-03-08T18:00:00Z")) val loggedTime = eventTime.plusSeconds(34) //eventTime: Long, loggedTime: Long, eventId: String, eventType: String,dimensions: Dimensions, signalingEvent: Option[SignalingEvent] //case class Dimensions(country: Option[String], continent: Option[String], carrier: Option[String],direction: Option[String]) val ce = CallEvent(eventTime.toEpochMilli, loggedTime.toEpochMilli, "uuid1", "signaling", Dimensions( country = Some("us"), continent = Some("na"), carrier = Some("verizon"), direction = Some("inbound") ), None) val ceSer = DiscoveryUtils.serialize(ce) val ceDeser = DiscoveryUtils.deserialize[CallEvent](ceSer) ce.equals(ceDeser) } }
Example 8
Source File: EpisodeSettingsExtractor.scala From scalalaz-gen with Apache License 2.0 | 5 votes |
package ru.scalalaz.gen.parsing import java.time.LocalDate import java.time.format.{ DateTimeFormatter, DateTimeParseException } import cats.Apply import cats.data.Validated.Valid import cats.data.{ Validated, ValidatedNel } import ru.scalalaz.gen.{ Enclosure, EpisodeSettings, SpecialPageSettings } object EpisodeSettingsExtractor { import ru.scalalaz.gen.parsing.EpisodeErrors._ def fromMap( map: Map[String, Option[String]] ): ValidatedNel[PageParseError, SpecialPageSettings] = new SettingsExtractor(map).extract class SettingsExtractor(map: Map[String, Option[String]]) { def extract: ValidatedNel[PageParseError, SpecialPageSettings] = Apply[ValidatedNel[PageParseError, *]].map2( read("title").toValidatedNel, read("date").andThen(parseDate).toValidatedNel ) { case (title, date) => //val enc = Enclosure(encUrl, if (encLength != "") encLength.toInt else -1) SpecialPageSettings(title, date) } private def read(key: String): Validated[PageParseError, String] = Validated.fromOption(map.get(key).flatten, MissingKey(key)) private def optRead(key: String): Validated[PageParseError, String] = Valid(map.get(key).flatten.getOrElse("")) private def parseDate( date: String ): Validated[PageParseError, LocalDate] = { def toDate = LocalDate.parse(date, DateTimeFormatter.ISO_LOCAL_DATE) Validated .catchOnly[DateTimeParseException](toDate) .leftMap(e => InvalidDate(e.getMessage)) } } }
Example 9
Source File: data.scala From scalalaz-gen with Apache License 2.0 | 5 votes |
package ru.scalalaz.gen import java.nio.file.Path import java.time.format.DateTimeFormatter import java.time.{ LocalDate, ZoneOffset } import knockoff.DefaultDiscounter._ import _root_.knockoff._ case class EpisodeSettings(title: String, description: String, audio: Enclosure, page: String, date: LocalDate) { def RFCDate: String = { val dateTime = date.atStartOfDay().atOffset(ZoneOffset.UTC) dateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME) } def ISODate: String = { date.format(DateTimeFormatter.ISO_DATE) } } case class Episode(settings: EpisodeSettings, content: String) { def title: String = settings.title def asHtml: String = { val blocks = knockoff(content) toXHTML(blocks).mkString } } case class EpisodeFile(path: Path, episode: Episode) case class SpecialPageSettings(title: String, date: LocalDate) { def ISODate: String = { date.format(DateTimeFormatter.ISO_DATE) } } case class Page(settings: SpecialPageSettings, content: String) { def title: String = settings.title def asHtml: String = { val blocks = knockoff(content) toXHTML(blocks).mkString } } case class PageFile(path: Path, page: Page)
Example 10
Source File: SparkEnv.scala From comet-data-pipeline with Apache License 2.0 | 5 votes |
package com.ebiznext.comet.config import java.time.LocalDateTime import java.time.format.DateTimeFormatter import com.typesafe.scalalogging.StrictLogging import org.apache.spark.SparkConf import org.apache.spark.sql.SparkSession lazy val session: SparkSession = { val session = if (settings.comet.hive) SparkSession.builder.config(config).enableHiveSupport().getOrCreate() else SparkSession.builder.config(config).getOrCreate() logger.info("Spark Version -> " + session.version) logger.info(session.conf.getAll.mkString("\n")) session } }
Example 11
Source File: Codecs.scala From bay-scalajs.g8 with Apache License 2.0 | 5 votes |
package shared.utils import cats.syntax.either._ import io.circe.Decoder import io.circe.Encoder import java.time._ import java.time.format.DateTimeFormatter trait Codecs extends UpickleCodecs with CirceCodecs object Codecs extends Codecs trait CirceCodecs { // use more stable encodings then standard private val fmt: DateTimeFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSZZZZZ") private val fmtLocal: DateTimeFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS") private val fmtDate: DateTimeFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd") private val fmtTime: DateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS") def createCirceCodec[T](encode: T => String, decode: String => T): (Encoder[T], Decoder[T]) = (Encoder.encodeString.contramap[T](encode), Decoder.decodeString.emap(str => Either.catchNonFatal(decode(str)).leftMap(_.getMessage))) implicit val (encodeOffsetDateTimeCirce, decodeOffsetDateTimeCirce) = createCirceCodec[OffsetDateTime](fmt.format, OffsetDateTime.parse) implicit val (encodeLocalDateTimeCirce, decodeLocalDateTimeCirce) = createCirceCodec[LocalDateTime](fmtLocal.format, LocalDateTime.parse) implicit val (encodeLocalDateCirce, decodeLocalDateCirce) = createCirceCodec[LocalDate](fmtDate.format, LocalDate.parse) implicit val (encodeLocalTimeCirce, decodeLocalTimeCirce) = createCirceCodec[LocalTime](fmtTime.format, LocalTime.parse) implicit val (encodeDurationCirce, decodeDurationCirce) = createCirceCodec[Duration](_.toString(), Duration.parse) } object CirceCodecs extends CirceCodecs trait UpickleCodecs { import upickle.default._ def createUpickleCode[T](encode: T => String, decode: String => T): (Writer[T], Reader[T]) = (Writer[T](e => upickle.Js.Str(encode(e))), Reader[T] { case upickle.Js.Str(jsStr) => decode(jsStr) }) implicit val (encodeOffsetDateTimeUpickle, decodeOffsetDateTimeUpickle) = createUpickleCode[OffsetDateTime](_.toString(), OffsetDateTime.parse) implicit val (encodeLocalDateTimeUpickle, decodeLocalDateTimeUpickle) = createUpickleCode[LocalDateTime](_.toString(), LocalDateTime.parse) implicit val (encodeLocalDateUpickle, decodeLocalDateUpickle) = createUpickleCode[LocalDate](_.toString(), LocalDate.parse) implicit val (encodeLocalTimeUpickle, decodeLocalTimeUpickle) = createUpickleCode[LocalTime](_.toString(), LocalTime.parse) implicit val (encodeDurationUpickle, decodeDurationUpickle) = createUpickleCode[Duration](_.toString(), Duration.parse) } object UpickleCodecs extends UpickleCodecs
Example 12
Source File: DateFormatUtils.scala From hail with MIT License | 5 votes |
package is.hail.utils import java.time.DayOfWeek import java.util.Locale import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder, TextStyle} import java.time.temporal.{ChronoField, WeekFields} import java.time.chrono.{ChronoLocalDate, Chronology} object DateFormatUtils { def parseDateFormat(str: String, locale: Locale): DateTimeFormatter = { val fmt = new DateTimeFormatterBuilder val chrono = Chronology.ofLocale(locale) lazy val _1970 = chrono.date(1970, 1, 1) val SUNDAY_START_ALWAYS = WeekFields.of(DayOfWeek.SUNDAY, 7) val MONDAY_START_ALWAYS = WeekFields.of(DayOfWeek.MONDAY, 7) def char(c: Char): Unit = fmt.appendLiteral(c) def spec(c: Char): Unit = { c match { case '%' => char('%') case 'A' => fmt.appendText(ChronoField.DAY_OF_WEEK, TextStyle.FULL) case 'a' => fmt.appendText(ChronoField.DAY_OF_WEEK, TextStyle.SHORT) case 'B' => fmt.appendText(ChronoField.MONTH_OF_YEAR, TextStyle.FULL) case 'b' | 'h' => fmt.appendText(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT) case 'D' => alternating("m/d/y") case 'd' => fmt.appendValue(ChronoField.DAY_OF_MONTH, 2) case 'e' => fmt.padNext(2); fmt.appendValue(ChronoField.DAY_OF_MONTH) case 'F' => alternating("Y-m-d") case 'H' => fmt.appendValue(ChronoField.HOUR_OF_DAY, 2) case 'I' => fmt.appendValue(ChronoField.CLOCK_HOUR_OF_AMPM, 2) case 'j' => fmt.appendValue(ChronoField.DAY_OF_YEAR, 3) case 'k' => fmt.padNext(2); fmt.appendValue(ChronoField.HOUR_OF_DAY) case 'l' => fmt.padNext(2); fmt.appendValue(ChronoField.CLOCK_HOUR_OF_AMPM) case 'M' => fmt.appendValue(ChronoField.MINUTE_OF_HOUR, 2) case 'm' => fmt.appendValue(ChronoField.MONTH_OF_YEAR, 2) case 'n' => char('\n') case 'p' => fmt.appendText(ChronoField.AMPM_OF_DAY, TextStyle.SHORT) case 'R' => alternating("H:M") case 'r' => alternating("I:M:S p") case 'S' => fmt.appendValue(ChronoField.SECOND_OF_MINUTE, 2) case 's' => fmt.appendValue(ChronoField.INSTANT_SECONDS) case 'T' => alternating("H:M:S") case 't' => char('\t') case 'U' => fmt.appendValue(SUNDAY_START_ALWAYS.weekOfYear(), 2) //Sunday first day case 'u' => fmt.appendValue(WeekFields.ISO.dayOfWeek()) // 1-7, starts on Monday case 'V' => fmt.appendValue(WeekFields.ISO.weekOfWeekBasedYear(), 2) case 'v' => alternating("e-b-Y") case 'W' => fmt.appendValue(MONDAY_START_ALWAYS.weekOfYear(), 2) // Monday first day case 'Y' => fmt.appendValue(ChronoField.YEAR, 4) case 'y' => fmt.appendValueReduced(ChronoField.YEAR, 2, 2, _1970) case 'Z' => fmt.appendZoneId() case 'z' => fmt.appendOffsetId() case 'E' | 'O' => char(c) // Python just keeps these two letters for whatever reason. case 'C' | 'c' | 'G' | 'g' | 'w'| 'X' | 'x' => throw new HailException(s"Currently unsupported time formatting character: $c") case d => fatal(s"invalid time format descriptor: $d") } } def alternating(s: String): Unit = { var isSpec = true for (c <- s) { if (isSpec) spec(c) else char(c) isSpec = !isSpec } } val chrs = str.iterator while (chrs.hasNext) chrs.next() match { case '%' => spec(if (chrs.hasNext) chrs.next() else '%') case c => char(c) } fmt.toFormatter } }
Example 13
Source File: CustomScalars.scala From graphql-gateway with Apache License 2.0 | 5 votes |
package sangria.gateway.schema import java.time.format.DateTimeFormatter import java.time.{Instant, OffsetDateTime, ZoneOffset, ZonedDateTime} import sangria.schema._ import sangria.ast import sangria.validation.ValueCoercionViolation import scala.util.{Failure, Success, Try} object CustomScalars { implicit val DateTimeType = ScalarType[ZonedDateTime]("DateTime", description = Some("DateTime is a scalar value that represents an ISO8601 formatted date and time."), coerceOutput = (date, _) ⇒ DateTimeFormatter.ISO_INSTANT.format(date), coerceUserInput = { case s: String ⇒ parseDateTime(s) match { case Success(date) ⇒ Right(date) case Failure(_) ⇒ Left(DateCoercionViolation) } case _ ⇒ Left(DateCoercionViolation) }, coerceInput = { case ast.StringValue(s, _, _, _, _) ⇒ parseDateTime(s) match { case Success(date) ⇒ Right(date) case Failure(_) ⇒ Left(DateCoercionViolation) } case _ ⇒ Left(DateCoercionViolation) }) def parseDateTime(s: String) = Try(DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(s).asInstanceOf[ZonedDateTime]) case object DateCoercionViolation extends ValueCoercionViolation("Date value expected") }
Example 14
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 15
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 16
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 17
Source File: TimeService.scala From get-programming-with-scala with MIT License | 5 votes |
package org.example.time import java.time.format.DateTimeFormatter import cats.effect.IO import org.http4s.HttpService import org.http4s.dsl.Http4sDsl class TimeService extends Http4sDsl[IO] { private val printer = new TimePrinter(DateTimeFormatter.RFC_1123_DATE_TIME) val service = HttpService[IO] { case GET -> Root / "datetime" / country => try { Ok(printer.now(country)) } catch { case ex: IllegalStateException => NotFound(ex.getMessage) } } }
Example 18
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 19
Source File: package.scala From pureconfig with Mozilla Public License 2.0 | 5 votes |
package pureconfig import java.time._ import java.time.format.DateTimeFormatter import com.typesafe.config.ConfigValueFactory import pureconfig.ConfigConvert.{ catchReadError, viaNonEmptyString } import pureconfig.error.FailureReason import scala.collection.JavaConverters._ package object configurable { def localDateConfigConvert(formatter: DateTimeFormatter): ConfigConvert[LocalDate] = viaNonEmptyString[LocalDate]( catchReadError(LocalDate.parse(_, formatter)), _.format(formatter)) def localTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[LocalTime] = viaNonEmptyString[LocalTime]( catchReadError(LocalTime.parse(_, formatter)), _.format(formatter)) def localDateTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[LocalDateTime] = viaNonEmptyString[LocalDateTime]( catchReadError(LocalDateTime.parse(_, formatter)), _.format(formatter)) def monthDayConfigConvert(formatter: DateTimeFormatter): ConfigConvert[MonthDay] = viaNonEmptyString[MonthDay]( catchReadError(MonthDay.parse(_, formatter)), _.format(formatter)) def offsetDateTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[OffsetDateTime] = viaNonEmptyString[OffsetDateTime]( catchReadError(OffsetDateTime.parse(_, formatter)), _.format(formatter)) def offsetTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[OffsetTime] = viaNonEmptyString[OffsetTime]( catchReadError(OffsetTime.parse(_, formatter)), _.format(formatter)) def yearMonthConfigConvert(formatter: DateTimeFormatter): ConfigConvert[YearMonth] = viaNonEmptyString[YearMonth]( catchReadError(YearMonth.parse(_, formatter)), _.format(formatter)) def zonedDateTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[ZonedDateTime] = viaNonEmptyString[ZonedDateTime]( catchReadError(ZonedDateTime.parse(_, formatter)), _.format(formatter)) def genericMapReader[K, V](keyParser: String => Either[FailureReason, K])(implicit readerV: Derivation[ConfigReader[V]]): ConfigReader[Map[K, V]] = ConfigReader.fromCursor { cursor => cursor.asMap.right.flatMap { map => map.foldLeft[ConfigReader.Result[Map[K, V]]](Right(Map.empty)) { case (acc, (key, valueCursor)) => val eitherKeyOrError = cursor.scopeFailure(keyParser(key)) val eitherValueOrError = readerV.value.from(valueCursor) ConfigReader.Result.zipWith(acc, ConfigReader.Result.zipWith(eitherKeyOrError, eitherValueOrError)(_ -> _))(_ + _) } } } def genericMapWriter[K, V](keyFormatter: K => String)(implicit writerV: Derivation[ConfigWriter[V]]): ConfigWriter[Map[K, V]] = ConfigWriter.fromFunction[Map[K, V]](map => ConfigValueFactory.fromMap(map.map { case (key, value) => keyFormatter(key) -> writerV.value.to(value) }.asJava)) }
Example 20
Source File: ConfigurableSuite.scala From pureconfig with Mozilla Public License 2.0 | 5 votes |
package pureconfig.configurable import java.time._ import java.time.format.DateTimeFormatter import scala.collection.JavaConverters._ import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigRenderOptions.concise import pureconfig.BaseSuite import pureconfig.arbitrary._ import pureconfig.error.UnknownKey import pureconfig.generic.auto._ import pureconfig.syntax._ class ConfigurableSuite extends BaseSuite { implicit override val generatorDrivenConfig = PropertyCheckConfiguration(minSuccessful = 100) behavior of "configurable converters" implicit val localTimeInstance = localTimeConfigConvert(DateTimeFormatter.ISO_TIME) implicit val localDateInstance = localDateConfigConvert(DateTimeFormatter.ISO_DATE) implicit val localDateTimeInstance = localDateTimeConfigConvert(DateTimeFormatter.ISO_DATE_TIME) implicit val monthDayInstance = monthDayConfigConvert(DateTimeFormatter.ofPattern("MM-dd")) implicit val offsetDateTimeInstance = offsetDateTimeConfigConvert(DateTimeFormatter.ISO_OFFSET_DATE_TIME) implicit val offsetTimeInstance = offsetTimeConfigConvert(DateTimeFormatter.ISO_OFFSET_TIME) implicit val yearMonthInstance = yearMonthConfigConvert(DateTimeFormatter.ofPattern("yyyy-MM")) implicit val zonedDateTimeInstance = zonedDateTimeConfigConvert(DateTimeFormatter.ISO_ZONED_DATE_TIME) checkArbitrary[LocalTime] checkArbitrary[LocalDate] checkArbitrary[LocalDateTime] checkArbitrary[MonthDay] checkArbitrary[OffsetDateTime] checkArbitrary[OffsetTime] checkArbitrary[YearMonth] checkArbitrary[ZonedDateTime] sealed trait Animal final case object Bird extends Animal final case object Monkey extends Animal case class Food(food: String) it should "parse using generic map reader" in { val conf = ConfigFactory.parseString("""{"bird": {"food": "worms"}, "monkey": {"food": "banana"}}""") implicit val reader = genericMapReader[Animal, Food] { case "bird" => Right(Bird) case "monkey" => Right(Monkey) case animal => Left(UnknownKey(s"$animal is unsupported")) } conf.to[Map[Animal, Food]].right.value shouldEqual Map(Bird -> Food("worms"), Monkey -> Food("banana")) } it should "format using generic map writer" in { implicit val writer = genericMapWriter[Animal, Food] { case Bird => "bird" case Monkey => "monkey" } val config = Map[Animal, Food](Bird -> Food("worms"), Monkey -> Food("banana")).toConfig.render(concise()) config shouldEqual """{"bird":{"food":"worms"},"monkey":{"food":"banana"}}""" } }
Example 21
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 22
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 23
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 24
Source File: package.scala From albedo with MIT License | 5 votes |
package ws.vinta.albedo import java.time.LocalDateTime import java.time.format.DateTimeFormatter import org.apache.spark.sql.SparkSession package object settings { private val spark = SparkSession.builder().getOrCreate() private val sc = spark.sparkContext val dataDir: String = sc.getConf.get("spark.albedo.dataDir", "./spark-data") val checkpointDir: String = sc.getConf.get("spark.albedo.checkpointDir", "./spark-data/checkpoint") def today: String = { val now = LocalDateTime.now() val formatter = DateTimeFormatter.ofPattern("yyyyMMdd") now.format(formatter) } def md5(text: String): String = { java.security.MessageDigest.getInstance("MD5").digest(text.getBytes()).map(0xFF & _).map { "%02x".format(_) }.foldLeft(""){_ + _} } }
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: ItTestPlugin.scala From Waves with MIT License | 5 votes |
import java.time.LocalDateTime import java.time.format.DateTimeFormatter import sbt.Keys._ import sbt.Tests.Group import sbt._ // Separate projects for integration tests because of IDEA: https://youtrack.jetbrains.com/issue/SCL-14363#focus=streamItem-27-3061842.0-0 object ItTestPlugin extends AutoPlugin { object autoImport extends ItKeys import autoImport._ override def projectSettings: Seq[Def.Setting[_]] = inConfig(Test)( Seq( logDirectory := { val runId = Option(System.getenv("RUN_ID")).getOrElse { val formatter = DateTimeFormatter.ofPattern("MM-dd--HH_mm_ss") formatter.format(LocalDateTime.now()) // git branch? } val r = target.value / "logs" / runId IO.createDirectory(r) r }, // Example: SCALATEST_EXCLUDE_TAGS="package1.Tag1 package2.Tag2 package3.Tag3" testOptions += { val excludeTags = sys.env.get("SCALATEST_EXCLUDE_TAGS").fold(Seq.empty[String])(Seq("-l", _)) val args = Seq("-fFW", (logDirectory.value / "summary.log").toString) ++ excludeTags Tests.Argument(TestFrameworks.ScalaTest, args: _*) }, parallelExecution in Test := true, tags in test += Tags.ForkedTestGroup -> 1, tags in testOnly += Tags.ForkedTestGroup -> 1, tags in testQuick += Tags.ForkedTestGroup -> 1, testGrouping := { // ffs, sbt! // https://github.com/sbt/sbt/issues/3266 val javaHomeValue = javaHome.value val logDirectoryValue = logDirectory.value val envVarsValue = envVars.value val javaOptionsValue = javaOptions.value for { group <- testGrouping.value suite <- group.tests } yield Group( suite.name, Seq(suite), Tests.SubProcess( ForkOptions( javaHome = javaHomeValue, outputStrategy = outputStrategy.value, bootJars = Vector.empty[java.io.File], workingDirectory = Option(baseDirectory.value), runJVMOptions = Vector( "-Dwaves.it.logging.appender=FILE", s"-Dwaves.it.logging.dir=${logDirectoryValue / suite.name.replaceAll("""(\w)\w*\.""", "$1.")}" // foo.bar.Baz -> f.b.Baz ) ++ javaOptionsValue, connectInput = false, envVars = envVarsValue )) ) } )) } trait ItKeys { val logDirectory = taskKey[File]("The directory where logs of integration tests are written") }
Example 27
Source File: BloombergFieldValueFn.scala From stream-reactor with Apache License 2.0 | 5 votes |
package com.datamountaineer.streamreactor.connect.bloomberg import java.time.format.DateTimeFormatter import java.time.{LocalDate, OffsetTime, ZoneOffset} import com.bloomberglp.blpapi.{Datetime, Element} import scala.collection.JavaConverters._ => // For SEQUENCE, iterate through each element and we need case (map, e) instead of `element` of existing sequence element. //element.elementIterator().asScala.foldLeft(new java.util.LinkedHashMap[String, Any]) { case (map, `element`) => element.elementIterator().asScala.foldLeft(new java.util.LinkedHashMap[String, Any]) { case (map, e) => map.put(e.name().toString, BloombergFieldValueFn(e)) map } //needs to be a java map because of json serialization case _ => if (element.isArray) { (0 until element.numValues()).map { i => BloombergFieldValueFn(element.getValueAsElement(i)) }.asJava } else { element.toString } } } def offsetDateTime(dt: Datetime): String = { val offsetSeconds = if (dt.hasParts(Datetime.TIME_ZONE_OFFSET)) { dt.timezoneOffsetMinutes() * 60 } else { 0 } val offset = ZoneOffset.ofTotalSeconds(offsetSeconds) OffsetTime.of(dt.hour(), dt.minute(), dt.second(), dt.nanosecond(), offset).toString } def localDate(dt: Datetime): String = { LocalDate.of(dt.year(), dt.month(), dt.dayOfMonth()).format(datetimeFormatter) } }
Example 28
Source File: EventQuery.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.model.event import java.time.OffsetDateTime import java.time.format.DateTimeFormatter case class EventQuery(tags: Set[String], `type`: Option[String], timestamp: Option[TimeRange], aggregator: Option[Aggregator] = None) case class TimeRange(lt: Option[String], lte: Option[String], gt: Option[String], gte: Option[String]) object TimeRange { def apply(from: Option[OffsetDateTime], to: Option[OffsetDateTime], includeLower: Boolean, includeUpper: Boolean): TimeRange = { def convert(time: Option[OffsetDateTime]): Option[String] = time.flatMap(t ⇒ Some(t.format(DateTimeFormatter.ISO_DATE_TIME))) val lt = if (to.isDefined && !includeUpper) convert(to) else None val lte = if (to.isDefined && includeUpper) convert(to) else None val gt = if (from.isDefined && !includeLower) convert(from) else None val gte = if (from.isDefined && includeLower) convert(from) else None TimeRange(lt, lte, gt, gte) } }
Example 29
Source File: DeploymentSerializationFormat.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.model.serialization import java.time.format.DateTimeFormatter import io.vamp.common.Lookup import io.vamp.model.artifact.DeploymentService.Status.Phase.Failed import io.vamp.model.artifact._ import io.vamp.model.notification.ModelNotificationProvider import org.json4s.JsonAST.JString import org.json4s._ import scala.collection.mutable.ArrayBuffer object DeploymentSerializationFormat extends io.vamp.common.json.SerializationFormat { override def customSerializers = super.customSerializers :+ new DeploymentSerializer(full = false) :+ new DeploymentServiceStatusSerializer() :+ new DeploymentServiceStatusPhaseSerializer() } object FullDeploymentSerializationFormat extends io.vamp.common.json.SerializationFormat { override def customSerializers = super.customSerializers :+ new DeploymentSerializer(full = true) :+ new DeploymentServiceStatusSerializer() :+ new DeploymentServiceStatusPhaseSerializer() } class DeploymentSerializer(full: Boolean) extends ArtifactSerializer[Deployment] with TraitDecomposer with BlueprintGatewaySerializer with DialectSerializer { override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = { case deployment: Deployment ⇒ val list = new ArrayBuffer[JField] list += JField("name", JString(deployment.name)) list += JField("kind", JString(deployment.kind)) list += JField("metadata", Extraction.decompose(deployment.metadata)(DefaultFormats)) list += JField(Lookup.entry, JString(deployment.lookupName)) list += JField("clusters", Extraction.decompose(deployment.clusters.map(cluster ⇒ cluster.name → cluster).toMap)) list += JField("ports", traits(deployment.ports)) if (full) list += JField("environment_variables", Extraction.decompose(deployment.environmentVariables)) else list += JField("environment_variables", traitsEnv(deployment.environmentVariables, alias = false)) list += JField("hosts", traits(deployment.hosts)) list += JField("dialects", serializeDialects(deployment.dialects)) new JObject(list.toList) } } class DeploymentServiceStatusSerializer extends ArtifactSerializer[DeploymentService.Status] { override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = { case status: DeploymentService.Status ⇒ val list = new ArrayBuffer[JField] list += JField("intention", JString(status.intention.toString)) list += JField("since", JString(status.since.format(DateTimeFormatter.ISO_DATE_TIME))) list += JField("phase", Extraction.decompose(status.phase)) new JObject(list.toList) } } class DeploymentServiceStatusPhaseSerializer extends ArtifactSerializer[DeploymentService.Status.Phase] with ModelNotificationProvider { override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = { case step: DeploymentService.Status.Phase ⇒ val list = new ArrayBuffer[JField] list += JField("name", JString(step.name)) list += JField("since", JString(step.since.format(DateTimeFormatter.ISO_DATE_TIME))) step match { case failure: Failed ⇒ list += JField("notification", JString(message(failure.notification))) case _ ⇒ } new JObject(list.toList) } }
Example 30
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 31
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 32
Source File: BatchExecution.scala From marvin-engine-executor with Apache License 2.0 | 5 votes |
package org.marvin.model import java.time.LocalDateTime import java.time.format.DateTimeFormatter sealed abstract class ExecutionStatus(val name: String, val code: Int) { override def toString: String = name } case object Working extends ExecutionStatus(name="working", code=0) case object Finished extends ExecutionStatus(name="finished", code=1) case object Failed extends ExecutionStatus(name="failed", code=(-1)) case class BatchExecution(actionName: String, protocol: String, datetime: LocalDateTime, status: ExecutionStatus){ override def toString: String = s"$actionName | $protocol | $status" override def equals(obj: scala.Any): Boolean = this.toString == obj.toString val formattedDatetime: String = datetime.format(DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss")) }
Example 33
Source File: package.scala From lighthouse with Apache License 2.0 | 5 votes |
package be.dataminded.lighthouse import java.time.LocalDate import java.time.format.DateTimeFormatter import be.dataminded.lighthouse.common.DateTimeFormatters import scopt.Read import scopt.Read.reads import scala.util.{Failure, Success, Try} package object config { implicit val LocalDateSupport: Read[LocalDate] = reads { timestamp => tryParseLocalDate(timestamp) match { case Success(localDate) => localDate case Failure(e) => throw new IllegalArgumentException(s"The given timestamp: [$timestamp] could not be parsed", e) } } private def tryParseLocalDate(timestamp: String): Try[LocalDate] = { Try { LocalDate.parse(timestamp, DateTimeFormatter.ISO_LOCAL_DATE) } recover { case _ => LocalDate.parse(timestamp, DateTimeFormatters.SimpleDateFormat) } } }
Example 34
Source File: MatchersProperties.scala From cornichon with Apache License 2.0 | 5 votes |
package com.github.agourlay.cornichon.matchers import java.time.Instant import java.time.format.DateTimeFormatter import com.github.agourlay.cornichon.matchers.Matchers._ import io.circe.Json import monix.eval.Task import monix.execution.Scheduler.Implicits.global import org.scalacheck._ import org.scalacheck.Prop._ import org.typelevel.claimant.Claim object MatchersProperties extends Properties("Matchers") { val reasonablyRandomInstantGen: Gen[Instant] = for { randomOffset <- Arbitrary.arbLong.arbitrary } yield Instant.now().plusMillis(randomOffset % 1000000000000L) val instantGen: Gen[Instant] = for { randomOffset <- Arbitrary.arbLong.arbitrary } yield Instant.now().plusMillis(randomOffset) property("any-integer correct for any int") = forAll(Gen.size) { int => Claim { anyInteger.predicate(Json.fromInt(int)) } } property("any-integer incorrect for any alphanum string") = forAll(Gen.alphaNumStr) { alphanum => Claim { !anyInteger.predicate(Json.fromString(alphanum)) } } property("any-positive-integer correct for any positive int") = forAll(Gen.choose(1, Int.MaxValue)) { int => Claim { anyPositiveInteger.predicate(Json.fromInt(int)) } } property("any-positive-integer incorrect for any alphanum string") = forAll(Gen.alphaNumStr) { alphanum => Claim { !anyPositiveInteger.predicate(Json.fromString(alphanum)) } } property("any-negative-integer correct for any negative int") = forAll(Gen.negNum[Int]) { int => Claim { anyNegativeInteger.predicate(Json.fromInt(int)) } } property("any-negative-integer incorrect for any alphanum string") = forAll(Gen.alphaNumStr) { alphanum => Claim { !anyNegativeInteger.predicate(Json.fromString(alphanum)) } } property("any-uuid correct for any valid UUID") = forAll(Gen.uuid) { uuid => Claim { anyUUID.predicate(Json.fromString(uuid.toString)) } } property("any-uuid incorrect for any alphanum string") = forAll(Gen.alphaNumStr) { alphanum => Claim { !anyUUID.predicate(Json.fromString(alphanum)) } } property("any-date-time correct for all ISO-compliant values, including Y10K+ dates") = forAll(instantGen) { instant => Claim { anyDateTime.predicate(Json.fromString(DateTimeFormatter.ISO_INSTANT.format(instant))) } } property("any-date-time correct in parallel") = { forAll(reasonablyRandomInstantGen) { instant => val booleans = 1.to(64).map { _ => Task.delay { anyDateTime.predicate(Json.fromString(DateTimeFormatter.ISO_INSTANT.format(instant))) } } val res = Task.parSequenceUnordered(booleans).runSyncUnsafe().foldLeft(List.empty[Boolean]) { case (acc, e) => e :: acc } Claim(res.forall(_ == true)) } } }
Example 35
Source File: Tweets.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.core.services.twitter import java.time.format.DateTimeFormatter import java.util.Locale import gospeak.core.domain.messages.Message import gospeak.core.domain.utils.Constants object Tweets { private val smallDate = DateTimeFormatter.ofPattern("MMMM dd").withZone(Constants.defaultZoneId.normalized()).withLocale(Locale.ENGLISH) def externalCfpCreated(msg: Message.ExternalCfpCreated): String = { val name = msg.event.twitterAccount.map(_.handle).getOrElse(msg.event.name.value) val date = msg.event.start.map(s => s" on ${smallDate.format(s)}").getOrElse("") val place = msg.event.location.map(l => s" in${l.locality.map(ll => s" $ll,").getOrElse("")} ${l.country}").getOrElse("") val endDate = msg.cfp.close.map(d => s" before ${smallDate.format(d)}").getOrElse("") val user = msg.by.links.twitter.map(_.handle).getOrElse(msg.by.name.value) val tags = (msg.event.twitterHashtag.map(" " + _.handle).toList ++ msg.event.tags.map(t => s" #${t.value.replace(" ", "")}")).mkString("") s"""!!Speaker announcement!! |$name is happening$date$place |Submit your proposals$endDate at ${msg.cfp.publicLink} |#speaking$tags |Thanks $user for the post! |""".stripMargin.trim } }
Example 36
Source File: localdatetime.scala From cats-time with MIT License | 5 votes |
package io.chrisdavenport.cats.time.instances import cats._ import cats.implicits._ import java.time.LocalDateTime import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME trait localdatetime { final def showLocalDateTime(formatter: DateTimeFormatter): Show[LocalDateTime] = Show[String].contramap(_.format(formatter)) implicit final val localdatetimeInstances = new Show[LocalDateTime] with Order[LocalDateTime] with Hash[LocalDateTime]{ override def hash(x: LocalDateTime): Int = x.hashCode override def compare(x: LocalDateTime, y: LocalDateTime): Int = x.compareTo(y) override def show(x: LocalDateTime): String = x.format(ISO_LOCAL_DATE_TIME) } } object localdatetime extends localdatetime
Example 37
Source File: localtime.scala From cats-time with MIT License | 5 votes |
package io.chrisdavenport.cats.time.instances import cats._ import cats.implicits._ import java.time.LocalTime import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter.ISO_LOCAL_TIME trait localtime { final def showLocalTime(formatter: DateTimeFormatter): Show[LocalTime] = Show[String].contramap(_.format(formatter)) implicit final val localtimeInstances = new Show[LocalTime] with Order[LocalTime] with Hash[LocalTime]{ override def hash(x: LocalTime): Int = x.hashCode override def compare(x: LocalTime, y: LocalTime): Int = x.compareTo(y) override def show(x: LocalTime): String = x.format(ISO_LOCAL_TIME) } } object localtime extends localtime
Example 38
Source File: offsetdatetime.scala From cats-time with MIT License | 5 votes |
package io.chrisdavenport.cats.time.instances import cats._ import cats.implicits._ import java.time.OffsetDateTime import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME trait offsetdatetime { final def showOffsetDateTime(formatter: DateTimeFormatter): Show[OffsetDateTime] = Show[String].contramap(_.format(formatter)) implicit final val offsetdatetimeInstances = new Show[OffsetDateTime] with Order[OffsetDateTime] with Hash[OffsetDateTime]{ override def hash(x: OffsetDateTime): Int = x.hashCode override def compare(x: OffsetDateTime, y: OffsetDateTime): Int = x.compareTo(y) override def show(x: OffsetDateTime): String = x.format(ISO_OFFSET_DATE_TIME) } } object offsetdatetime extends offsetdatetime
Example 39
Source File: offsettime.scala From cats-time with MIT License | 5 votes |
package io.chrisdavenport.cats.time.instances import cats._ import cats.implicits._ import java.time.OffsetTime import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter.ISO_OFFSET_TIME trait offsettime { final def showOffsetTime(formatter: DateTimeFormatter): Show[OffsetTime] = Show[String].contramap(_.format(formatter)) implicit final val offsettimeInstances = new Show[OffsetTime] with Order[OffsetTime] with Hash[OffsetTime]{ override def hash(x: OffsetTime): Int = x.hashCode override def compare(x: OffsetTime, y: OffsetTime): Int = x.compareTo(y) override def show(x: OffsetTime): String = x.format(ISO_OFFSET_TIME) } } object offsettime extends offsettime
Example 40
Source File: yearmonth.scala From cats-time with MIT License | 5 votes |
package io.chrisdavenport.cats.time.instances import cats._ import cats.implicits._ import java.time.YearMonth import java.time.format.DateTimeFormatter trait yearmonth { final def showYearMonth(formatter: DateTimeFormatter): Show[YearMonth] = Show[String].contramap(_.format(formatter)) private final val yearMonthFormatter = DateTimeFormatter.ofPattern("yyyy-MM") implicit final val yearmonthInstances = new Show[YearMonth] with Order[YearMonth] with Hash[YearMonth]{ override def hash(x: YearMonth): Int = x.hashCode override def compare(x: YearMonth, y: YearMonth): Int = x.compareTo(y) override def show(x: YearMonth): String = x.format(yearMonthFormatter) } } object yearmonth extends yearmonth
Example 41
Source File: localdate.scala From cats-time with MIT License | 5 votes |
package io.chrisdavenport.cats.time.instances import cats._ import cats.implicits._ import java.time.LocalDate import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter.ISO_LOCAL_DATE trait localdate { final def showLocalDate(formatter: DateTimeFormatter): Show[LocalDate] = Show[String].contramap(_.format(formatter)) implicit final val localdateInstances = new Show[LocalDate] with Order[LocalDate] with Hash[LocalDate]{ override def hash(x: LocalDate): Int = x.hashCode override def compare(x: LocalDate, y: LocalDate): Int = x.compareTo(y) override def show(x: LocalDate): String = x.format(ISO_LOCAL_DATE) } } object localdate extends localdate
Example 42
Source File: zoneddatetime.scala From cats-time with MIT License | 5 votes |
package io.chrisdavenport.cats.time.instances import cats._ import cats.implicits._ import java.time.ZonedDateTime import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME trait zoneddatetime { final def showZonedDateTime(formatter: DateTimeFormatter): Show[ZonedDateTime] = Show[String].contramap(_.format(formatter)) implicit final val zoneddatetimeInstances = new Hash[ZonedDateTime] with Order[ZonedDateTime] with Show[ZonedDateTime]{ override def hash(x: ZonedDateTime): Int = x.hashCode override def compare(x: ZonedDateTime, y: ZonedDateTime): Int = x.compareTo(y) override def show(x: ZonedDateTime): String = x.format(ISO_ZONED_DATE_TIME) } } object zoneddatetime extends zoneddatetime
Example 43
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 44
Source File: PaymentsValidatorSpec.scala From vat-api with Apache License 2.0 | 5 votes |
package v1.controllers.requestParsers.validators import java.time.LocalDate import java.time.format.DateTimeFormatter import support.UnitSpec import v1.models.errors.{FinancialDataInvalidDateFromError, FinancialDataInvalidDateRangeError, FinancialDataInvalidDateToError, VrnFormatError} import v1.models.request.payments.PaymentsRawData class PaymentsValidatorSpec extends UnitSpec { val validator = new PaymentsValidator() private val validVrn = "123456789" private val invalidVrn = "thisIsNotAVrn" private val validFrom = "2019-01-01" private val validTo = "2019-12-31" private val dateTimeFormatter = DateTimeFormatter ofPattern "yyyy-MM-dd" "running a validation" should { "return no errors" when { "a valid request" in { validator.validate(PaymentsRawData(validVrn, Some(validFrom), Some(validTo))) shouldBe Nil } "a 'to' date is today" in { val todayDate = LocalDate.now().format(dateTimeFormatter) val yesterdayDate = LocalDate.now().minusDays(1).format(dateTimeFormatter) validator.validate(PaymentsRawData(validVrn, Some(yesterdayDate), Some(todayDate))) shouldBe List() } "a 'from' date is on the minimum supported date" in { validator.validate(PaymentsRawData(validVrn, Some("2016-04-06"), Some("2016-04-07"))) shouldBe List() } } "return VrnFormatError error" when { "an invalid Vrn is supplied" in { validator.validate(PaymentsRawData(invalidVrn, Some(validFrom), Some(validTo))) shouldBe List(VrnFormatError) } //maintain order of preference to match legacy validation "an invalid Vrn, and invalid dates are supplied" in { validator.validate(PaymentsRawData(invalidVrn, Some("invalidFromDate"), Some("invalidToDate"))) shouldBe List(VrnFormatError) } } "return only FinancialDataInvalidDateFromError error" when { "an invalid from date format is supplied" in { validator.validate(PaymentsRawData(validVrn, Some("12-31-2020"), Some(validTo))) shouldBe List(FinancialDataInvalidDateFromError) } //maintain order of preference to match legacy validation "an invalid from date and invalid to date are supplied" in { validator.validate(PaymentsRawData(validVrn, Some("12-31-2020"), Some("invalidDateTo"))) shouldBe List(FinancialDataInvalidDateFromError) } "a 'from' date is before the minimum supported date" in { validator.validate(PaymentsRawData(validVrn, Some("2016-04-05"), Some("2019-01-01"))) shouldBe List(FinancialDataInvalidDateFromError) } } "return only FinancialDataInvalidDateToError error" when { "an invalid to date format is supplied" in { validator.validate(PaymentsRawData(validVrn, Some(validFrom), Some("12-31-2020"))) shouldBe List(FinancialDataInvalidDateToError) } "a 'to' date is in the future" in { val tomorrowDate = LocalDate.now().plusDays(1).format(dateTimeFormatter) validator.validate(PaymentsRawData(validVrn, Some("2018-01-01"), Some(tomorrowDate))) shouldBe List(FinancialDataInvalidDateToError) } } "return RuleDateRangeError error" when { "invalid date range is supplied" in { validator.validate(PaymentsRawData(validVrn, Some("2018-01-01"), Some("2019-01-01"))) shouldBe List(FinancialDataInvalidDateRangeError) } } } }
Example 45
Source File: JsonToCsv.scala From temperature-machine with Apache License 2.0 | 5 votes |
package bad.robot.temperature import java.lang.Math.abs import java.time.Instant import java.time.ZoneId._ import java.time.format.DateTimeFormatter import java.time.format.FormatStyle._ import java.util.Locale import io.circe.Decoder import scalaz.\/ object JsonToCsv { type Row = (String, Instant, Double) val DefaultTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(SHORT).withLocale(Locale.getDefault).withZone(systemDefault()) def convert(json: => Error \/ String, formatter: DateTimeFormatter): Error \/ String = { for { string <- json series <- decodeAsDisjunction[List[Series]](string) } yield { toCsv(series, formatter) } } private def toCsv(series: List[Series], formatter: DateTimeFormatter) = { val quote = "\"" def toRows: List[Row] = for { reading <- series measurement <- reading.data } yield { (reading.label, measurement.time, measurement.temperature.celsius) } def toCsv(rows: List[Row]): List[String] = { val enquote: String => String = value => s"$quote$value$quote" val heading = List( enquote("Sensor"), enquote("Time"), enquote("Temperature"), enquote("Difference") ).mkString(",") if (rows.isEmpty) return Nil val tuples = rows.map(x => (x._1, x._2, x._3, "0")) val rowsWithPercentageDifference = tuples.drop(1).scan(tuples.head) { case (previous, current) => (current._1, current._2, current._3, f"${abs(current._3 - previous._3)}%.2f") } heading :: rowsWithPercentageDifference.map(row => List( enquote(row._1), enquote(formatter.format(row._2)), enquote(row._3.toString), enquote(row._4.toString) ).mkString(",")) } toCsv(toRows).mkString(sys.props("line.separator")) } } object Series { import io.circe.generic.semiauto._ implicit val dataCodec: Decoder[Series] = deriveDecoder[Series] } case class Series(label: String, data: List[Data]) object Data { import io.circe.generic.semiauto._ implicit val seriesCodec: Decoder[Data] = deriveDecoder[Data] } case class Data(x: Long, y: String) { def time: Instant = Instant.ofEpochMilli(x) def temperature: Temperature = Temperature(y.toDouble) }
Example 46
Source File: ApolloTracingExtension.scala From sangria-slowlog with Apache License 2.0 | 5 votes |
package sangria.slowlog import java.time.Instant import java.time.format.DateTimeFormatter import java.util.concurrent.ConcurrentLinkedQueue import sangria.ast._ import sangria.execution._ import sangria.schema.Context import sangria.marshalling.queryAst._ import sangria.renderer.SchemaRenderer import scala.collection.JavaConverters._ object ApolloTracingExtension extends Middleware[Any] with MiddlewareExtension[Any] with MiddlewareAfterField[Any] with MiddlewareErrorField[Any] { type QueryVal = QueryTrace type FieldVal = Long def beforeQuery(context: MiddlewareQueryContext[Any, _, _]) = QueryTrace(Instant.now(), System.nanoTime(), new ConcurrentLinkedQueue) def afterQuery(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]) = () def beforeField(queryVal: QueryVal, mctx: MiddlewareQueryContext[Any, _, _], ctx: Context[Any, _]) = continue(System.nanoTime()) def afterField(queryVal: QueryVal, fieldVal: FieldVal, value: Any, mctx: MiddlewareQueryContext[Any, _, _], ctx: Context[Any, _]) = { updateMetric(queryVal, fieldVal, ctx) None } def fieldError(queryVal: QueryVal, fieldVal: FieldVal, error: Throwable, mctx: MiddlewareQueryContext[Any, _, _], ctx: Context[Any, _]) = updateMetric(queryVal, fieldVal, ctx) def updateMetric(queryVal: QueryVal, fieldVal: FieldVal, ctx: Context[Any, _]): Unit = queryVal.fieldData.add(ObjectValue( "path" -> ListValue(ctx.path.path.map(queryAstResultMarshaller.scalarNode(_, "Any", Set.empty))), "parentType" -> StringValue(ctx.parentType.name), "fieldName" -> StringValue(ctx.field.name), "returnType" -> StringValue(SchemaRenderer.renderTypeName(ctx.field.fieldType)), "startOffset" -> BigIntValue(fieldVal - queryVal.startNanos), "duration" -> BigIntValue(System.nanoTime() - fieldVal))) def afterQueryExtensions(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]): Vector[Extension[_]] = Vector(Extension(ObjectValue( "tracing" -> ObjectValue( "version" -> IntValue(1), "startTime" -> StringValue(DateTimeFormatter.ISO_INSTANT.format(queryVal.startTime)), "endTime" -> StringValue(DateTimeFormatter.ISO_INSTANT.format(Instant.now())), "duration" -> BigIntValue(System.nanoTime() - queryVal.startNanos), "execution" -> ObjectValue( "resolvers" -> ListValue(queryVal.fieldData.asScala.toVector)))): Value)) case class QueryTrace(startTime: Instant, startNanos: Long, fieldData: ConcurrentLinkedQueue[Value]) }
Example 47
Source File: InstantModule.scala From milan with Apache License 2.0 | 5 votes |
package com.amazon.milan.dataformats import java.time.format.{DateTimeFormatter, DateTimeParseException} import java.time.temporal.{TemporalAccessor, TemporalQuery} import java.time.{Instant, LocalDateTime, ZoneOffset} import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer} class InstantModule extends SimpleModule { this.addDeserializer[Instant](classOf[Instant], new MilanInstantDeserializer) } class MilanInstantDeserializer extends JsonDeserializer[Instant] { private val formatsToTry = List( DateTimeFormatter.ISO_INSTANT, DateTimeFormatter.ISO_DATE_TIME, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"), DateTimeFormatter.ISO_DATE) override def deserialize(parser: JsonParser, context: DeserializationContext): Instant = { val textValue = parser.getText this.parseInstant(textValue) } private val createInstant = new TemporalQuery[Instant] { override def queryFrom(temporal: TemporalAccessor): Instant = LocalDateTime.from(temporal).toInstant(ZoneOffset.UTC) } private def parseInstant(dateTimeString: String): Instant = { // Try a bunch of formats. // TODO: This is awful but will do for now. formatsToTry.map(formatter => this.tryParseFormat(dateTimeString, formatter)) .filter(_.isDefined) .map(_.get) .headOption match { case Some(instant) => instant case None => throw new DateTimeParseException(s"Unable to parse datetime string '$dateTimeString'.", dateTimeString, 0) } } private def tryParseFormat(dateTimeString: String, formatter: DateTimeFormatter): Option[Instant] = { try { Some(formatter.parse(dateTimeString, this.createInstant)) } catch { case _: DateTimeParseException => None } } }
Example 48
Source File: ColumnDateTimeFormats.scala From milan with Apache License 2.0 | 5 votes |
package com.amazon.milan.dataformats import java.time.Instant import java.time.format.DateTimeFormatter import com.fasterxml.jackson.core.JsonGenerator import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.databind.{JsonSerializer, SerializerProvider} class ColumnDateTimeFormatsInstantSerializer(dateTimeFormats: Map[String, DateTimeFormatter]) extends JsonSerializer[Instant] { override def serialize(value: Instant, jsonGenerator: JsonGenerator, serializerProvider: SerializerProvider): Unit = { val contextName = jsonGenerator.getOutputContext.getCurrentName val dateTimeFormatter = this.dateTimeFormats.get(contextName) match { case Some(formatter) => formatter case None => DateTimeFormatter.ISO_INSTANT } jsonGenerator.writeString(dateTimeFormatter.format(value)) } }
Example 49
Source File: TimeParser.scala From airframe with Apache License 2.0 | 5 votes |
package wvlet.airframe.metrics import java.time._ import java.time.format.DateTimeFormatter import wvlet.log.LogSupport import scala.annotation.tailrec import scala.util.{Failure, Success, Try} object TimeParser extends LogSupport { val localDatePattern = DateTimeFormatter.ofPattern("yyyy-MM-dd") val localDateTimePattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSS]") val zonedDateTimePatterns: List[DateTimeFormatter] = List( "yyyy-MM-dd HH:mm:ss[.SSS][ z][XXXXX][XXXX]['['VV']']", "yyyy-MM-dd'T'HH:mm:ss[.SSS][ z][XXXXX][XXXX]['['VV']']" ).map(DateTimeFormatter.ofPattern(_)) case class TimeParseResult(dateTime: ZonedDateTime, unit: TimeWindowUnit) def parseLocalDateTime(s: String, zone: ZoneOffset): Option[TimeParseResult] = { Try(LocalDateTime.parse(s, localDateTimePattern)) .map { d => TimeParseResult(ZonedDateTime.of(d, zone), TimeWindowUnit.Second) } .orElse { Try(LocalDate.parse(s, localDatePattern)) .map { d => TimeParseResult(d.atStartOfDay(zone), TimeWindowUnit.Day) } } .toOption } def parseZonedDateTime(s: String): Option[TimeParseResult] = { @tailrec def loop(lst: List[DateTimeFormatter]): Option[TimeParseResult] = { if (lst.isEmpty) { None } else { val formatter = lst.head Try(ZonedDateTime.parse(s, formatter)) match { case Success(dt) => Some(TimeParseResult(dt, TimeWindowUnit.Second)) case Failure(e) => loop(lst.tail) } } } loop(zonedDateTimePatterns.toList) } def parseAtLocalTimeZone(s: String): Option[ZonedDateTime] = parse(s, systemTimeZone) def parse(s: String, zone: ZoneOffset): Option[ZonedDateTime] = { parseTimeAndUnit(s, zone).map(_.dateTime) } def parseTimeAndUnit(s: String, zone: ZoneOffset): Option[TimeParseResult] = { parseLocalDateTime(s, zone) .orElse(parseZonedDateTime(s)) } }
Example 50
Source File: StringFormats.scala From swagger-check with MIT License | 5 votes |
package de.leanovate.swaggercheck.schema.model.formats import java.net.{URI, URL} import java.time.format.DateTimeFormatter import java.time.temporal.ChronoUnit import java.time.{Instant, LocalDate} import java.util.UUID import de.leanovate.swaggercheck.schema.model.{JsonPath, ValidationResult} import scala.util.Try object StringFormats { object URLString extends ValueFormat[String] { override def validate(path: JsonPath, value: String): ValidationResult = if (Try(new URL(value)).isSuccess) ValidationResult.success else ValidationResult.error(s"'$value' is not an url: $path") } object URIString extends ValueFormat[String] { override def validate(path: JsonPath, value: String): ValidationResult = if (Try(new URI(value)).isSuccess) ValidationResult.success else ValidationResult.error(s"'$value' is not an uri: $path") } object UUIDString extends ValueFormat[String] { override def validate(path: JsonPath, value: String): ValidationResult = if (Try(UUID.fromString(value)).isSuccess) ValidationResult.success else ValidationResult.error(s"'$value' is not an uuid: $path") } object EmailString extends ValueFormat[String] { val emailPattern = """(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])""".r override def validate(path: JsonPath, value: String): ValidationResult = if (emailPattern.pattern.matcher(value).matches()) { ValidationResult.success } else { ValidationResult.error(s"'$value' is not an email: $path") } } object DateString extends ValueFormat[String] { override def validate(path: JsonPath, value: String): ValidationResult = if (Try(DateTimeFormatter.ISO_DATE.parse(value)).isSuccess) ValidationResult.success else ValidationResult.error(s"'$value' is not a date: $path") } object DateTimeString extends ValueFormat[String] { override def validate(path: JsonPath, value: String): ValidationResult = if (Try(DateTimeFormatter.ISO_DATE_TIME.parse(value)).isSuccess) ValidationResult.success else ValidationResult.error(s"'$value' is not a date-time: $path") } val defaultFormats = Map( "url" -> URLString, "uri" -> URIString, "uuid" -> UUIDString, "email" -> EmailString, "date" -> DateString, "date-time" -> DateTimeString ) }
Example 51
Source File: GeneratableStringFormats.scala From swagger-check with MIT License | 5 votes |
package de.leanovate.swaggercheck.schema.gen.formats import java.time.format.DateTimeFormatter import java.time.temporal.ChronoUnit import java.time.{Instant, LocalDate} import de.leanovate.swaggercheck.generators.Generators import de.leanovate.swaggercheck.schema.model.formats.StringFormats import de.leanovate.swaggercheck.schema.model.{JsonPath, ValidationResult} import org.scalacheck.Gen object GeneratableStringFormats { object URLString extends GeneratableFormat[String] { override def generate: Gen[String] = Generators.url override def validate(path: JsonPath, value: String): ValidationResult = StringFormats.URLString.validate(path, value) } object URIString extends GeneratableFormat[String] { override def generate: Gen[String] = Generators.uri override def validate(path: JsonPath, value: String): ValidationResult = StringFormats.URIString.validate(path, value) } object UUIDString extends GeneratableFormat[String] { override def generate: Gen[String] = Gen.uuid.map(_.toString) override def validate(path: JsonPath, value: String): ValidationResult = StringFormats.UUIDString.validate(path, value) } object EmailString extends GeneratableFormat[String] { val emailPattern = """(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])""".r override def generate: Gen[String] = Generators.email override def validate(path: JsonPath, value: String): ValidationResult = StringFormats.EmailString.validate(path, value) } object DateString extends GeneratableFormat[String] { override def generate: Gen[String] = { Gen.choose[Int](-300000, 300000).map { diff: Int => val instant = LocalDate.ofEpochDay(diff) DateTimeFormatter.ISO_DATE.format(instant) } } override def validate(path: JsonPath, value: String): ValidationResult = StringFormats.DateString.validate(path, value) } object DateTimeString extends GeneratableFormat[String] { override def generate: Gen[String] = { Gen.choose[Long](Long.MinValue, Long.MaxValue).map { diff: Long => val instant = Instant.now().plus(diff, ChronoUnit.NANOS) DateTimeFormatter.ISO_INSTANT.format(instant) } } override def validate(path: JsonPath, value: String): ValidationResult = StringFormats.DateTimeString.validate(path, value) } val defaultFormats = Map( "url" -> URLString, "uri" -> URIString, "uuid" -> UUIDString, "email" -> EmailString, "date" -> DateString, "date-time" -> DateTimeString ) }
Example 52
Source File: Sessionize.scala From Mastering-Scala-Machine-Learning with MIT License | 5 votes |
package org.akozlov.chapter06 import java.io._ import java.time.ZoneOffset import java.time.LocalDateTime import java.time.format.DateTimeFormatter import org.apache.spark.{SparkConf,SparkContext} import org.apache.spark.storage.StorageLevel object Sessionize extends App { val sc = new SparkContext("local[8]", "Sessionize", new SparkConf()) val checkoutPattern = ".*>checkout.*".r.pattern // a basic page view structure case class PageView(ts: String, path: String) extends Serializable with Ordered[PageView] { override def toString: String = { s"($ts #$path)" } def compare(other: PageView) = ts compare other.ts } // represent a session case class Session[A <: PageView](id: String, visits: Seq[A]) extends Serializable { override def toString: String = { val vsts = visits.mkString("[", ",", "]") s"($id -> $vsts)" } } def toEpochSeconds(str: String) = { LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toEpochSecond(ZoneOffset.UTC) } val sessions = sc.textFile("data/clickstream") .map(line => {val parts = line.split("\t"); (parts(4), new PageView(parts(0), parts(20)))}) .groupByKey.map(x => { new Session(x._1, x._2.toSeq.sorted) } ) .cache // sessions.take(100).foreach(println) def findAllCheckoutSessions(s: Session[PageView]) = { s.visits.tails.filter { _ match { case PageView(ts1, "mycompanycom>homepage") :: PageView(ts2, page) :: tail if (page != "mycompanycom>homepage" ) => true; case _ => false } } .foldLeft(Seq[Session[PageView]]()) { case (r, x) => { x.find(y => checkoutPattern.matcher(y.path).matches) match { case Some(checkout) if (toEpochSeconds(checkout.ts) > toEpochSeconds(x.head.ts) + 60) => r.:+(new Session(s.id, x.slice(0, x.indexOf(checkout)))) case _ => r } } } } val prodLandingSessions = sessions.flatMap(findAllCheckoutSessions) prodLandingSessions.collect.foreach(println) sc.stop() }
Example 53
Source File: CassandraStorage.scala From graphsense-transformation with MIT License | 5 votes |
package at.ac.ait.storage import com.datastax.spark.connector.rdd.ValidRDDType import com.datastax.spark.connector.rdd.reader.RowReaderFactory import com.datastax.spark.connector.writer.{RowWriterFactory} import java.time.LocalDateTime import java.time.format.DateTimeFormatter import org.apache.spark.sql.{Dataset, Encoder, SparkSession} import scala.reflect.ClassTag import at.ac.ait.Util._ class CassandraStorage(spark: SparkSession) { import spark.implicits._ import com.datastax.spark.connector._ def load[T <: Product: ClassTag: RowReaderFactory: ValidRDDType: Encoder]( keyspace: String, tableName: String, columns: ColumnRef* ) = { spark.sparkContext.setJobDescription(s"Loading table ${tableName}") val table = spark.sparkContext.cassandraTable[T](keyspace, tableName) if (columns.isEmpty) table.toDS().as[T] else table.select(columns: _*).toDS().as[T] } def store[T <: Product: RowWriterFactory]( keyspace: String, tableName: String, df: Dataset[T] ) = { spark.sparkContext.setJobDescription(s"Writing table ${tableName}") val dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val timestamp = LocalDateTime.now().format(dtf) println(s"[$timestamp] Writing table ${tableName}") time { df.rdd.saveToCassandra(keyspace, tableName) } } }
Example 54
Source File: LiabilitiesValidatorSpec.scala From vat-api with Apache License 2.0 | 5 votes |
package v1.controllers.requestParsers.validators import java.time.LocalDate import java.time.format.DateTimeFormatter import support.UnitSpec import v1.models.errors.{FinancialDataInvalidDateFromError, FinancialDataInvalidDateRangeError, FinancialDataInvalidDateToError, VrnFormatError} import v1.models.request.liabilities.LiabilitiesRawData class LiabilitiesValidatorSpec extends UnitSpec { val validator = new LiabilitiesValidator() private val validVrn = "123456789" private val invalidVrn = "thisIsNotAVrn" private val validFrom = "2019-01-01" private val validTo = "2019-12-31" private val dateTimeFormatter = DateTimeFormatter ofPattern "yyyy-MM-dd" "running a validation" should { "return no errors" when { "a valid request" in { validator.validate(LiabilitiesRawData(validVrn, Some(validFrom), Some(validTo))) shouldBe Nil } "a 'to' date is today" in { val todayDate = LocalDate.now().format(dateTimeFormatter) val yesterdayDate = LocalDate.now().minusDays(1).format(dateTimeFormatter) validator.validate(LiabilitiesRawData(validVrn, Some(yesterdayDate), Some(todayDate))) shouldBe List() } "a 'from' date is on the minimum supported date" in { validator.validate(LiabilitiesRawData(validVrn, Some("2016-04-06"), Some("2016-04-07"))) shouldBe List() } } "return VrnFormatError error" when { "an invalid Vrn is supplied" in { validator.validate(LiabilitiesRawData(invalidVrn, Some(validFrom), Some(validTo))) shouldBe List(VrnFormatError) } //maintain order of preference to match legacy validation "an invalid Vrn, and invalid dates are supplied" in { validator.validate(LiabilitiesRawData(invalidVrn, Some("invalidFromDate"), Some("invalidToDate"))) shouldBe List(VrnFormatError) } } "return only FinancialDataInvalidDateFromError error" when { "an invalid from date format is supplied" in { validator.validate(LiabilitiesRawData(validVrn, Some("12-31-2020"), Some(validTo))) shouldBe List(FinancialDataInvalidDateFromError) } //maintain order of preference to match legacy validation "an invalid from date and invalid to date are supplied" in { validator.validate(LiabilitiesRawData(validVrn, Some("12-31-2020"), Some("invalidDateTo"))) shouldBe List(FinancialDataInvalidDateFromError) } "a 'from' date is before the minimum supported date" in { validator.validate(LiabilitiesRawData(validVrn, Some("2016-04-05"), Some("2019-01-01"))) shouldBe List(FinancialDataInvalidDateFromError) } } "return only FinancialDataInvalidDateToError error" when { "an invalid to date format is supplied" in { validator.validate(LiabilitiesRawData(validVrn, Some(validFrom), Some("12-31-2020"))) shouldBe List(FinancialDataInvalidDateToError) } "a 'to' date is in the future" in { val tomorrowDate = LocalDate.now().plusDays(1).format(dateTimeFormatter) validator.validate(LiabilitiesRawData(validVrn, Some("2018-01-01"), Some(tomorrowDate))) shouldBe List(FinancialDataInvalidDateToError) } } "return RuleDateRangeError error" when { "invalid date range is supplied" in { validator.validate(LiabilitiesRawData(validVrn, Some("2018-01-01"), Some("2019-01-01"))) shouldBe List(FinancialDataInvalidDateRangeError) } } } }
Example 55
Source File: ExportEndpoint.scala From temperature-machine with Apache License 2.0 | 5 votes |
package bad.robot.temperature.server import java.time.format.DateTimeFormatter import bad.robot.temperature.{Error, JsonToCsv} import cats.effect.IO import org.http4s.HttpService import org.http4s.MediaType.`text/csv` import org.http4s.dsl.io._ import org.http4s.headers.{`Content-Disposition`, `Content-Type`} import scalaz.\/ object ExportEndpoint { def apply(json: => Error \/ String, formatter: DateTimeFormatter) = HttpService[IO] { case GET -> Root / "temperatures.csv" => { val csv = JsonToCsv.convert(json, formatter) csv.toHttpResponse(Ok(_).map(_.putHeaders( `Content-Type`(`text/csv`), `Content-Disposition`("attachment", Map("filename" -> "temperatures.csv")) ))) } } }
Example 56
Source File: DateTimeFormatterHelper.scala From XSQL with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.catalyst.util import java.time._ import java.time.chrono.IsoChronology import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder, ResolverStyle} import java.time.temporal.{ChronoField, TemporalAccessor, TemporalQueries} import java.util.Locale import com.google.common.cache.CacheBuilder import org.apache.spark.sql.catalyst.util.DateTimeFormatterHelper._ trait DateTimeFormatterHelper { protected def toInstantWithZoneId(temporalAccessor: TemporalAccessor, zoneId: ZoneId): Instant = { val localTime = if (temporalAccessor.query(TemporalQueries.localTime) == null) { LocalTime.ofNanoOfDay(0) } else { LocalTime.from(temporalAccessor) } val localDate = LocalDate.from(temporalAccessor) val localDateTime = LocalDateTime.of(localDate, localTime) val zonedDateTime = ZonedDateTime.of(localDateTime, zoneId) Instant.from(zonedDateTime) } // Gets a formatter from the cache or creates new one. The buildFormatter method can be called // a few times with the same parameters in parallel if the cache does not contain values // associated to those parameters. Since the formatter is immutable, it does not matter. // In this way, synchronised is intentionally omitted in this method to make parallel calls // less synchronised. // The Cache.get method is not used here to avoid creation of additional instances of Callable. protected def getOrCreateFormatter(pattern: String, locale: Locale): DateTimeFormatter = { val key = (pattern, locale) var formatter = cache.getIfPresent(key) if (formatter == null) { formatter = buildFormatter(pattern, locale) cache.put(key, formatter) } formatter } } private object DateTimeFormatterHelper { val cache = CacheBuilder.newBuilder() .maximumSize(128) .build[(String, Locale), DateTimeFormatter]() def buildFormatter(pattern: String, locale: Locale): DateTimeFormatter = { new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendPattern(pattern) .parseDefaulting(ChronoField.ERA, 1) .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1) .parseDefaulting(ChronoField.DAY_OF_MONTH, 1) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .toFormatter(locale) .withChronology(IsoChronology.INSTANCE) .withResolverStyle(ResolverStyle.STRICT) } }
Example 57
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 58
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 59
Source File: ItTestPlugin.scala From matcher with MIT License | 5 votes |
import java.time.LocalDateTime import java.time.format.DateTimeFormatter import sbt.Keys._ import sbt.Tests.Group import sbt._ // Separate projects for integration tests because of IDEA: https://youtrack.jetbrains.com/issue/SCL-14363#focus=streamItem-27-3061842.0-0 object ItTestPlugin extends AutoPlugin { object autoImport extends ItKeys import autoImport._ override def projectSettings: Seq[Def.Setting[_]] = inConfig(Test)( Seq( logDirectory := { val runId = Option(System.getenv("RUN_ID")).getOrElse { val formatter = DateTimeFormatter.ofPattern("MM-dd--HH_mm_ss") formatter.format(LocalDateTime.now()) // git branch? } val r = target.value / "logs" / runId IO.createDirectory(r) r }, // Example: SCALATEST_EXCLUDE_TAGS="package1.Tag1 package2.Tag2 package3.Tag3" testOptions += { val excludeTags = sys.env.get("SCALATEST_EXCLUDE_TAGS").fold(Seq.empty[String])(Seq("-l", _)) val includeTags = sys.env.get("SCALATEST_INCLUDE_TAGS").fold(Seq.empty[String])(Seq("-n", _)) val args = Seq("-fFWD", (logDirectory.value / "summary.log").toString) ++ excludeTags ++ includeTags Tests.Argument(TestFrameworks.ScalaTest, args: _*) }, parallelExecution in Test := true, tags in test += Tags.ForkedTestGroup -> 1, tags in testOnly += Tags.ForkedTestGroup -> 1, tags in testQuick += Tags.ForkedTestGroup -> 1, testGrouping := { // ffs, sbt! // https://github.com/sbt/sbt/issues/3266 val javaHomeValue = javaHome.value val logDirectoryValue = logDirectory.value val envVarsValue = envVars.value val javaOptionsValue = javaOptions.value val resourceDirectoryValue = resourceDirectory.value for { group <- testGrouping.value suite <- group.tests } yield Group( suite.name, Seq(suite), Tests.SubProcess( ForkOptions( javaHome = javaHomeValue, outputStrategy = outputStrategy.value, bootJars = Vector.empty[java.io.File], workingDirectory = Option(baseDirectory.value), runJVMOptions = Vector( s"-Djava.util.logging.config.file=${resourceDirectoryValue / "jul.properties"}", s"-Dlogback.configurationFile=${resourceDirectoryValue / "logback-test.xml"}", "-Dwaves.it.logging.appender=FILE", s"-Dwaves.it.logging.dir=${logDirectoryValue / suite.name.replaceAll("""(\w)\w*\.""", "$1.")}" // foo.bar.Baz -> f.b.Baz ) ++ javaOptionsValue, connectInput = false, envVars = envVarsValue )) ) } )) } trait ItKeys { val logDirectory = taskKey[File]("The directory where logs of integration tests are written") }
Example 60
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 61
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 62
Source File: Time.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.navigator.console.commands import java.time.Instant import java.time.format.DateTimeFormatter import java.util.concurrent.TimeUnit import com.daml.navigator.console._ import com.daml.navigator.store.Store.ReportCurrentTime import com.daml.navigator.time.TimeProviderWithType import akka.pattern.ask import akka.util.Timeout import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration._ import scala.util.Try case object Time extends SimpleCommand { def name: String = "time" def description: String = "Print the ledger effective time" def params: List[Parameter] = List.empty def getTime(state: State): Future[TimeProviderWithType] = { implicit val actorTimeout: Timeout = Timeout(20, TimeUnit.SECONDS) implicit val executionContext: ExecutionContext = state.ec (state.store ? ReportCurrentTime) .mapTo[Try[TimeProviderWithType]] .map(t => t.get) } def formatTime(t: Instant): String = DateTimeFormatter.ISO_INSTANT.format(t) def eval( state: State, args: List[String], set: CommandSet): Either[CommandError, (State, String)] = { for { future <- Try(getTime(state)) ~> "Failed to get time" time <- Try(Await.result(future, 30.seconds)) ~> "Failed to get time" result <- Try(formatTime(time.time.getCurrentTime)) ~> "Failed to format time" } yield { (state, result) } } }
Example 63
Source File: SetTime.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.navigator.console.commands import java.time.Instant import java.time.format.DateTimeFormatter import java.util.concurrent.TimeUnit import com.daml.navigator.console._ import com.daml.navigator.store.Store.AdvanceTime import com.daml.navigator.time.TimeProviderWithType import akka.pattern.ask import akka.util.Timeout import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration._ import scala.util.Try case object SetTime extends SimpleCommand { def name: String = "set_time" def description: String = "Set the (static) ledger effective time" def params: List[Parameter] = List(ParameterString("time", "New (static) ledger effective time")) private def advanceTime(state: State, newTime: Instant): Future[TimeProviderWithType] = { implicit val actorTimeout: Timeout = Timeout(20, TimeUnit.SECONDS) implicit val executionContext: ExecutionContext = state.ec (state.store ? AdvanceTime(newTime)) .mapTo[Try[TimeProviderWithType]] .map(t => t.get) } private def formatTime(t: Instant): String = DateTimeFormatter.ISO_INSTANT.format(t) def eval( state: State, args: List[String], set: CommandSet): Either[CommandError, (State, String)] = { for { arg1 <- args.headOption ~> "Missing <time> argument" newTime <- Try(Instant.parse(arg1)) ~> "Failed to parse time" future <- Try(advanceTime(state, newTime)) ~> "Failed to advance time" confirmedTime <- Try(Await.result(future, 30.seconds)) ~> "Failed to advance time" result <- Try(formatTime(confirmedTime.time.getCurrentTime)) ~> "Failed to format time" } yield { (state, s"New ledger effective time: $result") } } }
Example 64
Source File: Response.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf.engine.trigger import akka.http.scaladsl.model._ import spray.json.DefaultJsonProtocol._ import spray.json._ import java.time.LocalDateTime import java.time.format.DateTimeFormatter // The HTTP service can `complete` using one of these functions to construct a // a response with a JSON object and status code matching the one in the body. object Response { def successResponse[A: JsonWriter](a: A): (StatusCode, JsObject) = { (StatusCodes.OK, resultJsObject(a)) } def errorResponse(status: StatusCode, es: String*): (StatusCode, JsObject) = { (status, errorsJsObject(status, es)) } // These functions are borrowed from the HTTP JSON ledger API but I haven't // factored them out for now as they are fairly small. def errorsJsObject(status: StatusCode, es: Seq[String]): JsObject = { val errors = es.toJson JsObject(statusField(status), ("errors", errors)) } def resultJsObject[A: JsonWriter](a: A): JsObject = { resultJsObject(a.toJson) } def resultJsObject(a: JsValue): JsObject = { JsObject(statusField(StatusCodes.OK), ("result", a)) } def statusField(status: StatusCode): (String, JsNumber) = ("status", JsNumber(status.intValue())) // Trigger status messages have timestamps for which this is the // formatter. object LocalDateTimeJsonFormat extends RootJsonFormat[LocalDateTime] { override def write(dt: LocalDateTime) = JsString(dt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)) override def read(json: JsValue): LocalDateTime = json match { case JsString(s) => LocalDateTime.parse(s, DateTimeFormatter.ISO_LOCAL_DATE_TIME) case _ => throw new DeserializationException("Decode local datetime failed") } } }
Example 65
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 66
Source File: package.scala From renku with Apache License 2.0 | 5 votes |
package ch.renku.acceptancetests.tooling import java.nio.file.Files.createDirectory import java.nio.file.{Path, Paths} import java.time.LocalDateTime.now import java.time.format.DateTimeFormatter import ch.renku.acceptancetests.model.users.UserCredentials package object console { def %%>(command: Command)(implicit workPath: Path, userCredentials: UserCredentials): String = new CommandExecutor(command).safeExecute val rootWorkDirectory: Path = Paths.get("target") def createTempFolder: Path = { val timestampPattern = DateTimeFormatter.ofPattern("yyyy_MM_dd_HHmm_ss") val folder = rootWorkDirectory.toUri resolve (now format timestampPattern) createDirectory(Paths.get(folder)) } implicit class CommandOps(val context: StringContext) { def c(args: Any*): Command = Command { context.parts.zipAll(args, "", "").foldLeft("") { case (command, (part, arg)) => s"$command$part$arg" } } } }
Example 67
Source File: projects.scala From renku with Apache License 2.0 | 5 votes |
package ch.renku.acceptancetests.model import java.net.URL import java.time.LocalDateTime import java.time.format.DateTimeFormatter import ch.renku.acceptancetests.generators.Generators.Implicits._ import ch.renku.acceptancetests.generators.Generators._ import ch.renku.acceptancetests.model.users.UserCredentials import eu.timepit.refined.api.Refined import eu.timepit.refined.collection.NonEmpty object projects { final case class ProjectIdentifier( namespace: String Refined NonEmpty, slug: String Refined NonEmpty ) final case class ProjectDetails( title: String Refined NonEmpty, description: String Refined NonEmpty, readmeTitle: String ) final case class ProjectUrl(value: String) { override lazy val toString: String = value } object ProjectUrl { implicit class ProjectUrlOps(projectUrl: ProjectUrl)(implicit userCredentials: UserCredentials) { import ch.renku.acceptancetests.tooling.UrlEncoder.urlEncode lazy val addGitCredentials: String = { val protocol = new URL(projectUrl.value).getProtocol projectUrl.value .replace( s"$protocol://", s"$protocol://${urlEncode(userCredentials.username.value)}:${urlEncode(userCredentials.password.value)}@" ) } } } object ProjectDetails { def generate: ProjectDetails = { val now = LocalDateTime.now() val desc = prefixParagraph("An automatically generated project for testing: ").generateOne val readmeTitle = s"test${now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmm_ss"))}" ProjectDetails(Refined.unsafeApply(s"test_${now.format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HHmm_ss"))}"), desc, readmeTitle) } def generateHandsOnProject(captureScreenshots: Boolean): ProjectDetails = if (captureScreenshots) { val readmeTitle = "flights tutorial" ProjectDetails(Refined.unsafeApply(readmeTitle), Refined.unsafeApply("A renku tutorial project."), readmeTitle) } else generate implicit class TitleOps(title: String Refined NonEmpty) { lazy val toPathSegment: String = title.value.replace(" ", "-") } } }
Example 68
Source File: BlogPostPage.scala From hepek with Apache License 2.0 | 5 votes |
package ba.sake.hepek.html.statik import java.time.format.DateTimeFormatter import java.time.LocalDate import scalatags.Text.all._ import ba.sake.hepek.core.RelativePath import ba.sake.hepek.utils.StringUtils import ba.sake.stone.Wither trait BlogPostPage extends StaticPage { def blogSettings: BlogSettings = BlogSettings() def categoryPosts: List[BlogPostPage] = List.empty } @Wither final case class BlogSettings( author: Option[String] = None, createDate: Option[LocalDate] = None, sections: List[Section] = List.empty, dateFormat: DateTimeFormatter = BlogSettings.DefaultDateFormat ) { def withDateFormat(df: DateTimeFormatter) = copy(dateFormat = df) def withDateFormat(df: String) = copy(dateFormat = DateTimeFormatter.ofPattern(df)) } object BlogSettings { val DefaultDateFormatPattern = "dd.MM.yyyy" val DefaultDateFormat = DateTimeFormatter.ofPattern(DefaultDateFormatPattern) } def ref(implicit caller: RelativePath): String = if (owner == caller) "#" + id else caller.relTo(owner) + "#" + id }
Example 69
Source File: CustomDateFormatters.scala From m3d-engine with Apache License 2.0 | 5 votes |
package com.adidas.analytics.algo.shared import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder} import java.time.temporal.ChronoField object CustomDateFormatters { val YEAR_WEEK: DateTimeFormatter = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4) .appendValue(ChronoField.ALIGNED_WEEK_OF_YEAR, 2) .parseDefaulting(ChronoField.DAY_OF_WEEK, 1) .toFormatter() val YEAR_WEEK_DAY: DateTimeFormatter = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4) .appendValue(ChronoField.ALIGNED_WEEK_OF_YEAR, 2) .appendValue(ChronoField.DAY_OF_WEEK, 1) .toFormatter() val YEAR_MONTH: DateTimeFormatter = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4) .appendValue(ChronoField.MONTH_OF_YEAR, 2) .parseDefaulting(ChronoField.DAY_OF_MONTH, 1) .toFormatter() }
Example 70
Source File: DoobieMeta.scala From docspell with GNU General Public License v3.0 | 5 votes |
package docspell.store.impl import java.time.format.DateTimeFormatter import java.time.{Instant, LocalDate} import docspell.common._ import docspell.common.syntax.all._ import com.github.eikek.calev.CalEvent import doobie._ import doobie.implicits.legacy.instant._ import doobie.util.log.Success import emil.doobie.EmilDoobieMeta import io.circe.{Decoder, Encoder} trait DoobieMeta extends EmilDoobieMeta { implicit val sqlLogging = LogHandler({ case e @ Success(_, _, _, _) => DoobieMeta.logger.trace("SQL " + e) case e => DoobieMeta.logger.error(s"SQL Failure: $e") }) def jsonMeta[A](implicit d: Decoder[A], e: Encoder[A]): Meta[A] = Meta[String].imap(str => str.parseJsonAs[A].fold(ex => throw ex, identity))(a => e.apply(a).noSpaces ) implicit val metaCollectiveState: Meta[CollectiveState] = Meta[String].imap(CollectiveState.unsafe)(CollectiveState.asString) implicit val metaUserState: Meta[UserState] = Meta[String].imap(UserState.unsafe)(UserState.asString) implicit val metaPassword: Meta[Password] = Meta[String].imap(Password(_))(_.pass) implicit val metaIdent: Meta[Ident] = Meta[String].imap(Ident.unsafe)(_.id) implicit val metaContactKind: Meta[ContactKind] = Meta[String].imap(ContactKind.unsafe)(_.asString) implicit val metaTimestamp: Meta[Timestamp] = Meta[Instant].imap(Timestamp(_))(_.value) implicit val metaJobState: Meta[JobState] = Meta[String].imap(JobState.unsafe)(_.name) implicit val metaDirection: Meta[Direction] = Meta[Boolean].imap(flag => if (flag) Direction.Incoming: Direction else Direction.Outgoing: Direction )(d => Direction.isIncoming(d)) implicit val metaPriority: Meta[Priority] = Meta[Int].imap(Priority.fromInt)(Priority.toInt) implicit val metaLogLevel: Meta[LogLevel] = Meta[String].imap(LogLevel.unsafeString)(_.name) implicit val metaLenientUri: Meta[LenientUri] = Meta[String].imap(LenientUri.unsafe)(_.asString) implicit val metaNodeType: Meta[NodeType] = Meta[String].imap(NodeType.unsafe)(_.name) implicit val metaLocalDate: Meta[LocalDate] = Meta[String].imap(str => LocalDate.parse(str))(_.format(DateTimeFormatter.ISO_DATE)) implicit val metaItemState: Meta[ItemState] = Meta[String].imap(ItemState.unsafe)(_.name) implicit val metNerTag: Meta[NerTag] = Meta[String].imap(NerTag.unsafe)(_.name) implicit val metaNerLabel: Meta[NerLabel] = jsonMeta[NerLabel] implicit val metaNerLabelList: Meta[List[NerLabel]] = jsonMeta[List[NerLabel]] implicit val metaItemProposal: Meta[MetaProposal] = jsonMeta[MetaProposal] implicit val metaItemProposalList: Meta[MetaProposalList] = jsonMeta[MetaProposalList] implicit val metaLanguage: Meta[Language] = Meta[String].imap(Language.unsafe)(_.iso3) implicit val metaCalEvent: Meta[CalEvent] = Meta[String].timap(CalEvent.unsafe)(_.asString) } object DoobieMeta extends DoobieMeta { import org.log4s._ private val logger = getLogger }
Example 71
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 72
Source File: Global.scala From wowchat with GNU General Public License v3.0 | 5 votes |
package wowchat.common import java.time.LocalDateTime import java.time.format.DateTimeFormatter import io.netty.channel.EventLoopGroup import net.dv8tion.jda.core.entities.TextChannel import wowchat.discord.Discord import wowchat.game.GameCommandHandler import scala.collection.mutable object Global { var group: EventLoopGroup = _ var config: WowChatConfig = _ var discord: Discord = _ var game: Option[GameCommandHandler] = None val discordToWow = new mutable.HashMap[String, mutable.Set[WowChannelConfig]] with mutable.MultiMap[String, WowChannelConfig] val wowToDiscord = new mutable.HashMap[(Byte, Option[String]), mutable.Set[(TextChannel, DiscordChannelConfig)]] with mutable.MultiMap[(Byte, Option[String]), (TextChannel, DiscordChannelConfig)] val guildEventsToDiscord = new mutable.HashMap[String, mutable.Set[TextChannel]] with mutable.MultiMap[String, TextChannel] def getTime: String = { LocalDateTime.now.format(DateTimeFormatter.ofPattern("HH:mm:ss")) } }
Example 73
Source File: DiscoveryUtilsSpec.scala From spark-summit-2018 with GNU General Public License v3.0 | 5 votes |
package com.twilio.open.streaming.trend.discovery import java.time.Instant import java.time.format.DateTimeFormatter import com.twilio.open.streaming.trend.discovery.protocol.{CallEvent, Dimensions} import org.scalatest.{FlatSpec, Matchers} class DiscoveryUtilsSpec extends FlatSpec with Matchers { // example using java serialization with case class "DiscoveryUtils" should " serialize and deserialize a CallEvent object" in { val eventTime = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2018-03-08T18:00:00Z")) val loggedTime = eventTime.plusSeconds(34) //eventTime: Long, loggedTime: Long, eventId: String, eventType: String,dimensions: Dimensions, signalingEvent: Option[SignalingEvent] //case class Dimensions(country: Option[String], continent: Option[String], carrier: Option[String],direction: Option[String]) val ce = CallEvent(eventTime.toEpochMilli, loggedTime.toEpochMilli, "uuid1", "signaling", Dimensions( country = Some("us"), continent = Some("na"), carrier = Some("verizon"), direction = Some("inbound") ), None) val ceSer = DiscoveryUtils.serialize(ce) val ceDeser = DiscoveryUtils.deserialize[CallEvent](ceSer) ce.equals(ceDeser) } }
Example 74
Source File: CassandraReadSideSpec.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.scaladsl.persistence.cassandra import java.time.LocalDateTime import java.time.ZoneOffset import java.time.format.DateTimeFormatter import akka.persistence.query.TimeBasedUUID import scala.concurrent.Future import scala.concurrent.duration._ import com.typesafe.config.ConfigFactory import com.lightbend.lagom.internal.persistence.ReadSideConfig import com.lightbend.lagom.internal.persistence.cassandra.CassandraReadSideSettings import com.lightbend.lagom.internal.scaladsl.persistence.cassandra.CassandraPersistentEntityRegistry import com.lightbend.lagom.internal.scaladsl.persistence.cassandra.CassandraReadSideImpl import com.lightbend.lagom.internal.scaladsl.persistence.cassandra.ScaladslCassandraOffsetStore import com.lightbend.lagom.scaladsl.persistence.TestEntity.Evt import com.lightbend.lagom.scaladsl.persistence._ object CassandraReadSideSpec { def firstTimeBucket: String = { val today = LocalDateTime.now(ZoneOffset.UTC) val firstBucketFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HH:mm") today.minusHours(3).format(firstBucketFormat) } val readSideConfig = ConfigFactory.parseString(s""" # speed up read-side queries cassandra-query-journal { first-time-bucket = "$firstTimeBucket" refresh-interval = 1s events-by-tag.eventual-consistency-delay = 1s } """) val defaultConfig = ConfigFactory .parseString("akka.loglevel = INFO") .withFallback(readSideConfig) val noAutoCreateConfig = ConfigFactory .parseString("lagom.persistence.read-side.cassandra.tables-autocreate = false") .withFallback(defaultConfig) } class CassandraReadSideSpec extends CassandraPersistenceSpec(CassandraReadSideSpec.defaultConfig, TestEntitySerializerRegistry) with AbstractReadSideSpec { import system.dispatcher protected override lazy val persistentEntityRegistry = new CassandraPersistentEntityRegistry(system) private lazy val testCasReadSideSettings: CassandraReadSideSettings = new CassandraReadSideSettings(system) private lazy val testSession: CassandraSession = new CassandraSession(system) private lazy val offsetStore = new ScaladslCassandraOffsetStore(system, testSession, testCasReadSideSettings, ReadSideConfig()) private lazy val cassandraReadSide = new CassandraReadSideImpl(system, testSession, offsetStore) override def processorFactory(): ReadSideProcessor[Evt] = new TestEntityReadSide.TestEntityReadSideProcessor(system, cassandraReadSide, testSession) private lazy val readSide = new TestEntityReadSide(system, testSession) override def getAppendCount(id: String): Future[Long] = readSide.getAppendCount(id) override def afterAll(): Unit = { super.afterAll() } } class CassandraReadSideAutoCreateSpec extends CassandraPersistenceSpec(CassandraReadSideSpec.noAutoCreateConfig, TestEntitySerializerRegistry) { import system.dispatcher private lazy val testSession: CassandraSession = new CassandraSession(system) private lazy val testCasReadSideSettings: CassandraReadSideSettings = new CassandraReadSideSettings(system) private lazy val offsetStore = new ScaladslCassandraOffsetStore(system, testSession, testCasReadSideSettings, ReadSideConfig()) "A Cassandra Read-Side" must { "not send ClusterStartupTask message, so startupTask must return None" + "when 'lagom.persistence.read-side.cassandra.tables-autocreate' flag is 'false'" in { offsetStore.startupTask shouldBe None } } }
Example 75
Source File: GenerateLogFile.scala From 006877 with MIT License | 5 votes |
package aia.stream import java.nio.file.{ Path, Paths } import java.nio.file.StandardOpenOption import java.nio.file.StandardOpenOption._ import java.time.ZonedDateTime import java.time.format.DateTimeFormatter import scala.concurrent.Future import akka.actor.ActorSystem import akka.stream.{ ActorMaterializer, IOResult } import akka.stream.scaladsl._ import akka.util.ByteString object GenerateLogFile extends App { val filePath = args(0) val numberOfLines = args(1).toInt val rnd = new java.util.Random() val sink = FileIO.toPath(FileArg.shellExpanded(filePath), Set(CREATE, WRITE, APPEND)) def line(i: Int) = { val host = "my-host" val service = "my-service" val time = ZonedDateTime.now.format(DateTimeFormatter.ISO_INSTANT) val state = if( i % 10 == 0) "warning" else if(i % 101 == 0) "error" else if(i % 1002 == 0) "critical" else "ok" val description = "Some description of what has happened." val tag = "tag" val metric = rnd.nextDouble() * 100 s"$host | $service | $state | $time | $description | $tag | $metric \n" } val graph = Source.fromIterator{() => Iterator.tabulate(numberOfLines)(line) }.map(l=> ByteString(l)).toMat(sink)(Keep.right) implicit val system = ActorSystem() implicit val ec = system.dispatcher implicit val materializer = ActorMaterializer() graph.run().foreach { result => println(s"Wrote ${result.count} bytes to '$filePath'.") system.terminate() } }
Example 76
Source File: EventMarshalling.scala From 006877 with MIT License | 5 votes |
package aia.stream import java.time.ZonedDateTime import java.time.format.{ DateTimeFormatter, DateTimeParseException } import scala.util.Try import spray.json._ trait EventMarshalling extends DefaultJsonProtocol { implicit val dateTimeFormat = new JsonFormat[ZonedDateTime] { def write(dateTime: ZonedDateTime) = JsString(dateTime.format(DateTimeFormatter.ISO_INSTANT)) def read(value: JsValue) = value match { case JsString(str) => try { ZonedDateTime.parse(str) } catch { case e: DateTimeParseException => val msg = s"Could not deserialize $str to ZonedDateTime" deserializationError(msg) } case js => val msg = s"Could not deserialize $js to ZonedDateTime." deserializationError(msg) } } implicit val stateFormat = new JsonFormat[State] { def write(state: State) = JsString(State.norm(state)) def read(value: JsValue) = value match { case JsString("ok") => Ok case JsString("warning") => Warning case JsString("error") => Error case JsString("critical") => Critical case js => val msg = s"Could not deserialize $js to State." deserializationError(msg) } } implicit val eventFormat = jsonFormat7(Event) implicit val logIdFormat = jsonFormat2(LogReceipt) implicit val errorFormat = jsonFormat2(ParseError) }
Example 77
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 78
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 79
Source File: JsonSnippets.scala From scala-stellar-sdk with Apache License 2.0 | 5 votes |
package stellar.sdk.model.op import java.time.format.DateTimeFormatter import java.util.Locale import stellar.sdk.model.{Amount, Asset, NonNativeAsset} trait JsonSnippets { val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'") def amountString(a: Amount): String = "%.7f".formatLocal(Locale.ROOT, a.units / math.pow(10, 7)) def amountDocPortion(amount: Amount, label: String = "amount", assetPrefix: String = ""): String = { s""" |"$label":${amountString(amount)} |${asset(amount.asset, assetPrefix)} """.stripMargin } def asset(a: Asset, assetPrefix: String = ""): String = a match { case nn: NonNativeAsset => s""" |"${assetPrefix}asset_type": "${nn.typeString}", |"${assetPrefix}asset_code": "${nn.code}", |"${assetPrefix}asset_issuer": "${nn.issuer.accountId}" """.stripMargin.trim case _ => s""" |"${assetPrefix}asset_type": "native" """.stripMargin.trim } def opt(key: String, value: Option[Any]) = value.map { case v: String => s""""$key":"$v",""" case v: Set[_] => s""""$key":[${ v.map { case s: String => s""""$s"""" case a => a }.mkString(",") }],""" case v => s""""$key":$v,""" }.getOrElse("") }
Example 80
Source File: BoxPathTest.scala From fs2-blobstore with Apache License 2.0 | 5 votes |
package blobstore.box import java.time.ZoneOffset import java.time.format.DateTimeFormatter import blobstore.AbstractPathTest import com.box.sdk.BoxFile import cats.syntax.either._ class BoxPathTest extends AbstractPathTest[BoxPath] { @SuppressWarnings(Array("scalafix:DisableSyntax.null")) val file = new BoxFile(null, "") val dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX") val json = s"""{ | "id": "fileId", | "type": "file", | "name": "$fileName", | "size": $fileSize, | "modified_at": "${dtf.format(lastModified.atZone(ZoneOffset.UTC))}", | "path_collection": { | "total_count": 2, | "entries": [ | { | "id": "rootId", | "name": "$root" | }, | { | "id": "folderId", | "name": "$dir" | } | ] | } |}""".stripMargin val fileInfo: BoxFile#Info = new file.Info(json) val concretePath = new BoxPath(Some(root), Some("rootId"), fileInfo.asLeft) }
Example 81
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 82
Source File: ScanObjectsModel.scala From project-matt with MIT License | 5 votes |
package org.datafy.aws.app.matt.models import java.time.ZonedDateTime import java.time.format.DateTimeFormatter import org.datafy.aws.app.matt.extras.{ElasticWrapper, RedisWrapper, S3KeySummary} import org.slf4j.LoggerFactory import io.circe._ import io.circe.generic.semiauto._ import io.circe.generic.auto._ import io.circe.syntax._ case class RiskStats(piiColumn: String, value: Int) case class ObjectScanStats ( s3Key: String, objectSummaryStats: List[RiskStats], classifier: String = "", scannedDate: Long = System.currentTimeMillis() ) case class FullScanStats ( s3Bucket: String, lastScannedKey: String, summaryStats: List[RiskStats], objectScanStats: List[ObjectScanStats], classifier: String = "", scannedDate: Long = System.currentTimeMillis(), totalObjectsSize: Option[Int] = None ) object ScanObjectsModel { val logger = LoggerFactory.getLogger("ScanObjectsModel") def saveScannedResults(scanStats: FullScanStats) = { logger.info("attempting some saving here") val response = ElasticWrapper.saveDocument(scanStats) logger.info("Payload saved: some saving here") response } def getLastScannedFromRedis(key: String) = { val lastScannedKey = RedisWrapper.getData(key) lastScannedKey } def saveLastScannedToRedis(key: String, s3ObjectSummary: List[S3KeySummary]) = { RedisWrapper.setData(key, s3ObjectSummary.last.key) logger.info(s"Cached last scanned key: ${s3ObjectSummary.last.key} to Redis Server") s3ObjectSummary.last.key } }
Example 83
Source File: ApiValueImplicits.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf.value.json import java.time.{Instant, LocalDate} import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder} import com.daml.lf.data.Time import com.daml.lf.value.{Value => V} object ApiValueImplicits { implicit final class `ApiTimestamp additions`(private val it: V.ValueTimestamp) extends AnyVal { import it._ def toInstant: Instant = value.toInstant def toIso8601: String = DateTimeFormatter.ISO_INSTANT.format(toInstant) } implicit final class `ApiDate additions`(private val it: V.ValueDate) extends AnyVal { import it._ def toLocalDate: LocalDate = LocalDate.ofEpochDay((value.days: Int).toLong) def toInstant: Instant = Instant.from(toLocalDate) def toIso8601: String = DateTimeFormatter.ISO_LOCAL_DATE.format(toLocalDate) } // Timestamp has microsecond resolution private val formatter: DateTimeFormatter = new DateTimeFormatterBuilder().appendInstant(6).toFormatter() implicit final class `ApiTimestamp.type additions`(private val it: V.ValueTimestamp.type) extends AnyVal { def fromIso8601(t: String): V.ValueTimestamp = fromInstant(Instant.parse(t)) def fromInstant(t: Instant): V.ValueTimestamp = V.ValueTimestamp(Time.Timestamp.assertFromInstant(t)) def fromMillis(t: Long): V.ValueTimestamp = V.ValueTimestamp(Time.Timestamp.assertFromLong(micros = t * 1000L)) } implicit final class `ApiDate.type additions`(private val it: V.ValueDate.type) extends AnyVal { def fromIso8601(t: String): V.ValueDate = fromLocalDate(LocalDate.parse(t, DateTimeFormatter.ISO_LOCAL_DATE)) def fromLocalDate(t: LocalDate): V.ValueDate = V.ValueDate(Time.Date.assertFromDaysSinceEpoch(t.toEpochDay.toInt)) } }
Example 84
Source File: RollingFileLogger.scala From odin with Apache License 2.0 | 4 votes |
package io.odin.loggers import java.nio.file.{Files, Path, Paths} import java.time.{Instant, LocalDateTime} import java.time.format.DateTimeFormatter import java.util.TimeZone import java.util.concurrent.TimeUnit import cats.Monad import cats.effect.concurrent.Ref import cats.effect.{Concurrent, ContextShift, Fiber, Resource, Timer} import cats.syntax.all._ import io.odin.formatter.Formatter import io.odin.{Level, Logger, LoggerMessage} import scala.concurrent.duration.{FiniteDuration, _} object RollingFileLogger { def apply[F[_]]( fileNamePattern: LocalDateTime => String, maxFileSizeInBytes: Option[Long], rolloverInterval: Option[FiniteDuration], formatter: Formatter, minLevel: Level )(implicit F: Concurrent[F], timer: Timer[F], cs: ContextShift[F]): Resource[F, Logger[F]] = { new RollingFileLoggerFactory( fileNamePattern, maxFileSizeInBytes, rolloverInterval, formatter, minLevel, FileLogger.apply[F] ).mk } private[odin] class RefLogger[F[_]: Timer: Monad]( current: Ref[F, Logger[F]], override val minLevel: Level ) extends DefaultLogger[F](minLevel) { def log(msg: LoggerMessage): F[Unit] = current.get.flatMap(_.log(msg)) override def log(msgs: List[LoggerMessage]): F[Unit] = current.get.flatMap(_.log(msgs)) } private[odin] class RollingFileLoggerFactory[F[_]]( fileNamePattern: LocalDateTime => String, maxFileSizeInBytes: Option[Long], rolloverInterval: Option[FiniteDuration], formatter: Formatter, minLevel: Level, underlyingLogger: (String, Formatter, Level) => Resource[F, Logger[F]], fileSizeCheck: Path => Long = Files.size )(implicit F: Concurrent[F], timer: Timer[F], cs: ContextShift[F]) { val df: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss") def mk: Resource[F, Logger[F]] = { val logger = for { ((logger, watcherFiber), release) <- allocate.allocated refLogger <- Ref.of(logger) refRelease <- Ref.of(release) _ <- F.start(rollingLoop(watcherFiber, refLogger, refRelease)) } yield { (new RefLogger(refLogger, minLevel), refRelease) } Resource.make(logger)(_._2.get.flatten).map { case (logger, _) => logger } } def now: F[Long] = timer.clock.realTime(TimeUnit.MILLISECONDS) def rollingLoop(watcher: Fiber[F, Unit], logger: Ref[F, Logger[F]], release: Ref[F, F[Unit]]): F[Unit] = for { _ <- watcher.join oldRelease <- release.get ((newLogger, newWatcher), newRelease) <- allocate.allocated _ <- logger.set(newLogger) _ <- release.set(newRelease) _ <- oldRelease _ <- rollingLoop(newWatcher, logger, release) } yield () } }