Represents an instant in time, used to both get the timestamp value and to measure the duration of a time span. Inspired by Rust's `std::time::Instant`.
| 23 | |
| 24 | @dataclasses.dataclass(frozen=True) |
| 25 | class Instant: |
| 26 | """ |
| 27 | Represents an instant in time, used to both get the timestamp value and to measure |
| 28 | the duration of a time span. |
| 29 | |
| 30 | Inspired by Rust's `std::time::Instant`. |
| 31 | """ |
| 32 | |
| 33 | # Creation time of this instant, using time.time(), to measure actual time. |
| 34 | # Note: using a `lambda` to correctly get the mocked time via `MockTiming`. |
| 35 | # pylint: disable-next=lambda-assignment |
| 36 | time: float = dataclasses.field(default_factory=lambda: time(), init=False) # noqa: PLW0108 |
| 37 | |
| 38 | # Performance counter tick of the instant, used to measure precise elapsed time. |
| 39 | # Note: using a `lambda` to correctly get the mocked time via `MockTiming`. |
| 40 | perf_count: float = dataclasses.field( |
| 41 | default_factory=lambda: perf_counter(), # noqa: PLW0108 |
| 42 | init=False, |
| 43 | ) |
| 44 | |
| 45 | def elapsed(self) -> Duration: |
| 46 | """Measure the duration since `Instant` was created.""" |
| 47 | return Duration(start=self, stop=Instant()) |
| 48 | |
| 49 | def as_utc(self) -> datetime: |
| 50 | """Instant as UTC datetime.""" |
| 51 | return datetime.fromtimestamp(self.time, timezone.utc) |
| 52 | |
| 53 | |
| 54 | @dataclasses.dataclass(frozen=True) |