| 1942 | |
| 1943 | |
| 1944 | class _Duration(Duration): |
| 1945 | @classmethod |
| 1946 | def from_timedelta( |
| 1947 | cls, delta: timedelta, *, _1_microsecond: timedelta = timedelta(microseconds=1) |
| 1948 | ) -> "_Duration": |
| 1949 | total_ms = delta // _1_microsecond |
| 1950 | seconds = int(total_ms / 1e6) |
| 1951 | nanos = int((total_ms % 1e6) * 1e3) |
| 1952 | return cls(seconds, nanos) |
| 1953 | |
| 1954 | def to_timedelta(self) -> timedelta: |
| 1955 | return timedelta(seconds=self.seconds, microseconds=self.nanos / 1e3) |
| 1956 | |
| 1957 | @staticmethod |
| 1958 | def delta_to_json(delta: timedelta) -> str: |
| 1959 | parts = str(delta.total_seconds()).split(".") |
| 1960 | if len(parts) > 1: |
| 1961 | while len(parts[1]) not in (3, 6, 9): |
| 1962 | parts[1] = f"{parts[1]}0" |
| 1963 | return f"{'.'.join(parts)}s" |
| 1964 | |
| 1965 | |
| 1966 | class _Timestamp(Timestamp): |