1 package frost.time
2
3 ====================================================================================================
4 A high-resolution timer which measures time either from the epoch (midnight UTC on January 1st,
5 1970) or between two events.
6 ====================================================================================================
7 class Timer {
8 @private
9 def native:Int64
10
11 @private
12 var basis:Real64 := 0
13
14 ================================================================================================
15 Creates a `Timer` which initially measures time relative to the epoch.
16 ================================================================================================
17 init() {
18 setup()
19 }
20
21 @override
22 method cleanup() {
23 destroy()
24 }
25
26 @private
27 @external(frostTimerSetup)
28 method setup()
29
30 @private
31 @external(frostTimerDestroy)
32 method destroy()
33
34 ================================================================================================
35 Reports the time elapsed in seconds. If this timer has been [reset()], reports the number of
36 seconds since the last `reset`, otherwise reports the number of seconds since the epoch.
37 ================================================================================================
38 method elapsed():Real64 {
39 return Real64(now()) - basis
40 }
41
42 @private
43 @external(frostTimerNow)
44 method now():builtin_float64
45
46 ================================================================================================
47 Resets the timer so that subsequent calls to [elapsed()] will be relative to this point in time.
48 ================================================================================================
49 method reset() {
50 basis := now()
51 }
52 }