-
-
Notifications
You must be signed in to change notification settings - Fork 86
Infinity dates+timestamps #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
1df3e6a
23bbfc2
df8ed0c
35bb3c7
412f61e
496d7e5
b01a9f6
11bfc1c
e9e0265
c8db8b4
5fc278b
862f31e
5211704
3ce7409
d2273a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,40 @@ namespace DuckDB.NET.Native; | |
| [StructLayout(LayoutKind.Sequential)] | ||
| public readonly struct DuckDBTimestamp(DuckDBDateOnly date, DuckDBTimeOnly time) | ||
| { | ||
| /// <summary> | ||
| /// Represents positive infinity for DuckDB timestamps. | ||
| /// </summary> | ||
| public static readonly DuckDBTimestamp PositiveInfinity = | ||
| // The +infinity date value is not representable by the timestamp type, | ||
| // so this constant should never occur in normal usage | ||
| new(DuckDBDateOnly.PositiveInfinity, new DuckDBTimeOnly(0, 0, 0)); | ||
|
||
|
|
||
| /// <summary> | ||
| /// Represents negative infinity for DuckDB timestamps. | ||
| /// </summary> | ||
| public static readonly DuckDBTimestamp NegativeInfinity = | ||
| // The -infinity date value is not representable by the timestamp type, | ||
| // so this constant should never occur in normal usage | ||
| new(DuckDBDateOnly.NegativeInfinity, new DuckDBTimeOnly(0, 0, 0)); | ||
|
|
||
| public DuckDBDateOnly Date { get; } = date; | ||
| public DuckDBTimeOnly Time { get; } = time; | ||
|
|
||
| /// <summary> | ||
| /// Returns true if this timestamp represents positive or negative infinity. | ||
| /// </summary> | ||
| public bool IsInfinity => Date.IsInfinity; | ||
|
|
||
| /// <summary> | ||
| /// Returns true if this timestamp represents positive infinity. | ||
| /// </summary> | ||
| public bool IsPositiveInfinity => Date.IsPositiveInfinity; | ||
|
|
||
| /// <summary> | ||
| /// Returns true if this timestamp represents negative infinity. | ||
| /// </summary> | ||
| public bool IsNegativeInfinity => Date.IsNegativeInfinity; | ||
|
|
||
| public DateTime ToDateTime() | ||
| { | ||
| return new DateTime(Date.Year, Date.Month, Date.Day).AddTicks(Time.Ticks); | ||
|
|
@@ -18,4 +49,33 @@ public static DuckDBTimestamp FromDateTime(DateTime dateTime) | |
| { | ||
| return new DuckDBTimestamp(DuckDBDateOnly.FromDateTime(dateTime), DuckDBTimeOnly.FromDateTime(dateTime)); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts a DuckDBTimestampStruct to DuckDBTimestamp, handling infinity values. | ||
| /// </summary> | ||
| public static DuckDBTimestamp FromDuckDBTimestampStruct(DuckDBTimestampStruct timestampStruct) | ||
| { | ||
| if (timestampStruct.IsPositiveInfinity) | ||
| return PositiveInfinity; | ||
| if (timestampStruct.IsNegativeInfinity) | ||
| return NegativeInfinity; | ||
|
|
||
| return NativeMethods.DateTimeHelpers.DuckDBFromTimestamp(timestampStruct); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts this DuckDBTimestamp to a DuckDBTimestampStruct, handling infinity values. | ||
| /// </summary> | ||
| public DuckDBTimestampStruct ToDuckDBTimestampStruct() | ||
| { | ||
| if (IsPositiveInfinity) | ||
| return DuckDBTimestampStruct.PositiveInfinity; | ||
| if (IsNegativeInfinity) | ||
| return DuckDBTimestampStruct.NegativeInfinity; | ||
|
|
||
| return NativeMethods.DateTimeHelpers.DuckDBToTimestamp(this); | ||
| } | ||
|
|
||
| public static implicit operator DateTime(DuckDBTimestamp timestamp) => timestamp.ToDateTime(); | ||
| public static implicit operator DuckDBTimestamp(DateTime timestamp) => DuckDBTimestamp.FromDateTime(timestamp); | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.