@@ -6,6 +6,26 @@ namespace DuckDB.NET.Native;
66[ StructLayout ( LayoutKind . Sequential ) ]
77public readonly struct DuckDBDateOnly ( int year , byte month , byte day )
88{
9+ /// <summary>
10+ /// Represents positive infinity for DuckDB dates.
11+ /// </summary>
12+ public static readonly DuckDBDateOnly PositiveInfinity =
13+ // This is the value returned by DuckDB for positive infinity dates when
14+ // passed to duckdb_from_date, and it is used for backwards compatibility.
15+ // It is theoretically equal to the max date value plus one day:
16+ // '5881580-07-10'::date + 1
17+ new ( 5881580 , 7 , 11 ) ;
18+
19+ /// <summary>
20+ /// Represents negative infinity for DuckDB dates.
21+ /// </summary>
22+ public static readonly DuckDBDateOnly NegativeInfinity =
23+ // This is the value returned by DuckDB for negative infinity dates when
24+ // passed to duckdb_from_date, and it is used for backwards compatibility.
25+ // It is theoretically equal to the min date value minus one day:
26+ // '5877642-06-25 (BC)'::date - 1
27+ new ( - 5877641 , 6 , 24 ) ;
28+
929 public int Year { get ; } = year ;
1030
1131 public byte Month { get ; } = month ;
@@ -14,19 +34,68 @@ public readonly struct DuckDBDateOnly(int year, byte month, byte day)
1434
1535 internal static readonly DuckDBDateOnly MinValue = FromDateTime ( DateTime . MinValue ) ;
1636
37+ /// <summary>
38+ /// Returns true if this date represents positive or negative infinity.
39+ /// </summary>
40+ public bool IsInfinity => IsPositiveInfinity || IsNegativeInfinity ;
41+
42+ /// <summary>
43+ /// Returns true if this date represents positive infinity.
44+ /// </summary>
45+ public bool IsPositiveInfinity => Equals ( PositiveInfinity ) ;
46+
47+ /// <summary>
48+ /// Returns true if this date represents negative infinity.
49+ /// </summary>
50+ public bool IsNegativeInfinity => Equals ( NegativeInfinity ) ;
51+
1752 public static DuckDBDateOnly FromDateTime ( DateTime dateTime ) => new DuckDBDateOnly ( dateTime . Year , ( byte ) dateTime . Month , ( byte ) dateTime . Day ) ;
1853
1954 public DateTime ToDateTime ( ) => new DateTime ( Year , Month , Day ) ;
2055
56+ #if NET6_0_OR_GREATER
57+
58+ public static DuckDBDateOnly FromDateOnly ( DateOnly dateOnly ) => new DuckDBDateOnly ( dateOnly . Year , ( byte ) dateOnly . Month , ( byte ) dateOnly . Day ) ;
59+
60+ public DateOnly ToDateOnly ( ) => new DateOnly ( Year , Month , Day ) ;
61+
62+ #endif
63+
64+ /// <summary>
65+ /// Converts a DuckDBDate to DuckDBDateOnly, handling infinity values.
66+ /// </summary>
67+ public static DuckDBDateOnly FromDuckDBDate ( DuckDBDate date )
68+ {
69+ if ( date . IsPositiveInfinity )
70+ return PositiveInfinity ;
71+ if ( date . IsNegativeInfinity )
72+ return NegativeInfinity ;
73+
74+ return NativeMethods . DateTimeHelpers . DuckDBFromDate ( date ) ;
75+ }
76+
77+ /// <summary>
78+ /// Converts this DuckDBDateOnly to a DuckDBDate, handling infinity values.
79+ /// </summary>
80+ public DuckDBDate ToDuckDBDate ( )
81+ {
82+ if ( IsPositiveInfinity )
83+ return DuckDBDate . PositiveInfinity ;
84+ if ( IsNegativeInfinity )
85+ return DuckDBDate . NegativeInfinity ;
86+
87+ return NativeMethods . DateTimeHelpers . DuckDBToDate ( this ) ;
88+ }
89+
2190 public static explicit operator DateTime ( DuckDBDateOnly dateOnly ) => dateOnly . ToDateTime ( ) ;
22-
91+
2392 public static explicit operator DuckDBDateOnly ( DateTime dateTime ) => FromDateTime ( dateTime ) ;
24-
93+
2594#if NET6_0_OR_GREATER
26-
27- public static implicit operator DateOnly ( DuckDBDateOnly dateOnly ) => new DateOnly ( dateOnly . Year , dateOnly . Month , dateOnly . Day ) ;
28-
29- public static implicit operator DuckDBDateOnly ( DateOnly date ) => new DuckDBDateOnly ( date . Year , ( byte ) date . Month , ( byte ) date . Day ) ;
30-
95+
96+ public static implicit operator DateOnly ( DuckDBDateOnly dateOnly ) => dateOnly . ToDateOnly ( ) ;
97+
98+ public static implicit operator DuckDBDateOnly ( DateOnly date ) => DuckDBDateOnly . FromDateOnly ( date ) ;
99+
31100#endif
32- }
101+ }
0 commit comments