-
Notifications
You must be signed in to change notification settings - Fork 4
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 #8 from t2v/feature/java8-date-time
Support Java8 Date&Time API
- Loading branch information
Showing
10 changed files
with
282 additions
and
155 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,5 +1,8 @@ | ||
language: scala | ||
|
||
scala: | ||
- 2.12.1 | ||
|
||
jdk: | ||
- oraclejdk8 | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package jp.t2v.util.locale | ||
|
||
object Implicits { | ||
|
||
implicit class HasHolidayNameOps[A](val a: A)(implicit ev: LocalDateConverter[A]) { | ||
def holidayName: Option[String] = Holidays(ev(a)) | ||
def isHoliday: Boolean = holidayName.isDefined | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
core/src/main/scala/jp/t2v/util/locale/LocalDateConverter.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,44 @@ | ||
package jp.t2v.util.locale | ||
|
||
import java.time.{LocalDate, LocalDateTime, ZonedDateTime} | ||
|
||
@FunctionalInterface | ||
abstract class LocalDateConverter[A] { | ||
def apply(value: A): LocalDate | ||
} | ||
object LocalDateConverter extends LowPriorityLocalDateConverter { | ||
implicit val localDate: LocalDateConverter[LocalDate] = d => d | ||
implicit def localDateTime: LocalDateConverter[LocalDateTime] = _.toLocalDate | ||
implicit def zonedDateTime: LocalDateConverter[ZonedDateTime] = _.toLocalDate | ||
} | ||
|
||
trait LowPriorityLocalDateConverter { | ||
import scala.language.experimental.macros | ||
implicit def jodaLocalDateConverter[A]: LocalDateConverter[A] = macro LocalDateConverterMacro.joda[A] | ||
} | ||
|
||
private[locale] object LocalDateConverterMacro { | ||
import scala.reflect.macros.blackbox | ||
|
||
def joda[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[LocalDateConverter[A]] = { | ||
import c.universe._ | ||
val A = weakTypeTag[A].tpe | ||
val expr = A.toString match { | ||
case "org.joda.time.LocalDate" => | ||
q"""new jp.t2v.util.locale.LocalDateConverter[$A] { | ||
def apply(d: $A) = java.time.LocalDate.of(d.getYear, d.getMonthOfYear, d.getDayOfMonth) | ||
}""" | ||
case "org.joda.time.DateTime" => | ||
q"""new jp.t2v.util.locale.LocalDateConverter[$A] { | ||
def apply(value: $A) = { | ||
val d = value.toLocalDate | ||
java.time.LocalDate.of(d.getYear, d.getMonthOfYear, d.getDayOfMonth) | ||
} | ||
}""" | ||
case _ => | ||
c.abort(c.enclosingPosition, s"Implicit LocalDateConverter[$A] is missing.") | ||
} | ||
c.Expr[LocalDateConverter[A]](expr) | ||
} | ||
|
||
} |
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,73 @@ | ||
package jp.t2v.util.locale | ||
|
||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
import org.scalatest._ | ||
|
||
class HolidaysSpec extends FlatSpec with Matchers { | ||
|
||
"The Holidays" should "return holiday name" in { | ||
import jp.t2v.util.locale.Implicits._ | ||
|
||
LocalDate.of(2012, 1, 1).holidayName should equal (Some("元日")) | ||
LocalDate.of(2012, 1, 2).holidayName should equal (Some("振替休日")) | ||
LocalDate.of(2012, 1, 9).holidayName should equal (Some("成人の日")) | ||
LocalDate.of(2012, 2, 11).holidayName should equal (Some("建国記念の日")) | ||
LocalDate.of(2012, 3, 20).holidayName should equal (Some("春分の日")) | ||
LocalDate.of(2012, 4, 29).holidayName should equal (Some("昭和の日")) | ||
LocalDate.of(2012, 4, 30).holidayName should equal (Some("振替休日")) | ||
LocalDate.of(2012, 5, 3).holidayName should equal (Some("憲法記念日")) | ||
LocalDate.of(2012, 5, 4).holidayName should equal (Some("みどりの日")) | ||
LocalDate.of(2012, 5, 5).holidayName should equal (Some("こどもの日")) | ||
LocalDate.of(2012, 7, 16).holidayName should equal (Some("海の日")) | ||
LocalDate.of(2012, 9, 17).holidayName should equal (Some("敬老の日")) | ||
LocalDate.of(2012, 9, 22).holidayName should equal (Some("秋分の日")) | ||
LocalDate.of(2012, 10, 8).holidayName should equal (Some("体育の日")) | ||
LocalDate.of(2012, 11, 3).holidayName should equal (Some("文化の日")) | ||
LocalDate.of(2012, 11, 23).holidayName should equal (Some("勤労感謝の日")) | ||
LocalDate.of(2012, 12, 23).holidayName should equal (Some("天皇誕生日")) | ||
LocalDate.of(2012, 12, 24).holidayName should equal (Some("振替休日")) | ||
|
||
// 水曜日の振替休日 | ||
LocalDate.of(2009, 5, 6).holidayName should equal (Some("振替休日")) | ||
} | ||
|
||
implicit class WrapString(s: String) { | ||
def ld: LocalDate = DateTimeFormatter.ofPattern("yyyy/MM/dd").parse(s, LocalDate.from) | ||
} | ||
|
||
"Holidays extractor" should "extract holiday name" in { | ||
val d = Seq( | ||
"2012/04/28".ld, | ||
"2012/04/29".ld, | ||
"2012/04/30".ld, | ||
"2012/05/01".ld, | ||
"2012/05/02".ld, | ||
"2012/05/03".ld, | ||
"2012/05/04".ld, | ||
"2012/05/05".ld, | ||
"2012/05/06".ld | ||
) | ||
|
||
val actual = d map { | ||
case Holidays(name) => name | ||
case _ => "平日" | ||
} | ||
|
||
val expected = Seq( | ||
"平日", | ||
"昭和の日", | ||
"振替休日", | ||
"平日", | ||
"平日", | ||
"憲法記念日", | ||
"みどりの日", | ||
"こどもの日", | ||
"平日" | ||
) | ||
|
||
actual should equal (expected) | ||
} | ||
|
||
} |
Oops, something went wrong.