A nanosecond-resolution timestamp.
| 2 | |
| 3 | |
| 4 | class Timestamp: |
| 5 | """A nanosecond-resolution timestamp.""" |
| 6 | |
| 7 | def __init__(self, sec: float, nsec: float) -> None: |
| 8 | if nsec < 0 or nsec >= 1e9: |
| 9 | raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}") |
| 10 | if sec < 0: |
| 11 | nsec = -nsec |
| 12 | self.sec: int = int(sec) |
| 13 | self.nsec: int = int(nsec) |
| 14 | |
| 15 | def __str__(self) -> str: |
| 16 | return f"{self.sec}.{self.nsec:09d}" |
| 17 | |
| 18 | def __repr__(self) -> str: |
| 19 | return f"Timestamp({self.sec}, {self.nsec})" |
| 20 | |
| 21 | def __float__(self) -> float: |
| 22 | return float(self.sec) + float(self.nsec) / 1e9 |
| 23 | |
| 24 | def __eq__(self, other: object) -> bool: |
| 25 | return isinstance(other, Timestamp) and self.sec == other.sec and self.nsec == other.nsec |
| 26 | |
| 27 | def __ne__(self, other: object) -> bool: |
| 28 | return not self == other |
| 29 | |
| 30 | def __gt__(self, other: "Timestamp") -> bool: |
| 31 | return self.nsec > other.nsec if self.sec == other.sec else self.sec > other.sec |
| 32 | |
| 33 | def __lt__(self, other: "Timestamp") -> bool: |
| 34 | return self.nsec < other.nsec if self.sec == other.sec else self.sec < other.sec |
| 35 | |
| 36 | |
| 37 | # BucketSpan is experimental and subject to change at any time. |
no outgoing calls