author | wenzelm |
Tue, 26 Mar 2024 21:44:18 +0100 | |
changeset 80018 | ac4412562c7b |
parent 79775 | 752806151432 |
permissions | -rw-r--r-- |
45674 | 1 |
/* Title: Pure/General/time.scala |
40393 | 2 |
Author: Makarius |
3 |
||
45674 | 4 |
Time based on milliseconds. |
40393 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
45664 | 9 |
|
50298 | 10 |
import java.util.Locale |
64056 | 11 |
import java.time.Instant |
50298 | 12 |
|
13 |
||
75393 | 14 |
object Time { |
47993 | 15 |
def seconds(s: Double): Time = new Time((s * 1000.0).round) |
74158 | 16 |
def minutes(m: Double): Time = new Time((m * 60000.0).round) |
17 |
def ms(ms: Long): Time = new Time(ms) |
|
63700 | 18 |
def hms(h: Int, m: Int, s: Double): Time = seconds(s + 60 * m + 3600 * h) |
63686 | 19 |
|
61528 | 20 |
def now(): Time = ms(System.currentTimeMillis()) |
21 |
||
51587 | 22 |
val zero: Time = ms(0) |
78357 | 23 |
val min: Time = ms(Long.MinValue) |
24 |
val max: Time = ms(Long.MaxValue) |
|
51533 | 25 |
|
26 |
def print_seconds(s: Double): String = |
|
27 |
String.format(Locale.ROOT, "%.3f", s.asInstanceOf[AnyRef]) |
|
64056 | 28 |
|
64058 | 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 | 40 |
} |
41 |
||
75393 | 42 |
final class Time private(val ms: Long) extends AnyVal { |
65616 | 43 |
def proper_ms: Option[Long] = if (ms == 0) None else Some(ms) |
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 | 47 |
|
56691 | 48 |
def + (t: Time): Time = new Time(ms + t.ms) |
49 |
def - (t: Time): Time = new Time(ms - t.ms) |
|
50 |
||
61602 | 51 |
def compare(t: Time): Int = ms compare t.ms |
56691 | 52 |
def < (t: Time): Boolean = ms < t.ms |
53 |
def <= (t: Time): Boolean = ms <= t.ms |
|
54 |
def > (t: Time): Boolean = ms > t.ms |
|
55 |
def >= (t: Time): Boolean = ms >= t.ms |
|
56 |
||
57 |
def min(t: Time): Time = if (this < t) this else t |
|
58 |
def max(t: Time): Time = if (this > t) this else t |
|
40852 | 59 |
|
78888 | 60 |
def scale(s: Double): Time = new Time((s * ms).ceil.toLong) |
61 |
||
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
51587
diff
changeset
|
62 |
def is_zero: Boolean = ms == 0 |
46768 | 63 |
def is_relevant: Boolean = ms >= 1 |
64 |
||
57912 | 65 |
override def toString: String = Time.print_seconds(seconds) |
46768 | 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 | 68 |
|
75393 | 69 |
def message_hms: String = { |
62571 | 70 |
val s = ms / 1000 |
71 |
String.format(Locale.ROOT, "%d:%02d:%02d", |
|
71163 | 72 |
java.lang.Long.valueOf(s / 3600), |
73 |
java.lang.Long.valueOf((s / 60) % 60), |
|
74 |
java.lang.Long.valueOf(s % 60)) |
|
62571 | 75 |
} |
64056 | 76 |
|
77 |
def instant: Instant = Instant.ofEpochMilli(ms) |
|
71684 | 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 |
} |