java.time.temporal.ChronoField Scala Examples
The following examples show how to use java.time.temporal.ChronoField.
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: IsoEraTest.scala From scala-js-java-time with BSD 3-Clause "New" or "Revised" License | 5 votes |
package org.scalajs.testsuite.javalib.time.chrono import java.time.DateTimeException import java.time.chrono.IsoEra import java.time.temporal.ChronoField import org.junit.Test import org.junit.Assert._ import org.scalajs.testsuite.javalib.time.TemporalAccessorTest import org.scalajs.testsuite.utils.AssertThrows._ class IsoEraTest extends TemporalAccessorTest[IsoEra] { import IsoEra._ val samples = values.toSeq def isSupported(field: ChronoField): Boolean = field == ChronoField.ERA @Test def test_getValue(): Unit = { assertEquals(0, BCE.getValue) assertEquals(1, CE.getValue) } @Test def test_getLong(): Unit = { for (era <- samples) assertEquals(era.getValue.toLong, era.getLong(ChronoField.ERA)) } @Test def test_compareTo(): Unit = { assertEquals(0, BCE.compareTo(BCE)) assertTrue(BCE.compareTo(CE) < 0) assertTrue(CE.compareTo(BCE) > 0) assertEquals(0, CE.compareTo(CE)) } @Test def test_values(): Unit = { val eras = Array[AnyRef](BCE, CE) assertArrayEquals(eras, values.asInstanceOf[Array[AnyRef]]) } @Test def test_valueOf(): Unit = { assertEquals(BCE, valueOf("BCE")) assertEquals(CE, valueOf("CE")) expectThrows(classOf[IllegalArgumentException], valueOf("")) } @Test def test_of(): Unit = { assertEquals(BCE, of(0)) assertEquals(CE, of(1)) for (n <- Seq(Int.MinValue, -1, 2, Int.MaxValue)) expectThrows(classOf[DateTimeException], of(n)) } }
Example 2
Source File: MomentDateTime.scala From dtc with Apache License 2.0 | 5 votes |
package dtc.js import java.time.temporal.ChronoField import java.time.{DayOfWeek, Duration, LocalDate, LocalTime} import dtc._ import moment.{Date, Moment, Units} import scala.scalajs.js.Array def underlyingMoment: Date = copy def dayOfWeek: DayOfWeek = DayOfWeek.of(dayOfWeekJSToJVM(underlying.day())) def dayOfMonth: Int = underlying.date() def month: Int = underlying.month() + 1 def year: Int = underlying.year() def hour: Int = underlying.hour() def minute: Int = underlying.minute() def second: Int = underlying.second() def millisecond: Int = underlying.millisecond() def withYear(year: Int): T = updated(_.year(year.toDouble)) def withMonth(month: Int): T = updated(_.month(month.toDouble - 1)) def withDayOfMonth(dayOfMonth: Int): T = updated(_.date(dayOfMonth.toDouble)) def withHour(hour: Int): T = updated(_.hour(hour.toDouble)) def withMinute(minute: Int): T = updated(_.minute(minute.toDouble)) def withSecond(second: Int): T = updated(_.second(second.toDouble)) def withMillisecond(millisecond: Int): T = updated(_.millisecond(millisecond.toDouble)) def withTime(time: LocalTime): T = updated(_ .hour(time.getHour.toDouble) .minute(time.getMinute.toDouble) .second(time.getSecond.toDouble) .millisecond(time.get(ChronoField.MILLI_OF_SECOND).toDouble) ) def withDate(date: LocalDate): T = updated(_ .year(date.getYear.toDouble) .month(date.getMonthValue.toDouble - 1) .date(date.getDayOfMonth.toDouble) ) def toLocalDate: LocalDate = LocalDate.of(year, month, dayOfMonth) def toLocalTime: LocalTime = LocalTime.of(hour, minute, second, millisToNanos(millisecond)) def yearsUntil(other: T): Long = other.underlying.diff(underlying, Units.Year).toLong def monthsUntil(other: T): Long = other.underlying.diff(underlying, Units.Month).toLong def daysUntil(other: T): Long = other.underlying.diff(underlying, Units.Day).toLong def millisecondsUntil(other: T): Long = other.underlying.diff(underlying, Units.Millisecond).toLong def secondsUntil(other: T): Long = other.underlying.diff(underlying, Units.Second).toLong def minutesUntil(other: T): Long = other.underlying.diff(underlying, Units.Minute).toLong def hoursUntil(other: T): Long = other.underlying.diff(underlying, Units.Hour).toLong def plus(d: Duration): T = plusMillis(d.toMillis) def minus(d: Duration): T = plusMillis(-d.toMillis) def plusDays(n: Int): T = updated(_.add(n.toDouble, Units.Day)) def plusMonths(n: Int): T = updated(_.add(n.toDouble, Units.Month)) def plusYears(n: Int): T = updated(_.add(n.toDouble, Units.Year)) def plusMillis(n: Long): T def format(formatString: String): String = underlying.format(formatString) override def toString: String = underlying.toString } object MomentDateTime { def compare[T <: MomentDateTime[T]](x: T, y: T): Int = Ordering.Double.compare(x.underlying.value(), y.underlying.value()) private[js] def constructorArray(date: LocalDate, time: LocalTime): Array[Int] = Array( date.getYear, date.getMonthValue - 1, date.getDayOfMonth, time.getHour, time.getMinute, time.getSecond, time.get(ChronoField.MILLI_OF_SECOND) ) private[js] def utcMoment(date: LocalDate, time: LocalTime): Date = Moment.utc(constructorArray(date, time)) }
Example 3
Source File: LocalDateTimeLaws.scala From dtc with Apache License 2.0 | 5 votes |
package dtc.laws import java.time.temporal.{ChronoField, ChronoUnit} import java.time.{LocalDate, LocalTime} import cats.kernel.laws._ import cats.kernel.laws.discipline.{catsLawsIsEqToProp => p} import dtc.Local import dtc.syntax.local._ import org.scalacheck.{Gen, Prop} import org.scalacheck.Prop.forAll trait LocalDateTimeLaws[A] { implicit def D: Local[A] val genLocalDate: Gen[LocalDate] val genLocalTime: Gen[LocalTime] def constructorConsistency: Prop = forAll(genLocalDate, genLocalTime) { (date: LocalDate, time: LocalTime) => val dt = D.of(date, time) p(dt.date <-> date) && (dt.time <-> time.truncatedTo(ChronoUnit.MILLIS)) } def plainConstructorConsistency: Prop = forAll(genLocalDate, genLocalTime) { (date: LocalDate, time: LocalTime) => val dt = D.of( date.getYear, date.getMonthValue, date.getDayOfMonth, time.getHour, time.getMinute, time.getSecond, time.get(ChronoField.MILLI_OF_SECOND)) p(dt.date <-> date) && (dt.time <-> time.truncatedTo(ChronoUnit.MILLIS)) } } object LocalDateTimeLaws { def apply[A]( gLocalTime: Gen[LocalTime], gLocalDate: Gen[LocalDate])( implicit ev: Local[A]): LocalDateTimeLaws[A] = new LocalDateTimeLaws[A] { def D: Local[A] = ev val genLocalDate: Gen[LocalDate] = gLocalDate val genLocalTime: Gen[LocalTime] = gLocalTime } }
Example 4
Source File: DTCSuite.scala From dtc with Apache License 2.0 | 5 votes |
package dtc.tests import java.time.temporal.ChronoField import java.time.temporal.ChronoUnit._ import java.time.{Duration, LocalDate, LocalTime} import dtc.TimeZoneId import org.scalacheck.{Arbitrary, Gen} import org.scalatest.funspec.AnyFunSpecLike import org.scalatest.matchers.should.Matchers import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks import org.typelevel.discipline.scalatest.FunSpecDiscipline trait DTCSuite extends AnyFunSpecLike with Matchers with ScalaCheckDrivenPropertyChecks with FunSpecDiscipline { override implicit val generatorDrivenConfig: PropertyCheckConfiguration = PropertyCheckConfiguration( minSuccessful = 100 ) private val nanoOfDayRange = ChronoField.NANO_OF_DAY.range() val genLocalTime: Gen[LocalTime] = Gen.choose(nanoOfDayRange.getMinimum, nanoOfDayRange.getMaximum).map(LocalTime.ofNanoOfDay) implicit val arbLocalTime: Arbitrary[LocalTime] = Arbitrary(genLocalTime) val genDuration: Gen[Duration] = Gen.choose(Long.MinValue / 1000, Long.MaxValue / 1000) .map(l => Duration.of(l, MILLIS)) implicit val arbDuration = Arbitrary(genDuration) def genDateTimeFromSameOffsetPeriod(period: SameZoneOffsetPeriod): Gen[(LocalDate, LocalTime, TimeZoneId)] = for { date <- Gen.choose(period.startDate.toEpochDay + 1L, period.endDate.toEpochDay - 1L).map(LocalDate.ofEpochDay) timeBounds <- Gen.const( if (date == period.startDate && date == period.endDate) (period.startTime, period.endTime) else if (date == period.startDate) (period.startTime, LocalTime.MAX) else if (date == period.endDate) (LocalTime.MAX, period.endTime) else (LocalTime.MIN, LocalTime.MAX) ) time <- Gen.choose(timeBounds._1.toNanoOfDay, timeBounds._2.toNanoOfDay).map(LocalTime.ofNanoOfDay) } yield (date, time, period.zone) }
Example 5
Source File: ZonedDateTimeInstanceWithoutOrder.scala From dtc with Apache License 2.0 | 5 votes |
package dtc.instances import java.time._ import java.time.temporal.{ChronoField, ChronoUnit} import dtc.{Offset, TimeZoneId, Zoned, millisToNanos, truncateToMillis} import dtc.syntax.timeZone._ trait ZonedDateTimeInstanceWithoutOrder extends Zoned[ZonedDateTime] { self => def capture(date: LocalDate, time: LocalTime, zone: TimeZoneId): ZonedDateTime = ZonedDateTime.of(date, time.truncatedTo(ChronoUnit.MILLIS), zone.zoneId) def withZoneSameInstant(x: ZonedDateTime, zone: TimeZoneId): ZonedDateTime = x.withZoneSameInstant(zone.zoneId) def withZoneSameLocal(x: ZonedDateTime, zone: TimeZoneId): ZonedDateTime = x.withZoneSameLocal(zone.zoneId) def zone(x: ZonedDateTime): TimeZoneId = TimeZoneId(x.getZone.getId) def date(x: ZonedDateTime): LocalDate = x.toLocalDate def time(x: ZonedDateTime): LocalTime = x.toLocalTime.truncatedTo(ChronoUnit.MILLIS) def plus(x: ZonedDateTime, d: Duration): ZonedDateTime = x.plus(truncateToMillis(d)) def minus(x: ZonedDateTime, d: Duration): ZonedDateTime = x.minus(truncateToMillis(d)) def plusDays(x: ZonedDateTime, days: Int): ZonedDateTime = x.plusDays(days.toLong) def plusMonths(x: ZonedDateTime, months: Int): ZonedDateTime = x.plusMonths(months.toLong) def plusYears(x: ZonedDateTime, years: Int): ZonedDateTime = x.plusYears(years.toLong) def withYear(x: ZonedDateTime, year: Int): ZonedDateTime = x.withYear(year) def withMonth(x: ZonedDateTime, month: Int): ZonedDateTime = x.withMonth(month) def withDayOfMonth(x: ZonedDateTime, dayOfMonth: Int): ZonedDateTime = x.withDayOfMonth(dayOfMonth) def withHour(x: ZonedDateTime, hour: Int): ZonedDateTime = x.withHour(hour) def withMinute(x: ZonedDateTime, minute: Int): ZonedDateTime = x.withMinute(minute) def withSecond(x: ZonedDateTime, second: Int): ZonedDateTime = x.withSecond(second) def withMillisecond(x: ZonedDateTime, millisecond: Int): ZonedDateTime = x.withNano(millisToNanos(millisecond)) def withTime(x: ZonedDateTime, time: LocalTime): ZonedDateTime = ZonedDateTime.of(date(x), time, zone(x).zoneId) def withDate(x: ZonedDateTime, date: LocalDate): ZonedDateTime = ZonedDateTime.of(date, time(x), zone(x).zoneId) def offset(x: ZonedDateTime): Offset = Offset(x.getOffset.getTotalSeconds) def dayOfWeek(x: ZonedDateTime): DayOfWeek = x.getDayOfWeek def dayOfMonth(x: ZonedDateTime): Int = x.getDayOfMonth def month(x: ZonedDateTime): Int = x.getMonthValue def year(x: ZonedDateTime): Int = x.getYear def millisecond(x: ZonedDateTime): Int = x.get(ChronoField.MILLI_OF_SECOND) def second(x: ZonedDateTime): Int = x.getSecond def minute(x: ZonedDateTime): Int = x.getMinute def hour(x: ZonedDateTime): Int = x.getHour def yearsUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.YEARS) def monthsUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.MONTHS) def daysUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.DAYS) def hoursUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.HOURS) def minutesUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.MINUTES) def secondsUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.SECONDS) def millisecondsUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.MILLIS) def utc(x: ZonedDateTime): (LocalDate, LocalTime) = { val utcTime = x.withZoneSameInstant(ZoneOffset.UTC) utcTime.toLocalDate -> utcTime.toLocalTime } }
Example 6
Source File: localDateTime.scala From dtc with Apache License 2.0 | 5 votes |
package dtc.instances import java.time.temporal.{ChronoField, ChronoUnit} import java.time._ import dtc._ object localDateTime { implicit val localDateTimeDTC: Local[LocalDateTime] = new Local[LocalDateTime] { def compare(x: LocalDateTime, y: LocalDateTime): Int = x.compareTo(y) def date(x: LocalDateTime): LocalDate = x.toLocalDate def time(x: LocalDateTime): LocalTime = x.toLocalTime.truncatedTo(ChronoUnit.MILLIS) def of(date: LocalDate, time: LocalTime): LocalDateTime = LocalDateTime.of(date, time.truncatedTo(ChronoUnit.MILLIS)) def of( year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int, millisecond: Int): LocalDateTime = LocalDateTime.of(year, month, day, hour, minute, second, millisToNanos(millisecond)) def plus(x: LocalDateTime, d: Duration): LocalDateTime = x.plus(truncateToMillis(d)) def minus(x: LocalDateTime, d: Duration): LocalDateTime = x.minus(truncateToMillis(d)) def plusDays(x: LocalDateTime, days: Int): LocalDateTime = x.plusDays(days.toLong) def plusMonths(x: LocalDateTime, months: Int): LocalDateTime = x.plusMonths(months.toLong) def plusYears(x: LocalDateTime, years: Int): LocalDateTime = x.plusYears(years.toLong) def withYear(x: LocalDateTime, year: Int): LocalDateTime = x.withYear(year) def withMonth(x: LocalDateTime, month: Int): LocalDateTime = x.withMonth(month) def withDayOfMonth(x: LocalDateTime, dayOfMonth: Int): LocalDateTime = x.withDayOfMonth(dayOfMonth) def withHour(x: LocalDateTime, hour: Int): LocalDateTime = x.withHour(hour) def withMinute(x: LocalDateTime, minute: Int): LocalDateTime = x.withMinute(minute) def withSecond(x: LocalDateTime, second: Int): LocalDateTime = x.withSecond(second) def withMillisecond(x: LocalDateTime, millisecond: Int): LocalDateTime = x.withNano(millisToNanos(millisecond)) def withTime(x: LocalDateTime, time: LocalTime): LocalDateTime = LocalDateTime.of(date(x), time) def withDate(x: LocalDateTime, date: LocalDate): LocalDateTime = LocalDateTime.of(date, time(x)) def dayOfWeek(x: LocalDateTime): DayOfWeek = x.getDayOfWeek def dayOfMonth(x: LocalDateTime): Int = x.getDayOfMonth def month(x: LocalDateTime): Int = x.getMonthValue def year(x: LocalDateTime): Int = x.getYear def millisecond(x: LocalDateTime): Int = x.get(ChronoField.MILLI_OF_SECOND) def second(x: LocalDateTime): Int = x.getSecond def minute(x: LocalDateTime): Int = x.getMinute def hour(x: LocalDateTime): Int = x.getHour def yearsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.YEARS) def monthsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.MONTHS) def daysUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.DAYS) def hoursUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.HOURS) def minutesUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.MINUTES) def secondsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.SECONDS) def millisecondsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.MILLIS) } implicit val captureLocalDateTime: Capture[LocalDateTime] = new Capture[LocalDateTime] { def capture(date: LocalDate, time: LocalTime, zone: TimeZoneId): LocalDateTime = ZonedDateTime.of(date, time, ZoneId.of(zone.id)).withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime } }
Example 7
Source File: jsDate.scala From dtc with Apache License 2.0 | 5 votes |
package dtc.instances import java.time._ import java.time.temporal.ChronoField import dtc.Local import dtc.js.JSDate object jsDate { implicit val jsDateLocalDTC: Local[JSDate] = new Local[JSDate] { def compare(x: JSDate, y: JSDate): Int = JSDate.compare(x, y) def of(date: LocalDate, time: LocalTime): JSDate = JSDate.of(date, time) def of(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int, millisecond: Int): JSDate = JSDate.of(year, month, day, hour, minute, second, millisecond) def date(x: JSDate): LocalDate = x.toLocalDate def time(x: JSDate): LocalTime = x.toLocalTime def plus(x: JSDate, d: Duration): JSDate = x.plus(d) def minus(x: JSDate, d: Duration): JSDate = x.minus(d) def plusDays(x: JSDate, days: Int): JSDate = x.plusDays(days) def plusMonths(x: JSDate, months: Int): JSDate = x.plusMonths(months) def plusYears(x: JSDate, years: Int): JSDate = x.plusYears(years) def withYear(x: JSDate, year: Int): JSDate = x.withYear(year) def withMonth(x: JSDate, month: Int): JSDate = x.withMonth(month) def withDayOfMonth(x: JSDate, dayOfMonth: Int): JSDate = x.withDayOfMonth(dayOfMonth) def withHour(x: JSDate, hour: Int): JSDate = x.withHour(hour) def withMinute(x: JSDate, minute: Int): JSDate = x.withMinute(minute) def withSecond(x: JSDate, second: Int): JSDate = x.withSecond(second) def withMillisecond(x: JSDate, millisecond: Int): JSDate = x.withMillisecond(millisecond) def withTime(x: JSDate, time: LocalTime): JSDate = withMillisecond( withSecond( withMinute( withHour(x, time.getHour), time.getMinute), time.getSecond), time.get(ChronoField.MILLI_OF_SECOND)) def withDate(x: JSDate, date: LocalDate): JSDate = withDayOfMonth(withMonth(withYear(x, date.getYear), date.getMonthValue), date.getDayOfMonth) def dayOfWeek(x: JSDate): DayOfWeek = x.dayOfWeek def dayOfMonth(x: JSDate): Int = x.dayOfMonth def month(x: JSDate): Int = x.month def year(x: JSDate): Int = x.year def millisecond(x: JSDate): Int = x.millisecond def second(x: JSDate): Int = x.second def minute(x: JSDate): Int = x.minute def hour(x: JSDate): Int = x.hour def yearsUntil(x: JSDate, until: JSDate): Long = x.yearsUntil(until) def monthsUntil(x: JSDate, until: JSDate): Long = x.monthsUntil(until) def daysUntil(x: JSDate, until: JSDate): Long = x.daysUntil(until) def hoursUntil(x: JSDate, until: JSDate): Long = x.hoursUntil(until) def minutesUntil(x: JSDate, until: JSDate): Long = x.minutesUntil(until) def secondsUntil(x: JSDate, until: JSDate): Long = x.secondsUntil(until) def millisecondsUntil(x: JSDate, until: JSDate): Long = x.millisecondsUntil(until) } }
Example 8
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 9
Source File: TimeBasedUUIDs.scala From akka-persistence-couchbase with Apache License 2.0 | 5 votes |
package akka.persistence.couchbase.internal import java.time.Instant import java.time.format.{DateTimeFormatterBuilder, SignStyle} import java.time.temporal.ChronoField import java.util.{Comparator, UUID} import akka.annotation.InternalApi def toSortableString(id: UUID): String = { require(id.version() == 1) val builder = new StringBuilder() val instant = UUIDTimestamp(id.timestamp()).toInstant builder.append(SortableTimeFormatter.format(instant)) builder.append('_') builder.append("%20s".format(java.lang.Long.toUnsignedString(id.getLeastSignificantBits))) builder.toString() } def fromSortableString(text: String): UUID = { val parts = text.split('_') val parsed = SortableTimeFormatter.parse(parts(0)) val instant = Instant.from(parsed).atZone(UUIDTimestamp.GMT) val timestamp = UUIDTimestamp(instant) val lsb = java.lang.Long.parseUnsignedLong(parts(1).trim) TimeBasedUUIDs.create(timestamp, lsb) } }
Example 10
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 11
Source File: JavaTimeCronDateTimeRegressionSpec.scala From cron4s with Apache License 2.0 | 5 votes |
package cron4s.lib.javatime import java.time.LocalDateTime import java.time.temporal.{ChronoField, ChronoUnit} import cron4s._ import org.scalatest.matchers.should.Matchers import org.scalatest.flatspec.AnyFlatSpec 10 * * * ?") val next = cron.next(from).get from.until(next, ChronoUnit.SECONDS) shouldBe 17L } // https://github.com/alonsodomin/cron4s/issues/59 "Cron with day of week" should "yield a date in the future" in { val cron = Cron.unsafeParse("0 0 0 ? * 1-3") for (dayOfMonth <- 1 to 30) { val from = LocalDateTime.of(2017, 3, dayOfMonth, 2, 0, 0) cron.next(from).forall(_.isAfter(from)) shouldBe true } } }
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: ScheduleEventCodecSpec.scala From aecor with MIT License | 5 votes |
package aecor.tests import java.time.temporal.{ ChronoField, Temporal } import java.time.{ Instant, LocalDateTime } import aecor.runtime.akkapersistence.serialization.{ PersistentDecoder, PersistentEncoder } import aecor.schedule.ScheduleEvent import org.scalacheck.{ Arbitrary, Gen, Properties, ScalacheckShapeless } import org.scalacheck.Prop.forAll class ScheduleEventCodecSpec extends Properties("ScheduleEventCodec") with ScalacheckShapeless { val encoder = PersistentEncoder[ScheduleEvent] val decoder = PersistentDecoder[ScheduleEvent] // OpenJDK 9+ offers more precise system clock than millisecond. // https://bugs.openjdk.java.net/browse/JDK-8068730 def dropBelowMillis[A <: Temporal](t: A): A = t.`with`(ChronoField.MICRO_OF_SECOND, t.getLong(ChronoField.MILLI_OF_SECOND) * 1000L) .asInstanceOf[A] implicit val arbitraryLocalDateTime = Arbitrary( Gen.lzy(Gen.const(dropBelowMillis(LocalDateTime.now()))) ) implicit val arbitraryInstant = Arbitrary(Gen.lzy(Gen.const(dropBelowMillis(Instant.now())))) property("encode/decode") = forAll { e: ScheduleEvent => val repr = encoder.encode(e) val decoded = decoder.decode(repr) decoded == Right(e) } }
Example 14
Source File: CronGenerator.scala From lemon-schedule with GNU General Public License v2.0 | 5 votes |
package com.gabry.job.utils import java.time.temporal.ChronoField import java.time.{Instant, ZoneId, ZonedDateTime} import java.util.Locale import com.cronutils.descriptor.CronDescriptor import com.cronutils.model.CronType import com.cronutils.model.definition.CronDefinitionBuilder import com.cronutils.model.time.ExecutionTime import com.cronutils.parser.CronParser def isValid(cronExpression:String):Boolean = { try{ parser.parse(cronExpression) true }catch{ case ex:Exception => false } } }