@@ -31,41 +31,36 @@ class Instant:
3131
3232 # Creation time of this instant, using time.time(), to measure actual time.
3333 # Use a `lambda` to initialize the default to correctly get the mocked time via `MockTiming`.
34- _start_time : float = dataclasses .field (default_factory = lambda : time (), init = False )
34+ time : float = dataclasses .field (default_factory = lambda : time (), init = False )
3535
36- # Initial " tick" of the performance counter to measure precise elapsed time.
36+ # Performance counter tick of the instant, used to measure precise elapsed time.
3737 # Use a `lambda` to initialize the default to correctly get the mocked time via `MockTiming`.
38- _start_perf : float = dataclasses .field (
38+ perf_count : float = dataclasses .field (
3939 default_factory = lambda : perf_counter (), init = False
4040 )
4141
4242 def duration (self ) -> Duration :
4343 """Measure the duration since `Instant` was created."""
44- return Duration (
45- start = self . _start_time ,
46- elapsed_s = perf_counter ( ) - self . _start_perf ,
47- stop = time (),
48- )
44+ return Duration (start = self , stop = Instant ())
45+
46+ def as_utc ( self ) -> datetime :
47+ """Instant as UTC datetime."""
48+ return datetime . fromtimestamp ( self . time , timezone . utc )
4949
5050
5151@dataclasses .dataclass (frozen = True )
5252class Duration :
5353 """A span of time as measured by `Instant.duration()`."""
5454
5555 # Start time of the duration, as seconds since epoch.
56- start : float
56+ start : Instant
5757 # Stop time of the duration, as seconds since epoch.
58- stop : float
59- # Elapsed time of the duration, in seconds, measured using a performance counter for precise timing.
60- elapsed_s : float
61-
62- def start_utc (self ) -> datetime :
63- """Start time of the duration, as a datetime in UTC."""
64- return datetime .fromtimestamp (self .start , timezone .utc )
58+ stop : Instant
6559
66- def stop_utc (self ) -> datetime : # pragma: no cover
67- """Stop time of the duration, as a datetime in UTC."""
68- return datetime .fromtimestamp (self .stop , timezone .utc )
60+ @property
61+ def elapsed_s (self ) -> float :
62+ """Elapsed time of the duration, in seconds, measured using a performance counter for precise timing."""
63+ return self .stop .perf_count - self .start .perf_count
6964
7065
7166@dataclasses .dataclass
0 commit comments