src/Pure/General/time.scala
author wenzelm
Tue Apr 01 16:16:25 2014 +0200 (2014-04-01)
changeset 56351 1c735e46acf0
parent 53292 f567c1c7b180
child 56691 ad5d7461b370
permissions -rw-r--r--
simplified using "value class";
wenzelm@45674
     1
/*  Title:      Pure/General/time.scala
wenzelm@45673
     2
    Module:     PIDE
wenzelm@40393
     3
    Author:     Makarius
wenzelm@40393
     4
wenzelm@45674
     5
Time based on milliseconds.
wenzelm@40393
     6
*/
wenzelm@40393
     7
wenzelm@40393
     8
package isabelle
wenzelm@40393
     9
wenzelm@45664
    10
wenzelm@50298
    11
import java.util.Locale
wenzelm@50298
    12
wenzelm@50298
    13
wenzelm@40848
    14
object Time
wenzelm@40393
    15
{
wenzelm@47993
    16
  def seconds(s: Double): Time = new Time((s * 1000.0).round)
wenzelm@44699
    17
  def ms(m: Long): Time = new Time(m)
wenzelm@51587
    18
  val zero: Time = ms(0)
wenzelm@51533
    19
wenzelm@51533
    20
  def print_seconds(s: Double): String =
wenzelm@51533
    21
    String.format(Locale.ROOT, "%.3f", s.asInstanceOf[AnyRef])
wenzelm@40393
    22
}
wenzelm@40393
    23
wenzelm@56351
    24
final class Time private(val ms: Long) extends AnyVal
wenzelm@40848
    25
{
wenzelm@51587
    26
  def + (t: Time): Time = new Time(ms + t.ms)
wenzelm@51587
    27
wenzelm@40848
    28
  def seconds: Double = ms / 1000.0
wenzelm@40852
    29
wenzelm@40852
    30
  def min(t: Time): Time = if (ms < t.ms) this else t
wenzelm@40852
    31
  def max(t: Time): Time = if (ms > t.ms) this else t
wenzelm@40852
    32
wenzelm@53292
    33
  def is_zero: Boolean = ms == 0
wenzelm@46768
    34
  def is_relevant: Boolean = ms >= 1
wenzelm@46768
    35
wenzelm@51533
    36
  override def toString = Time.print_seconds(seconds)
wenzelm@46768
    37
wenzelm@40848
    38
  def message: String = toString + "s"
wenzelm@40848
    39
}
wenzelm@40848
    40