All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.kodeworks.kvarg.util.DateImplicits.scala Maven / Gradle / Ivy

There is a newer version: 0.7
Show newest version
package no.kodeworks.kvarg.util

import java.sql.Timestamp
import java.time.{Instant, LocalDate, LocalDateTime, ZoneOffset}

import slick.basic.DatabaseConfig
import slick.jdbc.JdbcProfile

trait DateImplicits {
  val dbCfg: DatabaseConfig[JdbcProfile]

  import dbCfg.profile.api._

  implicit def localDateColumnType = MappedColumnType.base[LocalDate, Timestamp](
    { ld =>
      if (null == ld) null else
        new Timestamp(ld.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli)
    }, { ts =>
      if (ts == null) null else
        LocalDateTime.ofInstant(Instant.ofEpochMilli(ts.getTime), ZoneOffset.UTC).toLocalDate
    }
  )

  implicit def localDateTimeColumnType = MappedColumnType.base[LocalDateTime, Timestamp](
    { ldt =>
      if (null == ldt) null else
        new Timestamp(ldt.toInstant(ZoneOffset.UTC).toEpochMilli)
    }, { timestamp =>
      if (timestamp == null) null else
        LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp.getTime), ZoneOffset.UTC)
    }
  )
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy