Struct DateTime64
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
A 64-bit signed tick count representing a date/time value with full
long range and a NaT sentinel, matching NumPy's
np.datetime64 semantics. Used as a conversion-helper type in
Converts.
[Serializable]
public readonly struct DateTime64 : IComparable, IComparable<DateTime64>, IEquatable<DateTime64>, IConvertible, ISpanFormattable, IFormattable
- Implements
- Inherited Members
- Extension Methods
Remarks
One tick equals 100 nanoseconds (same unit as Ticks).
Ticks == 0 is midnight of 1 January 0001 (the
Gregorian epoch of DateTime), which is not the Unix
epoch.
NaT semantics. NaT (Ticks == long.MinValue)
is Not-a-Time, analogous to IEEE 754 NaN:
- NaT propagates through arithmetic.
operator ==/!=/</>/<=/>=follow NumPy: any comparison involving NaT isfalsefor==/</>/<=/>=, andtruefor!=.- Equals(DateTime64) follows the IEquatable<T> convention (two NaTs are considered equal bit-wise) so that GetHashCode() is contract-compliant and NaT can be used as a Dictionary<TKey, TValue> key. This mirrors how .NET handles NaN:
double.NaN.Equals(double.NaN)istruebutdouble.NaN == double.NaNisfalse.
Constructors
DateTime64(DateTime)
Constructs a DateTime64 from a DateTime. The Kind is discarded (NumPy datetime64 has no timezone).
public DateTime64(DateTime dateTime)
Parameters
dateTimeDateTime
DateTime64(DateTimeOffset)
Constructs a DateTime64 from a DateTimeOffset. Stored as UtcTicks (offset discarded).
public DateTime64(DateTimeOffset dateTimeOffset)
Parameters
dateTimeOffsetDateTimeOffset
DateTime64(long)
Constructs a DateTime64 from a raw tick count (any int64, including NaT).
public DateTime64(long ticks)
Parameters
tickslong
Fields
Epoch
The .NET calendar epoch (midnight 0001-01-01), same as MinValue.
public static readonly DateTime64 Epoch
Field Value
MaxValue
The largest representable value (Ticks == long.MaxValue).
public static readonly DateTime64 MaxValue
Field Value
MinValue
The smallest non-NaT representable value (Ticks == long.MinValue + 1).
public static readonly DateTime64 MinValue
Field Value
NaT
Not-a-Time sentinel (Ticks == long.MinValue), matching NumPy.
public static readonly DateTime64 NaT
Field Value
Properties
IsNaT
true iff this instance is Not-a-Time (Ticks == long.MinValue).
public bool IsNaT { get; }
Property Value
IsValidDateTime
public bool IsValidDateTime { get; }
Property Value
Ticks
The raw 100-ns tick count (full int64; long.MinValue for NaT).
public long Ticks { get; }
Property Value
Methods
Add(TimeSpan)
Add a TimeSpan. NaT propagates; overflow saturates to NaT.
public DateTime64 Add(TimeSpan value)
Parameters
valueTimeSpan
Returns
AddTicks(long)
Add a raw tick delta. NaT propagates; overflow saturates to NaT.
public DateTime64 AddTicks(long delta)
Parameters
deltalong
Returns
Compare(DateTime64, DateTime64)
Compares by ticks. NaT sorts before every other value (as the smallest int64).
public static int Compare(DateTime64 t1, DateTime64 t2)
Parameters
t1DateTime64t2DateTime64
Returns
CompareTo(DateTime64)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
public int CompareTo(DateTime64 value)
Parameters
valueDateTime64
Returns
- int
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes otherin the sort order.Zero This instance occurs in the same position in the sort order as other.Greater than zero This instance follows otherin the sort order.
CompareTo(object?)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
public int CompareTo(object? value)
Parameters
valueobject
Returns
- int
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes objin the sort order.Zero This instance occurs in the same position in the sort order as obj.Greater than zero This instance follows objin the sort order.
Exceptions
- ArgumentException
objis not the same type as this instance.
Equals(DateTime64)
Bitwise tick equality (NaT.Equals(NaT) returns true).
public bool Equals(DateTime64 other)
Parameters
otherDateTime64
Returns
Equals(DateTime64, DateTime64)
public static bool Equals(DateTime64 t1, DateTime64 t2)
Parameters
t1DateTime64t2DateTime64
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? value)
Parameters
valueobject
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Parse(string)
Parses a string produced by ToString(). Case-sensitive
"NaT" literal returns NaT. Otherwise delegates
to Parse(string, IFormatProvider).
public static DateTime64 Parse(string s)
Parameters
sstring
Returns
Parse(string, IFormatProvider?)
public static DateTime64 Parse(string s, IFormatProvider? provider)
Parameters
sstringproviderIFormatProvider
Returns
Subtract(DateTime64)
Difference as a TimeSpan. If either operand is NaT,
returns MinValue (TimeSpan's NaT-equivalent,
since TimeSpan.MinValue.Ticks == long.MinValue).
public TimeSpan Subtract(DateTime64 other)
Parameters
otherDateTime64
Returns
Subtract(TimeSpan)
Subtract a TimeSpan. NaT propagates; overflow saturates to NaT.
public DateTime64 Subtract(TimeSpan value)
Parameters
valueTimeSpan
Returns
ToDateTime()
Convert to DateTime. Throws InvalidOperationException for NaT / out-of-range.
public DateTime ToDateTime()
Returns
ToDateTime(DateTime)
Convert to DateTime, returning fallback
for NaT / out-of-range values instead of throwing.
public DateTime ToDateTime(DateTime fallback)
Parameters
fallbackDateTime
Returns
ToDateTimeOffset()
Convert to DateTimeOffset at UTC. Throws for NaT / out-of-range.
public DateTimeOffset ToDateTimeOffset()
Returns
ToDateTimeOffset(DateTimeOffset)
Convert to DateTimeOffset, returning fallback
for NaT / out-of-range values instead of throwing.
public DateTimeOffset ToDateTimeOffset(DateTimeOffset fallback)
Parameters
fallbackDateTimeOffset
Returns
ToString()
Formats as ISO-8601 (DateTime's "o" format) for
in-range values, "NaT" for NaT, and "DateTime64(ticks=N)"
for values outside DateTime's range.
public override string ToString()
Returns
ToString(IFormatProvider?)
public string ToString(IFormatProvider? provider)
Parameters
providerIFormatProvider
Returns
ToString(string?)
public string ToString(string? format)
Parameters
formatstring
Returns
ToString(string?, IFormatProvider?)
Formats the value of the current instance using the specified format.
public string ToString(string? format, IFormatProvider? provider)
Parameters
formatstringThe format to use.
-or-
A null reference (
Nothingin Visual Basic) to use the default format defined for the type of the IFormattable implementation.providerIFormatProvider
Returns
- string
The value of the current instance in the specified format.
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
Non-allocating formatter: writes directly to destination
when possible via TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider).
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
Parameters
destinationSpan<char>charsWrittenintformatReadOnlySpan<char>providerIFormatProvider
Returns
TryParse(string?, out DateTime64)
public static bool TryParse(string? s, out DateTime64 result)
Parameters
sstringresultDateTime64
Returns
TryParse(string?, IFormatProvider?, DateTimeStyles, out DateTime64)
public static bool TryParse(string? s, IFormatProvider? provider, DateTimeStyles styles, out DateTime64 result)
Parameters
sstringproviderIFormatProviderstylesDateTimeStylesresultDateTime64
Returns
TryToDateTime(out DateTime)
public bool TryToDateTime(out DateTime result)
Parameters
resultDateTime
Returns
TryToDateTimeOffset(out DateTimeOffset)
Try to convert to DateTimeOffset.
public bool TryToDateTimeOffset(out DateTimeOffset result)
Parameters
resultDateTimeOffset
Returns
Operators
operator +(DateTime64, TimeSpan)
public static DateTime64 operator +(DateTime64 d, TimeSpan t)
Parameters
dDateTime64tTimeSpan
Returns
operator ==(DateTime64, DateTime64)
public static bool operator ==(DateTime64 d1, DateTime64 d2)
Parameters
d1DateTime64d2DateTime64
Returns
explicit operator DateTime(DateTime64)
Explicit narrowing to DateTime. Throws for NaT / out-of-range.
public static explicit operator DateTime(DateTime64 value)
Parameters
valueDateTime64
Returns
explicit operator DateTimeOffset(DateTime64)
Explicit narrowing to DateTimeOffset (UTC). Throws for NaT / out-of-range.
public static explicit operator DateTimeOffset(DateTime64 value)
Parameters
valueDateTime64
Returns
explicit operator long(DateTime64)
Explicit extraction of the raw int64 tick count.
public static explicit operator long(DateTime64 value)
Parameters
valueDateTime64
Returns
operator >(DateTime64, DateTime64)
public static bool operator >(DateTime64 d1, DateTime64 d2)
Parameters
d1DateTime64d2DateTime64
Returns
operator >=(DateTime64, DateTime64)
public static bool operator >=(DateTime64 d1, DateTime64 d2)
Parameters
d1DateTime64d2DateTime64
Returns
implicit operator DateTime64(DateTime)
Implicit widening from DateTime (drops Kind).
public static implicit operator DateTime64(DateTime value)
Parameters
valueDateTime
Returns
implicit operator DateTime64(DateTimeOffset)
Implicit widening from DateTimeOffset (via UtcTicks).
public static implicit operator DateTime64(DateTimeOffset value)
Parameters
valueDateTimeOffset
Returns
implicit operator DateTime64(long)
Implicit widening from long (raw tick count).
public static implicit operator DateTime64(long ticks)
Parameters
tickslong
Returns
operator !=(DateTime64, DateTime64)
public static bool operator !=(DateTime64 d1, DateTime64 d2)
Parameters
d1DateTime64d2DateTime64
Returns
operator <(DateTime64, DateTime64)
public static bool operator <(DateTime64 d1, DateTime64 d2)
Parameters
d1DateTime64d2DateTime64
Returns
operator <=(DateTime64, DateTime64)
public static bool operator <=(DateTime64 d1, DateTime64 d2)
Parameters
d1DateTime64d2DateTime64
Returns
operator -(DateTime64, DateTime64)
public static TimeSpan operator -(DateTime64 d1, DateTime64 d2)
Parameters
d1DateTime64d2DateTime64
Returns
operator -(DateTime64, TimeSpan)
public static DateTime64 operator -(DateTime64 d, TimeSpan t)
Parameters
dDateTime64tTimeSpan