nitypes.bintime.TimeDelta
- class nitypes.bintime.TimeDelta
- class TimeDelta(value: _OtherTimeDelta, /)
- class TimeDelta(seconds: SupportsIndex | decimal.Decimal | float)
A duration, represented in NI Binary Time Format (NI-BTF).
TimeDelta represents time as a 128-bit fixed point number with 64-bit whole seconds and 64-bit fractional seconds.
Warning
The fractional seconds are represented as a binary fraction, which is a sum of inverse powers of 2. Values that are not exactly representable as binary fractions will display rounding error or “bruising” similar to a floating point number.
TimeDelta instances are duck typing compatible with
datetime.timedeltaandhightime.timedelta.Constructing
You can construct a
TimeDeltafrom a number of seconds, expressed as anint,float, ordecimal.Decimal.>>> TimeDelta(100) nitypes.bintime.TimeDelta(Decimal('100')) >>> TimeDelta(100.125) nitypes.bintime.TimeDelta(Decimal('100.125')) >>> from decimal import Decimal >>> TimeDelta(Decimal("100.125")) nitypes.bintime.TimeDelta(Decimal('100.125'))
TimeDeltahas the same resolution and rounding behavior asDateTime.>>> TimeDelta(Decimal("100.01234567890123456789")) nitypes.bintime.TimeDelta(Decimal('100.012345678901234567889'))
Unlike other
timedeltaobjects, you cannot construct aTimeDeltafrom separate weeks, days, hours, etc. If you want to do that, construct adatetime.timedeltaorhightime.timedeltaand then use it to construct aTimeDelta.>>> import datetime, hightime >>> TimeDelta(datetime.timedelta(days=1, microseconds=1)) nitypes.bintime.TimeDelta(Decimal('86400.0000010000000000000')) >>> TimeDelta(hightime.timedelta(days=1, femtoseconds=1)) nitypes.bintime.TimeDelta(Decimal('86400.0000000000000010000'))
Math Operations
DateTimeandTimeDeltasupport the same math operations asdatetime.datetimeanddatetime.timedelta.For example, you can add or subtract
TimeDeltaobjects together:>>> TimeDelta(100.5) + TimeDelta(0.5) nitypes.bintime.TimeDelta(Decimal('101')) >>> TimeDelta(100.5) - TimeDelta(0.5) nitypes.bintime.TimeDelta(Decimal('100'))
Or add/subtract a
DateTimewith aTimeDelta,datetime.timedelta, orhightime.timedelta:>>> DateTime(2025, 1, 1, tzinfo=datetime.timezone.utc) + TimeDelta(86400) nitypes.bintime.DateTime(2025, 1, 2, 0, 0, tzinfo=datetime.timezone.utc) >>> DateTime(2025, 1, 1, tzinfo=datetime.timezone.utc) + datetime.timedelta(days=1) nitypes.bintime.DateTime(2025, 1, 2, 0, 0, tzinfo=datetime.timezone.utc) >>> DateTime(2025, 1, 1, tzinfo=datetime.timezone.utc) + hightime.timedelta(femtoseconds=1) nitypes.bintime.DateTime(2025, 1, 1, 0, 0, 0, 0, 1, 13873, tzinfo=datetime.timezone.utc)
Class members
- __slots__ = ['_ticks']
- classmethod from_ticks(ticks: SupportsIndex) typing_extensions.Self
Create a TimeDelta from a 128-bit fixed point number expressed as an integer.
- classmethod from_tuple(value: nitypes.bintime.TimeValueTuple) typing_extensions.Self
Create a TimeDelta from 64-bit whole seconds and fractional seconds ints.
- property femtoseconds: int
The number of femtoseconds in the time delta, up to the nearest microsecond.
- property yoctoseconds: int
The number of yoctoseconds in the time delta, up to the nearest femtosecond.
Warning
Because this class uses a 64-bit binary fraction, the smallest value it can represent is
1.0 / (1 << 64)seconds, which is about 54210 yoctoseconds.
- to_tuple() nitypes.bintime.TimeValueTuple
The whole seconds and fractional seconds parts of the time delta as 64-bit ints.
- total_seconds() float
The total seconds in the time delta.
Warning
Converting a time value to a floating point number loses precision. Consider using
precision_total_secondsinstead.
- precision_total_seconds() decimal.Decimal
The precise total seconds in the time delta.
Note: up to 64 significant digits are used in computation.
- __add__(value: TimeDelta | _OtherTimeDelta, /) TimeDelta
- __add__(value: hightime.datetime, /) hightime.datetime
- __add__(value: datetime.datetime, /) datetime.datetime
Return self+value.
- __radd__
- __rsub__(value: TimeDelta | _OtherTimeDelta, /) TimeDelta
- __rsub__(value: hightime.datetime, /) hightime.datetime
- __rsub__(value: datetime.datetime, /) datetime.datetime
Return value-self.
- __mul__(value: int | float | decimal.Decimal, /) TimeDelta
Return self*value.
- __rmul__