src/Pure/General/time.scala
author wenzelm
Thu Apr 24 11:01:14 2014 +0200 (2014-04-24)
changeset 56691 ad5d7461b370
parent 56351 1c735e46acf0
child 57912 dd9550f84106
permissions -rw-r--r--
tuned signature, in accordance to ML version;
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@56691
    19
  def now(): Time = ms(System.currentTimeMillis())
wenzelm@51533
    20
wenzelm@51533
    21
  def print_seconds(s: Double): String =
wenzelm@51533
    22
    String.format(Locale.ROOT, "%.3f", s.asInstanceOf[AnyRef])
wenzelm@40393
    23
}
wenzelm@40393
    24
wenzelm@56351
    25
final class Time private(val ms: Long) extends AnyVal
wenzelm@40848
    26
{
wenzelm@40848
    27
  def seconds: Double = ms / 1000.0
wenzelm@40852
    28
wenzelm@56691
    29
  def + (t: Time): Time = new Time(ms + t.ms)
wenzelm@56691
    30
  def - (t: Time): Time = new Time(ms - t.ms)
wenzelm@56691
    31
wenzelm@56691
    32
  def < (t: Time): Boolean = ms < t.ms
wenzelm@56691
    33
  def <= (t: Time): Boolean = ms <= t.ms
wenzelm@56691
    34
  def > (t: Time): Boolean = ms > t.ms
wenzelm@56691
    35
  def >= (t: Time): Boolean = ms >= t.ms
wenzelm@56691
    36
wenzelm@56691
    37
  def min(t: Time): Time = if (this < t) this else t
wenzelm@56691
    38
  def max(t: Time): Time = if (this > t) this else t
wenzelm@40852
    39
wenzelm@53292
    40
  def is_zero: Boolean = ms == 0
wenzelm@46768
    41
  def is_relevant: Boolean = ms >= 1
wenzelm@46768
    42
wenzelm@51533
    43
  override def toString = Time.print_seconds(seconds)
wenzelm@46768
    44
wenzelm@40848
    45
  def message: String = toString + "s"
wenzelm@40848
    46
}
wenzelm@40848
    47