Skip to content

Commit

Permalink
Merge pull request #171 from alexarchambault/develop
Browse files Browse the repository at this point in the history
Injections from jodatime LocalDate / LocalTime / YearMonth / MonthDay to String
  • Loading branch information
johnynek committed Aug 14, 2014
2 parents 2e5a961 + bf3722f commit d0d45ba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.twitter.bijection.jodatime

import java.util.Date
import org.joda.time.DateTime
import org.joda.time.{ DateTime, LocalDate, LocalTime, YearMonth, MonthDay }
import com.twitter.bijection.Inversion.attempt
import com.twitter.bijection.{ Injection, InversionFailure, AbstractInjection }

Expand All @@ -25,4 +25,28 @@ trait DateInjections {
def apply(d: DateTime) = d.toDate()
override def invert(d: Date) = attempt(d)(new DateTime(_))
}

implicit val jodaLocalDate2String: Injection[LocalDate, String] =
new AbstractInjection[LocalDate, String] {
def apply(d: LocalDate) = d.toString
override def invert(s: String) = attempt(s)(LocalDate.parse(_))
}

implicit val jodaLocalTime2String: Injection[LocalTime, String] =
new AbstractInjection[LocalTime, String] {
def apply(d: LocalTime) = d.toString
override def invert(s: String) = attempt(s)(LocalTime.parse(_))
}

implicit val jodaYearMonth2String: Injection[YearMonth, String] =
new AbstractInjection[YearMonth, String] {
def apply(d: YearMonth) = d.toString
override def invert(s: String) = attempt(s)(YearMonth.parse(_))
}

implicit val jodaMonthDay2String: Injection[MonthDay, String] =
new AbstractInjection[MonthDay, String] {
def apply(d: MonthDay) = d.toString
override def invert(s: String) = attempt(s)(MonthDay.parse(_))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.scalacheck.Arbitrary
import org.scalacheck.Prop._
import com.twitter.bijection.{ Bijection, BaseProperties, ImplicitBijection }
import java.util.Date
import org.joda.time.DateTime
import org.joda.time.{ DateTime, LocalDate, LocalTime, YearMonth, MonthDay }
import com.twitter.bijection._

object DateBijectionsLaws extends Properties("DateBijections") with BaseProperties with DateBijections with DateInjections {
Expand All @@ -17,6 +17,14 @@ object DateBijectionsLaws extends Properties("DateBijections") with BaseProperti

implicit val date = arbitraryViaFn { (dtime: Long) => new DateTime(dtime) }

implicit val localDate = arbitraryViaFn { (dtime: Long) => new LocalDate(dtime) }

implicit val localTime = arbitraryViaFn { (dtime: Long) => new LocalTime(dtime) }

implicit val yearMonth = arbitraryViaFn { (dtime: Long) => new YearMonth(dtime) }

implicit val monthDay = arbitraryViaFn { (dtime: Long) => new MonthDay(dtime) }

property("Long <=> Joda") = isBijection[Long, DateTime]

property("Date <=> Joda") = isBijection[Date, DateTime]
Expand All @@ -25,4 +33,12 @@ object DateBijectionsLaws extends Properties("DateBijections") with BaseProperti

property("round trips Joda -> Date") = isLooseInjection[DateTime, Date]

property("round trips LocalDate -> String") = isLooseInjection[LocalDate, String]

property("round trips LocalTime -> String") = isLooseInjection[LocalTime, String]

property("round trips YearMonth -> String") = isLooseInjection[YearMonth, String]

property("round trips MonthDay -> String") = isLooseInjection[MonthDay, String]

}

0 comments on commit d0d45ba

Please sign in to comment.