Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrettyDuration #19

Open
jchapuis opened this issue May 17, 2018 · 1 comment
Open

PrettyDuration #19

jchapuis opened this issue May 17, 2018 · 1 comment

Comments

@jchapuis
Copy link

jchapuis commented May 17, 2018

Hey, about about porting over your PrettyDuration internal class in Akka to this library? It's something I repeatedly copy/paste because it's so handy for logging and annoying to code 😉

How about something along the lines of (not systematically tested):

import java.util.Locale

import com.markatta.timeforscala.{ Duration, Nanos, TimeUnit }

import scala.concurrent.duration.{ DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, SECONDS }

object PrettyDuration {
  implicit class PrettyPrintableDuration(val duration: Duration) extends AnyVal {

    /** Selects most appropriate TimeUnit for given duration and formats it accordingly, with 4 digits precision **/
    def pretty: String = pretty(includeNanos = false)

    /** Selects most appropriate TimeUnit for given duration and formats it accordingly */
    def pretty(includeNanos: Boolean, precision: Int = 4): String = {
      require(precision > 0, "precision must be > 0")

      duration match {
        case d: Duration =>
          val nanos = d.toNanos
          val unit  = chooseUnit(nanos)
          val value = nanos.toDouble / NANOSECONDS.convert(1, unit)
          s"%.${precision}g %s%s".formatLocal(
            Locale.ROOT,
            value,
            abbreviate(unit),
            if (includeNanos) s" ($nanos ns)" else ""
          )
        case _ => "undefined"
      }
    }

    def chooseUnit(nanos: Long): TimeUnit = {
      val d = Nanos(nanos)

      if (d.toDays > 0) DAYS
      else if (d.toHours > 0) HOURS
      else if (d.toMinutes > 0) MINUTES
      else if (d.getSeconds > 0) SECONDS
      else if (d.toMillis > 0) MILLISECONDS
      else if (d.toNanos / 1000 > 0) MICROSECONDS
      else NANOSECONDS
    }

    def abbreviate(unit: TimeUnit): String = unit match {
      case NANOSECONDS  => "ns"
      case MICROSECONDS => "μs"
      case MILLISECONDS => "ms"
      case SECONDS      => "s"
      case MINUTES      => "min"
      case HOURS        => "h"
      case DAYS         => "d"
    }
  }

}
@johanandren
Copy link
Owner

Sounds like a good idea, please PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants