diff options
author | Stewart Stewart <stewinsalot@gmail.com> | 2016-12-17 00:50:22 -0500 |
---|---|---|
committer | Stewart Stewart <stewinsalot@gmail.com> | 2016-12-17 00:50:22 -0500 |
commit | 8415a3675234b2fa15d0806ed0ed3100c1aef932 (patch) | |
tree | 849be016906c50281be3904c837e7130af7f7662 /src/main/scala/xyz/driver/core/date.scala | |
parent | 9bf738070a6588cd731adef5173f586fa52611c8 (diff) | |
parent | 1cab0ee11c8bcee14103dbd0fb5e350241b9d260 (diff) | |
download | driver-core-8415a3675234b2fa15d0806ed0ed3100c1aef932.tar.gz driver-core-8415a3675234b2fa15d0806ed0ed3100c1aef932.tar.bz2 driver-core-8415a3675234b2fa15d0806ed0ed3100c1aef932.zip |
Merge branch 'date' into dal-improvements
Diffstat (limited to 'src/main/scala/xyz/driver/core/date.scala')
-rw-r--r-- | src/main/scala/xyz/driver/core/date.scala | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/date.scala b/src/main/scala/xyz/driver/core/date.scala new file mode 100644 index 0000000..05d139d --- /dev/null +++ b/src/main/scala/xyz/driver/core/date.scala @@ -0,0 +1,45 @@ +package xyz.driver.core + +import java.util.Calendar + +import scalaz.{@@, Tag} + +object date { + + type Month = Int @@ Month.type + private[date] def tagMonth(value: Int): Month = Tag.of[Month.type](value) + + object Month { + val JANUARY = tagMonth(Calendar.JANUARY) + val FEBRUARY = tagMonth(Calendar.FEBRUARY) + val MARCH = tagMonth(Calendar.MARCH) + val APRIL = tagMonth(Calendar.APRIL) + val MAY = tagMonth(Calendar.MAY) + val JUNE = tagMonth(Calendar.JUNE) + val JULY = tagMonth(Calendar.JULY) + val AUGUST = tagMonth(Calendar.AUGUST) + val SEPTEMBER = tagMonth(Calendar.SEPTEMBER) + val OCTOBER = tagMonth(Calendar.OCTOBER) + val DECEMBER = tagMonth(Calendar.DECEMBER) + } + + final case class Date(year: Int, month: Month, day: Int) { + def iso8601: String = f"$year%04d-${Tag.unwrap(month) + 1}%02d-$day%02d" + def toJavaSqlDate = new java.sql.Date(toJavaDate.getTime) + def toJavaDate: java.util.Date = { + val cal = Calendar.getInstance() + cal.set(Calendar.YEAR, year - 1900) + cal.set(Calendar.MONTH, Tag.unwrap(month)) + cal.set(Calendar.DAY_OF_MONTH, day) + cal.getTime + } + } + + object Date { + def fromJavaDate(date: java.util.Date) = { + val cal = Calendar.getInstance() + cal.setTime(date) + Date(cal.get(Calendar.YEAR), tagMonth(cal.get(Calendar.MONTH)), cal.get(Calendar.DAY_OF_MONTH)) + } + } +} |