dacr / unittools

scala unit tools : durations and bytes size

GitHub

unittools Build Status License Codacy codecov

Some basic unit conversion operation. It Assumes that default units are milliseconds for durations, and bytes for data size.

In your build.sbt, add this :

libraryDependencies += "fr.janalyse" %% "unittools" % version

Latest version: Maven Scaladex

Usage

Examples coming from the test cases :

  test("duration rewritten") {
    "1".toDuration.toDurationDesc should equal("1ms")
    "7d".toDuration.toDurationDesc should equal("1w")
    "60m".toDuration.toDurationDesc should equal("1h")
    "3600s".toDuration.toDurationDesc should equal("1h")
    "3600000ms".toDuration.toDurationDesc should equal("1h")
  }

  
  test("size basics") {
    0.toSizeDesc should equal("0b")
    1.toSizeDesc should equal("1b")
    "1mb".toSize should equal(pow(1024L,2))
    "10mb25kb".toSize should equal(10*pow(1024L,2)+25*1024L)
  }


  test("classes parameter usage") {
    import fr.janalyse.unittools._
    case class ExampleClass(howlong:DurationHelper, amount:SizeHelper)
    val example = ExampleClass(howlong="5h30m", amount="10mb")
    val howlong:Long = example.howlong
    val amount:Long  = example.amount
  }

  
  test("basics") {
    (5 m).value should equal (60L*1000*5)
    (3 h).value should equal (3600L*1000*3)
    (1.m + 1.s).value should equal (61L*1000)
    (1.h + 10.m + 10.s).value should equal (3600L*1000+600*1000+10*1000)
  }