Skip to content
Pyrula
Pre-release: the packages are not on PyPI yet, so pip install does not resolve. Build from source to try it today.

Duration

Duration is an immutable, hashable time span backed by an i64 nanosecond count.

from pyrula import Duration
Duration.millis(250) # integer units: nanos, micros, millis
Duration.seconds(1.5) # float units: seconds, minutes, hours, days
Duration.seconds(0.5) == Duration.millis(500) # True

Integer constructors take an exact int. Float constructors round to the nearest nanosecond.

d = Duration.seconds(1.5)
d.to_nanos() # 1_500_000_000
d.to_millis() # 1500 (truncates)
d.to_seconds() # 1.5
Duration.seconds(1) + Duration.millis(500) # Duration(1500ms)
Duration.seconds(1) * 3 # Duration(3s)
Duration.seconds(1) < Duration.seconds(2) # True

+, -, * and all six comparisons are supported. repr uses the largest unit that is exact: Duration(250ms), Duration(2m).

import datetime as dt
Duration.seconds(1).to_timedelta() # datetime.timedelta(seconds=1)
Duration.from_timedelta(dt.timedelta(milliseconds=250)) # Duration(250ms)

timedelta has microsecond resolution, so to_timedelta() truncates finer precision.

View this page as Markdown