Skip to content

Commit

Permalink
Add hours and days
Browse files Browse the repository at this point in the history
  • Loading branch information
zainab-ali committed Aug 7, 2017
1 parent eb8bac8 commit 86e5ef4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/scala/libra/imperial/package.scala
@@ -0,0 +1,27 @@
package libra

import ops.base.{Show, ConversionFactor}
import spire._, spire.algebra._, spire.math._, spire.implicits._
import singleton.ops._
import libra.si.Time

/* Imperial units */
package object imperial {

/** Time units */
trait Day extends Unit[Time]
trait Hour extends Unit[Time]

implicit def dayShow: Show[Day] = Show[Day]("days")
implicit def hourShow: Show[Hour] = Show[Hour]("hours")

implicit def dayHourConversion[A](
implicit c: ConvertableTo[A]
): ConversionFactor[A, Time, Day, Hour] =
new ConversionFactor(c.fromInt(24))

implicit final class BaseQuantityImperialOps[A](val a: A) extends AnyVal {
def days: QuantityOf[A, Time, Day] = Quantity(a)
def hours: QuantityOf[A, Time, Hour] = Quantity(a)
}
}
29 changes: 29 additions & 0 deletions src/test/scala/libra/ImperialSpec.scala
@@ -0,0 +1,29 @@
package libra

import spire.implicits._
import libra.imperial._, libra.si._

import org.scalatest._

class ImperialSpec extends WordSpec {

"day" should {
"show" in {
assert(2.days.show === ("2 days [T]"))
}

"hour value" in {
assert(2.0.days.to[Hour].value === (48.0))
}
}

"hour" should {
"show" in {
assert(2.hours.show === ("2 hours [T]"))
}

"day value" in {
assert(24.0.hours.to[Day].value === (1.0))
}
}
}

0 comments on commit 86e5ef4

Please sign in to comment.