nitypes.bintime.DateTime
- class nitypes.bintime.DateTime
- class DateTime(value: _OtherDateTime, /)
- class DateTime(year: SupportsIndex, month: SupportsIndex, day: SupportsIndex, hour: SupportsIndex = ..., minute: SupportsIndex = ..., second: SupportsIndex = ..., microsecond: SupportsIndex = ..., femtosecond: SupportsIndex = ..., yoctosecond: SupportsIndex = ..., tzinfo: datetime.tzinfo | None = None)
An absolute time in NI Binary Time Format (NI-BTF).
DateTime 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.
DateTime instances are duck typing compatible with a subset of the method and properties supported by
datetime.datetimeandhightime.datetime.This class only supports the UTC time zone and does not support timezone-naive times.
This class does not support the
foldproperty for disambiguating repeated times for daylight saving time and time zone changes.Constructing
As with
datetime.datetime, you can construct aDateTimeby specifying the year, month, day, etc.:>>> import datetime >>> DateTime(2025, 5, 25, 16, 45, tzinfo=datetime.timezone.utc) nitypes.bintime.DateTime(2025, 5, 25, 16, 45, tzinfo=datetime.timezone.utc)
Note
DateTimeonly supportsdatetime.timezone.utc. It does not support time-zone-naive objects or time zones other than UTC.You can also construct a
DateTimefrom adatetime.datetimeorhightime.datetime:>>> DateTime(datetime.datetime(2025, 5, 25, 16, 45, tzinfo=datetime.timezone.utc)) nitypes.bintime.DateTime(2025, 5, 25, 16, 45, tzinfo=datetime.timezone.utc) >>> import hightime >>> DateTime(hightime.datetime(2025, 5, 25, 16, 45, tzinfo=datetime.timezone.utc)) nitypes.bintime.DateTime(2025, 5, 25, 16, 45, tzinfo=datetime.timezone.utc)
You can get the current time of day by calling
DateTime.now:>>> DateTime.now(datetime.timezone.utc) nitypes.bintime.DateTime(...)
Properties
Like other
datetimeobjects,DateTimehas properties for the year, month, day, hour, minute, second, and microsecond.>>> import datetime >>> x = DateTime(datetime.datetime(2025, 5, 25, 16, 45, tzinfo=datetime.timezone.utc)) >>> (x.year, x.month, x.day) (2025, 5, 25) >>> (x.hour, x.minute, x.second, x.microsecond) (16, 45, 0, 0)
Like
hightime.datetime, it also supports the femtosecond and yoctosecond properties.>>> (x.femtosecond, x.yoctosecond) (0, 0)
Resolution
NI-BTF is a high-resolution time format, so it has significantly higher resolution than
datetime.datetime. However,hightime.datetimehas even higher resolution:Class
Smallest Time Increment
1 microsecond (1e-6 sec)
54210 yoctoseconds (5.4e-20 sec)
1 yoctosecond (1e-24 sec)
As a result,
hightime.datetimecan represent the time down to the exact yoctosecond, butDateTimerounds the yoctosecond field.>>> import hightime >>> x = hightime.datetime(2025, 1, 1, yoctosecond=123456789, tzinfo=datetime.timezone.utc) >>> x hightime.datetime(2025, 1, 1, 0, 0, 0, 0, 0, 123456789, tzinfo=datetime.timezone.utc) >>> DateTime(x) nitypes.bintime.DateTime(2025, 1, 1, 0, 0, 0, 0, 0, 123436417, tzinfo=datetime.timezone.utc)
Rounding
NI-BTF represents fractional seconds 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.
For example, it may round 100 microseconds down to 99.9999… microseconds.
>>> x = hightime.datetime(2025, 1, 1, microsecond=100, tzinfo=datetime.timezone.utc) >>> x hightime.datetime(2025, 1, 1, 0, 0, 0, 100, tzinfo=datetime.timezone.utc) >>> DateTime(x) nitypes.bintime.DateTime(2025, 1, 1, 0, 0, 0, 99, 999999999, 999991239, tzinfo=datetime.timezone.utc)
Class members
- max: ClassVar[DateTime]
The latest supported
DateTimeobject, before midnight on Dec 31, 9999, UTC.
- __slots__ = ['_offset', '_hightime_cache']
- classmethod from_ticks(ticks: SupportsIndex) typing_extensions.Self
Create an DateTime from a 128-bit fixed point number expressed as an integer.
- classmethod from_tuple(value: nitypes.bintime.TimeValueTuple) typing_extensions.Self
Create a DateTime from whole and fractional seconds as 64-bit ints.
- classmethod from_offset(offset: nitypes.bintime.TimeDelta) typing_extensions.Self
Create an DateTime from a TimeValue offset from the epoch, Jan 1, 1904.
- property yoctosecond: int
The yoctosecond, between 0 and 999_999_999 inclusive.
Warning
Because this class uses a 64-bit binary fraction, the smallest time increment it can represent is
1.0 / (1 << 64)seconds, which is about 54210 yoctoseconds.
- property tzinfo: datetime.tzinfo | None
The time zone.
- to_tuple() nitypes.bintime.TimeValueTuple
Convert to the number of whole and fractional seconds since the epoch, Jan 1, 1904.
- classmethod now(tz: datetime.tzinfo | None = None) typing_extensions.Self
Return the current absolute time.
- __add__(value: nitypes.bintime.TimeDelta | nitypes.bintime._timedelta._OtherTimeDelta, /) DateTime
Return self+value.
- __radd__
- __sub__(value: DateTime | _OtherDateTime, /) nitypes.bintime.TimeDelta
- __sub__(value: nitypes.bintime.TimeDelta | nitypes.bintime._timedelta._OtherTimeDelta, /) DateTime
Return self-value.
- __rsub__(value: DateTime | _OtherDateTime, /) nitypes.bintime.TimeDelta
- __rsub__(value: nitypes.bintime.TimeDelta | nitypes.bintime._timedelta._OtherTimeDelta, /) DateTime
Return value-self.