-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from kinoplan/feature/implicits-java-time
Add implicits java time
- Loading branch information
Showing
28 changed files
with
991 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
implicits/date/java-time/src/main/scala/io/kinoplan/utils/implicits/java/time/JavaTime.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...date/java-time/src/main/scala/io/kinoplan/utils/implicits/java/time/LocalDateSyntax.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
28 changes: 28 additions & 0 deletions
28
.../java-time/src/main/scala/io/kinoplan/utils/implicits/java/time/LocalDateTimeSyntax.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
19 changes: 19 additions & 0 deletions
19
...date/java-time/src/main/scala/io/kinoplan/utils/implicits/java/time/LocalTimeSyntax.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
28 changes: 28 additions & 0 deletions
28
...java-time/src/main/scala/io/kinoplan/utils/implicits/java/time/OffsetDateTimeSyntax.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
19 changes: 19 additions & 0 deletions
19
...ate/java-time/src/main/scala/io/kinoplan/utils/implicits/java/time/OffsetTimeSyntax.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.