blob: 10974e009db24f9b476c18d0d15297e9d0e76826 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# The Duration type.
Duration = datetime.timedelta
# There is a loss of precision when converting from Rust durations,
# which are accurate to the nanosecond,
# to Python durations, which are only accurate to the microsecond.
class _UniffiConverterDuration(_UniffiConverterRustBuffer):
@staticmethod
def read(buf):
seconds = buf.read_u64()
microseconds = buf.read_u32() / 1.0e3
return datetime.timedelta(seconds=seconds, microseconds=microseconds)
@staticmethod
def write(value, buf):
seconds = value.seconds + value.days * 24 * 3600
nanoseconds = value.microseconds * 1000
if seconds < 0:
raise ValueError("Invalid duration, must be non-negative")
buf.write_i64(seconds)
buf.write_u32(nanoseconds)
|