Skip to content

Commit

Permalink
Merge pull request #214 from kinoplan/feature/implicits-java-time
Browse files Browse the repository at this point in the history
Add implicits java time
  • Loading branch information
kazievab authored Aug 30, 2022
2 parents 20ec4e5 + 83e62fd commit c9ceaad
Show file tree
Hide file tree
Showing 28 changed files with 991 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .sbtopts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-J-Xms512M
-J-Xmx1024M
-J-Xms2G
-J-Xmx3G
-J-XX:+UseConcMarkSweepGC
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ libraryDependencies ++= Seq(
"io.kinoplan" %% "utils-implicits-any" % ${version}, // JVM and Scala.js
"io.kinoplan" %% "utils-implicits-boolean" % ${version}, // JVM and Scala.js
"io.kinoplan" %% "utils-implicits-collection" % ${version}, // JVM and Scala.js
"io.kinoplan" %% "utils-implicits-java-time" % ${version}, // JVM and Scala.js
"io.kinoplan" %% "utils-implicits-joda-time" % ${version}, // JVM only
// play
"io.kinoplan" %% "utils-play-reactivemongo" % ${version}, // JVM only
Expand Down
31 changes: 11 additions & 20 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ ThisBuild / resolvers += "Artima Maven Repository".at("https://repo.artima.com/r
lazy val date = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("common/date"))
.configure(ModulesCommon.dateProfile)
.jsConfigure(ProjectSettings.scalaJsProfile)

lazy val dateJs = date.js
lazy val dateJvm = date.jvm
.configureCross(ModulesCommon.dateProfile)

lazy val logbackConfig = project
.in(file("common/logback-config"))
Expand All @@ -36,34 +32,29 @@ lazy val reactivemongoBsonAny = project
lazy val implicitsAny = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("implicits/any"))
.configure(ModulesImplicits.anyProfile)
.jsConfigure(ProjectSettings.scalaJsProfile)

lazy val implicitsAnyJs = implicitsAny.js
lazy val implicitsAnyJvm = implicitsAny.jvm
.configureCross(ModulesImplicits.anyProfile)

lazy val implicitsBoolean = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("implicits/boolean"))
.configure(ModulesImplicits.booleanProfile)
.jsConfigure(ProjectSettings.scalaJsProfile)

lazy val implicitsBooleanJs = implicitsBoolean.js
lazy val implicitsBooleanJvm = implicitsBoolean.jvm
.configureCross(ModulesImplicits.booleanProfile)

lazy val implicitsCollection = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("implicits/collection"))
.configure(ModulesImplicits.collectionProfile)
.jsConfigure(ProjectSettings.scalaJsProfile)
.configureCross(ModulesImplicits.collectionProfile)

lazy val implicitsCollectionJs = implicitsCollection.js
lazy val implicitsCollectionJvm = implicitsCollection.jvm
lazy val implicitsJavaTime = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("implicits/date/java-time"))
.configureCross(ModulesImplicits.javaTimeProfile)
.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))
.dependsOn(date)

lazy val implicitsJodaTime = project
.in(file("implicits/date/joda-time"))
.configure(ModulesImplicits.jodaTimeProfile)
.dependsOn(dateJvm)
.dependsOn(date.jvm)

// zzzzzzzzzzzzzzzzzzzz Play Modules zzzzzzzzzzzzzzzzzzzz

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package io.kinoplan.utils.date
trait DateTimePattern {
val `yyyyMMddHHmmss` = "yyyyMMddHHmmss"
val `dd MMMM yyyy HH:mm` = "dd MMMM yyyy HH:mm"
val `dd MMMM yyyy, HH:mm` = "dd MMMM yyyy, HH:mm"
val `dd.MM.yyyy HH:mm` = "dd.MM.yyyy HH:mm"
val `dd.MM.yyyy HH_mm` = "dd.MM.yyyy HH_mm"
val `dd.MM.yyyy / HH:mm` = "dd.MM.yyyy / HH:mm"
val `dd_MM_yyyy_HH_mm` = "dd_MM_yyyy_HH_mm"
val `dd.MM.yy HH:mm` = "dd.MM.yy HH:mm"
Expand All @@ -14,6 +16,7 @@ trait DateTimePattern {
val `yyyy-MM-dd HH:mm` = "yyyy-MM-dd HH:mm"
val `yyyy-MM-dd'T'HH:mm:ss` = "yyyy-MM-dd'T'HH:mm:ss"
val `yyyy-MM-dd'T'HH:mm:ss.SSS` = "yyyy-MM-dd'T'HH:mm:ss.SSS"
val `HH:mm:ss dd.MM.yyyy` = "HH:mm:ss dd.MM.yyyy"
}

object DateTimePattern extends DateTimePattern
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ trait DateTimePatternExtension extends DatePatternExtension with TimePatternExte
@inline
def `dd MMMM yyyy HH:mm`: String = toString(DateTimePattern.`dd MMMM yyyy HH:mm`)

@inline
def `dd MMMM yyyy, HH:mm`: String = toString(DateTimePattern.`dd MMMM yyyy, HH:mm`)

@inline
def `dd.MM.yyyy HH:mm`: String = toString(DateTimePattern.`dd.MM.yyyy HH:mm`)

@inline
def `dd.MM.yyyy HH_mm`: String = toString(DateTimePattern.`dd.MM.yyyy HH_mm`)

@inline
def `dd.MM.yyyy / HH:mm`: String = toString(DateTimePattern.`dd.MM.yyyy / HH:mm`)

Expand Down Expand Up @@ -42,4 +48,7 @@ trait DateTimePatternExtension extends DatePatternExtension with TimePatternExte
@inline
def `yyyy-MM-dd'T'HH:mm:ss.SSS`: String = toString(DateTimePattern.`yyyy-MM-dd'T'HH:mm:ss.SSS`)

@inline
def `HH:mm:ss dd.MM.yyyy`: String = toString(DateTimePattern.`HH:mm:ss dd.MM.yyyy`)

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.kinoplan.utils.date
trait TimePattern {
val `HH:mm:ss` = "HH:mm:ss"
val `HH:mm` = "HH:mm"
val `HH_mm` = "HH_mm"
val `mm:ss` = "mm:ss"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ trait TimePatternExtension {
@inline
def `HH:mm`: String = toString(TimePattern.`HH:mm`)

@inline
def `HH_mm`: String = toString(TimePattern.`HH_mm`)

@inline
def `mm:ss`: String = toString(TimePattern.`mm:ss`)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.kinoplan.utils.implicits.java.time

import java.time._

import scala.util.Try

object JavaTime {

object offsetDateTime {

def fromUnixTimestamp(value: Int): OffsetDateTime = fromUnixTimestamp(value.toLong)

def fromUnixTimestamp(value: Long): OffsetDateTime = OffsetDateTime
.ofInstant(Instant.ofEpochSecond(value), ZoneId.systemDefault())

def fromUnixTimestampO(value: Int): Option[OffsetDateTime] = fromUnixTimestampO(value.toLong)

def fromUnixTimestampO(value: Long): Option[OffsetDateTime] =
if (value > 0) Some(fromUnixTimestamp(value))
else None

def parse(value: String): Try[OffsetDateTime] = Try(OffsetDateTime.parse(value))

def parseO(value: String): Option[OffsetDateTime] = parse(value).toOption

}

object localDate {

def fromUnixTimestamp(value: Int): LocalDate = offsetDateTime
.fromUnixTimestamp(value)
.toLocalDate

def fromUnixTimestamp(value: Long): LocalDate = offsetDateTime
.fromUnixTimestamp(value)
.toLocalDate

def fromUnixTimestampO(value: Int): Option[LocalDate] = fromUnixTimestampO(value.toLong)

def fromUnixTimestampO(value: Long): Option[LocalDate] =
if (value > 0) Some(fromUnixTimestamp(value))
else None

def parse(value: String): Try[LocalDate] = Try(LocalDate.parse(value))

def parseO(value: String): Option[LocalDate] = parse(value).toOption

}

object localDateTime {
def fromUnixTimestamp(value: Int): LocalDateTime = fromUnixTimestamp(value.toLong)

def fromUnixTimestamp(value: Long): LocalDateTime = LocalDateTime
.ofInstant(Instant.ofEpochSecond(value), ZoneId.systemDefault())

def fromUnixTimestampO(value: Int): Option[LocalDateTime] = fromUnixTimestampO(value.toLong)

def fromUnixTimestampO(value: Long): Option[LocalDateTime] =
if (value > 0) Some(fromUnixTimestamp(value))
else None

def parse(value: String): Try[LocalDateTime] = Try(LocalDateTime.parse(value))

def parseO(value: String): Option[LocalDateTime] = parse(value).toOption

}

object localTime {

def fromUnixTimestamp(value: Int): LocalTime = offsetDateTime
.fromUnixTimestamp(value)
.toLocalTime

def fromUnixTimestamp(value: Long): LocalTime = offsetDateTime
.fromUnixTimestamp(value)
.toLocalTime

def fromUnixTimestampO(value: Int): Option[LocalTime] = fromUnixTimestampO(value.toLong)

def fromUnixTimestampO(value: Long): Option[LocalTime] =
if (value > 0) Some(fromUnixTimestamp(value))
else None

def parse(value: String): Try[LocalTime] = Try(LocalTime.parse(value))

def parseO(value: String): Option[LocalTime] = parse(value).toOption

}

object offsetTime {

def fromUnixTimestamp(value: Int): OffsetTime = offsetDateTime
.fromUnixTimestamp(value)
.toOffsetTime

def fromUnixTimestamp(value: Long): OffsetTime = offsetDateTime
.fromUnixTimestamp(value)
.toOffsetTime

def fromUnixTimestampO(value: Int): Option[OffsetTime] = fromUnixTimestampO(value.toLong)

def fromUnixTimestampO(value: Long): Option[OffsetTime] =
if (value > 0) Some(fromUnixTimestamp(value))
else None

def parse(value: String): Try[OffsetTime] = Try(OffsetTime.parse(value))

def parseO(value: String): Option[OffsetTime] = parse(value).toOption

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.kinoplan.utils.implicits.java.time

import java.time.{LocalDate, OffsetDateTime, ZoneOffset}
import java.time.format.DateTimeFormatter

import io.kinoplan.utils.date.DatePatternExtension

final private[implicits] class LocalDateOps(private val value: LocalDate)
extends DatePatternExtension {

@inline
def toOffsetDateTime: OffsetDateTime = value.atStartOfDay().atOffset(ZoneOffset.UTC)

override def toString(pattern: String): String = DateTimeFormatter.ofPattern(pattern).format(value)

}

trait LocalDateSyntax {
implicit final def syntaxLocalDateOps(value: LocalDate): LocalDateOps = new LocalDateOps(value)
}

object LocalDateSyntax extends LocalDateSyntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.kinoplan.utils.implicits.java.time

import java.time.{LocalDateTime, ZoneId}
import java.time.format.DateTimeFormatter

import io.kinoplan.utils.date.DateTimePatternExtension

final private[implicits] class LocalDateTimeOps(private val value: LocalDateTime)
extends DateTimePatternExtension {

@inline
def timestamp: Int = timestampLong.toInt

@inline
def timestampLong: Long = value.atZone(ZoneId.systemDefault()).toInstant.toEpochMilli / 1000

override def toString(pattern: String): String = DateTimeFormatter.ofPattern(pattern).format(value)

}

trait LocalDateTimeSyntax {

implicit final def syntaxLocalDateTimeOps(value: LocalDateTime): LocalDateTimeOps =
new LocalDateTimeOps(value)

}

object LocalDateTimeSyntax extends LocalDateTimeSyntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.kinoplan.utils.implicits.java.time

import java.time.LocalTime
import java.time.format.DateTimeFormatter

import io.kinoplan.utils.date.TimePatternExtension

final private[implicits] class LocalTimeOps(private val value: LocalTime)
extends TimePatternExtension {

override def toString(pattern: String): String = DateTimeFormatter.ofPattern(pattern).format(value)

}

trait LocalTimeSyntax {
implicit final def syntaxLocalTimeOps(value: LocalTime): LocalTimeOps = new LocalTimeOps(value)
}

object LocalTimeSyntax extends LocalTimeSyntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.kinoplan.utils.implicits.java.time

import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

import io.kinoplan.utils.date.DateTimePatternExtension

final private[implicits] class OffsetDateTimeOps(private val value: OffsetDateTime)
extends DateTimePatternExtension {

@inline
def timestamp: Int = timestampLong.toInt

@inline
def timestampLong: Long = value.toInstant.toEpochMilli / 1000

override def toString(pattern: String): String = DateTimeFormatter.ofPattern(pattern).format(value)

}

trait OffsetDateTimeSyntax {

implicit final def syntaxOffsetDateTimeOps(value: OffsetDateTime): OffsetDateTimeOps =
new OffsetDateTimeOps(value)

}

object OffsetDateTimeSyntax extends OffsetDateTimeSyntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.kinoplan.utils.implicits.java.time

import java.time.OffsetTime
import java.time.format.DateTimeFormatter

import io.kinoplan.utils.date.TimePatternExtension

final private[implicits] class OffsetTimeOps(private val value: OffsetTime)
extends TimePatternExtension {

override def toString(pattern: String): String = DateTimeFormatter.ofPattern(pattern).format(value)

}

trait OffsetTimeSyntax {
implicit final def syntaxOffsetTimeOps(value: OffsetTime): OffsetTimeOps = new OffsetTimeOps(value)
}

object OffsetTimeSyntax extends OffsetTimeSyntax
Loading

0 comments on commit c9ceaad

Please sign in to comment.