src/Pure/General/time.scala
author wenzelm
Tue, 26 Mar 2024 21:44:18 +0100
changeset 80018 ac4412562c7b
parent 79775 752806151432
permissions -rw-r--r--
more robust XML body: allow empty text, as well as arbitrary pro-forma markup (e.g. see XML.blob in Isabelle/ML);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45674
eb65c9d17e2f clarified Time vs. Timing;
wenzelm
parents: 45673
diff changeset
     1
/*  Title:      Pure/General/time.scala
40393
2bb7ec08574a somewhat more uniform timing in ML vs. Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
2bb7ec08574a somewhat more uniform timing in ML vs. Scala;
wenzelm
parents:
diff changeset
     3
45674
eb65c9d17e2f clarified Time vs. Timing;
wenzelm
parents: 45673
diff changeset
     4
Time based on milliseconds.
40393
2bb7ec08574a somewhat more uniform timing in ML vs. Scala;
wenzelm
parents:
diff changeset
     5
*/
2bb7ec08574a somewhat more uniform timing in ML vs. Scala;
wenzelm
parents:
diff changeset
     6
2bb7ec08574a somewhat more uniform timing in ML vs. Scala;
wenzelm
parents:
diff changeset
     7
package isabelle
2bb7ec08574a somewhat more uniform timing in ML vs. Scala;
wenzelm
parents:
diff changeset
     8
45664
ac6e704dcd12 tuned signature (according to ML version);
wenzelm
parents: 45249
diff changeset
     9
50298
1426d478ccda tuned import;
wenzelm
parents: 47993
diff changeset
    10
import java.util.Locale
64056
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents: 63700
diff changeset
    11
import java.time.Instant
50298
1426d478ccda tuned import;
wenzelm
parents: 47993
diff changeset
    12
1426d478ccda tuned import;
wenzelm
parents: 47993
diff changeset
    13
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74158
diff changeset
    14
object Time {
47993
135fd6f2dadd less warning in scala-2.10.0-M3;
wenzelm
parents: 46768
diff changeset
    15
  def seconds(s: Double): Time = new Time((s * 1000.0).round)
74158
wenzelm
parents: 73702
diff changeset
    16
  def minutes(m: Double): Time = new Time((m * 60000.0).round)
wenzelm
parents: 73702
diff changeset
    17
  def ms(ms: Long): Time = new Time(ms)
63700
2a95d904672e clarified options and arguments;
wenzelm
parents: 63688
diff changeset
    18
  def hms(h: Int, m: Int, s: Double): Time = seconds(s + 60 * m + 3600 * h)
63686
66f217416da7 statistics from session build output;
wenzelm
parents: 62571
diff changeset
    19
61528
053f7083b3eb global start time as reference point;
wenzelm
parents: 57912
diff changeset
    20
  def now(): Time = ms(System.currentTimeMillis())
053f7083b3eb global start time as reference point;
wenzelm
parents: 57912
diff changeset
    21
51587
7050c4656fd8 more operations on Time, Timing;
wenzelm
parents: 51533
diff changeset
    22
  val zero: Time = ms(0)
78357
0cecb7236298 more operations;
wenzelm
parents: 75393
diff changeset
    23
  val min: Time = ms(Long.MinValue)
0cecb7236298 more operations;
wenzelm
parents: 75393
diff changeset
    24
  val max: Time = ms(Long.MaxValue)
51533
3f6280aedbcc dockable window for timing information;
wenzelm
parents: 50298
diff changeset
    25
3f6280aedbcc dockable window for timing information;
wenzelm
parents: 50298
diff changeset
    26
  def print_seconds(s: Double): String =
3f6280aedbcc dockable window for timing information;
wenzelm
parents: 50298
diff changeset
    27
    String.format(Locale.ROOT, "%.3f", s.asInstanceOf[AnyRef])
64056
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents: 63700
diff changeset
    28
64058
ea528dc9962d proper calculation;
wenzelm
parents: 64056
diff changeset
    29
  def instant(t: Instant): Time = ms(t.getEpochSecond * 1000L + t.getNano / 1000000L)
79775
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    30
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    31
  def guard_property(prop: String): Time =
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    32
    System.getProperty(prop, "") match {
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    33
      case Value.Seconds(t) => t
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    34
      case "true" => Time.min
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    35
      case "false" | "" => Time.max
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    36
      case s =>
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    37
        error("Bad system property " + prop + ": " + quote(s) +
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    38
        "\n expected true, false, or time in seconds")
752806151432 clarified signature: incorporate guard into Logger;
wenzelm
parents: 78888
diff changeset
    39
    }
40393
2bb7ec08574a somewhat more uniform timing in ML vs. Scala;
wenzelm
parents:
diff changeset
    40
}
2bb7ec08574a somewhat more uniform timing in ML vs. Scala;
wenzelm
parents:
diff changeset
    41
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74158
diff changeset
    42
final class Time private(val ms: Long) extends AnyVal {
65616
b8738569b8db clarified database layout;
wenzelm
parents: 65597
diff changeset
    43
  def proper_ms: Option[Long] = if (ms == 0) None else Some(ms)
b8738569b8db clarified database layout;
wenzelm
parents: 65597
diff changeset
    44
40848
8662b9b1f123 more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents: 40393
diff changeset
    45
  def seconds: Double = ms / 1000.0
63688
cc57255bf6ae gnuplot presentation similar to former isatest-statistics;
wenzelm
parents: 63686
diff changeset
    46
  def minutes: Double = ms / 60000.0
40852
aee98c88c587 builtin time bounds (again);
wenzelm
parents: 40848
diff changeset
    47
56691
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    48
  def + (t: Time): Time = new Time(ms + t.ms)
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    49
  def - (t: Time): Time = new Time(ms - t.ms)
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    50
61602
a2f0f659a3c2 added option timeout_scale;
wenzelm
parents: 61528
diff changeset
    51
  def compare(t: Time): Int = ms compare t.ms
56691
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    52
  def < (t: Time): Boolean = ms < t.ms
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    53
  def <= (t: Time): Boolean = ms <= t.ms
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    54
  def > (t: Time): Boolean = ms > t.ms
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    55
  def >= (t: Time): Boolean = ms >= t.ms
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    56
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    57
  def min(t: Time): Time = if (this < t) this else t
ad5d7461b370 tuned signature, in accordance to ML version;
wenzelm
parents: 56351
diff changeset
    58
  def max(t: Time): Time = if (this > t) this else t
40852
aee98c88c587 builtin time bounds (again);
wenzelm
parents: 40848
diff changeset
    59
78888
95bbf9a576b3 prefer Time.scale(), following Isabelle/ML;
wenzelm
parents: 78357
diff changeset
    60
  def scale(s: Double): Time = new Time((s * ms).ceil.toLong)
95bbf9a576b3 prefer Time.scale(), following Isabelle/ML;
wenzelm
parents: 78357
diff changeset
    61
53292
f567c1c7b180 option to insert unique completion immediately into buffer;
wenzelm
parents: 51587
diff changeset
    62
  def is_zero: Boolean = ms == 0
46768
46acd255810d relevant timing as in ML;
wenzelm
parents: 46712
diff changeset
    63
  def is_relevant: Boolean = ms >= 1
46acd255810d relevant timing as in ML;
wenzelm
parents: 46712
diff changeset
    64
57912
wenzelm
parents: 56691
diff changeset
    65
  override def toString: String = Time.print_seconds(seconds)
46768
46acd255810d relevant timing as in ML;
wenzelm
parents: 46712
diff changeset
    66
40848
8662b9b1f123 more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents: 40393
diff changeset
    67
  def message: String = toString + "s"
62571
2fd90993a928 print timing like lib/scripts/timestop.bash;
wenzelm
parents: 61602
diff changeset
    68
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74158
diff changeset
    69
  def message_hms: String = {
62571
2fd90993a928 print timing like lib/scripts/timestop.bash;
wenzelm
parents: 61602
diff changeset
    70
    val s = ms / 1000
2fd90993a928 print timing like lib/scripts/timestop.bash;
wenzelm
parents: 61602
diff changeset
    71
    String.format(Locale.ROOT, "%d:%02d:%02d",
71163
b5822f4c3fde tuned -- avoid deprecated constructors;
wenzelm
parents: 65617
diff changeset
    72
      java.lang.Long.valueOf(s / 3600),
b5822f4c3fde tuned -- avoid deprecated constructors;
wenzelm
parents: 65617
diff changeset
    73
      java.lang.Long.valueOf((s / 60) % 60),
b5822f4c3fde tuned -- avoid deprecated constructors;
wenzelm
parents: 65617
diff changeset
    74
      java.lang.Long.valueOf(s % 60))
62571
2fd90993a928 print timing like lib/scripts/timestop.bash;
wenzelm
parents: 61602
diff changeset
    75
  }
64056
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents: 63700
diff changeset
    76
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents: 63700
diff changeset
    77
  def instant: Instant = Instant.ofEpochMilli(ms)
71684
5036edb025b7 clarified signature;
wenzelm
parents: 71163
diff changeset
    78
73702
7202e12cb324 tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents: 73385
diff changeset
    79
  def sleep(): Unit = Thread.sleep(ms)
40848
8662b9b1f123 more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents: 40393
diff changeset
    80
}