55using NpgsqlTypes ;
66using static Npgsql . Util . Statics ;
77
8- #pragma warning disable 618 // NpgsqlDate is obsolete, remove in 7.0
9-
108namespace Npgsql . Internal . TypeHandlers . DateTimeHandlers
119{
1210 /// <summary>
@@ -19,12 +17,15 @@ namespace Npgsql.Internal.TypeHandlers.DateTimeHandlers
1917 /// should be considered somewhat unstable, and may change in breaking ways, including in non-major releases.
2018 /// Use it at your own risk.
2119 /// </remarks>
22- public partial class DateHandler : NpgsqlSimpleTypeHandlerWithPsv < DateTime , NpgsqlDate > ,
23- INpgsqlSimpleTypeHandler < int >
20+ public partial class DateHandler : NpgsqlSimpleTypeHandler < DateTime > , INpgsqlSimpleTypeHandler < int >
2421#if NET6_0_OR_GREATER
2522 , INpgsqlSimpleTypeHandler < DateOnly >
2623#endif
2724 {
25+ const string InfinityExceptionMessage = "Can't read infinity value since Npgsql.DisableDateTimeInfinityConversions is enabled" ;
26+
27+ static readonly DateTime BaseValueDateTime = new ( 2000 , 1 , 1 , 0 , 0 , 0 ) ;
28+
2829 /// <summary>
2930 /// Constructs a <see cref="DateHandler"/>
3031 /// </summary>
@@ -34,32 +35,16 @@ public DateHandler(PostgresType postgresType) : base(postgresType) {}
3435
3536 /// <inheritdoc />
3637 public override DateTime Read ( NpgsqlReadBuffer buf , int len , FieldDescription ? fieldDescription = null )
37- {
38- var npgsqlDate = ReadPsv ( buf , len , fieldDescription ) ;
39-
40- if ( npgsqlDate . IsFinite )
41- return ( DateTime ) npgsqlDate ;
42- if ( DisableDateTimeInfinityConversions )
43- throw new InvalidCastException ( "Can't convert infinite date values to DateTime" ) ;
44- if ( npgsqlDate . IsInfinity )
45- return DateTime . MaxValue ;
46- return DateTime . MinValue ;
47- }
48-
49- /// <remarks>
50- /// Copied wholesale from Postgresql backend/utils/adt/datetime.c:j2date
51- /// </remarks>
52- protected override NpgsqlDate ReadPsv ( NpgsqlReadBuffer buf , int len , FieldDescription ? fieldDescription = null )
53- {
54- var binDate = buf . ReadInt32 ( ) ;
55-
56- return binDate switch
38+ => buf . ReadInt32 ( ) switch
5739 {
58- int . MaxValue => NpgsqlDate . Infinity ,
59- int . MinValue => NpgsqlDate . NegativeInfinity ,
60- _ => new NpgsqlDate ( binDate + 730119 )
40+ int . MaxValue => DisableDateTimeInfinityConversions
41+ ? throw new InvalidCastException ( InfinityExceptionMessage )
42+ : DateTime . MaxValue ,
43+ int . MinValue => DisableDateTimeInfinityConversions
44+ ? throw new InvalidCastException ( InfinityExceptionMessage )
45+ : DateTime . MinValue ,
46+ var value => BaseValueDateTime + TimeSpan . FromDays ( value )
6147 } ;
62- }
6348
6449 int INpgsqlSimpleTypeHandler < int > . Read ( NpgsqlReadBuffer buf , int len , FieldDescription ? fieldDescription )
6550 => buf . ReadInt32 ( ) ;
@@ -71,9 +56,6 @@ int INpgsqlSimpleTypeHandler<int>.Read(NpgsqlReadBuffer buf, int len, FieldDescr
7156 /// <inheritdoc />
7257 public override int ValidateAndGetLength ( DateTime value , NpgsqlParameter ? parameter ) => 4 ;
7358
74- /// <inheritdoc />
75- public override int ValidateAndGetLength ( NpgsqlDate value , NpgsqlParameter ? parameter ) => 4 ;
76-
7759 /// <inheritdoc />
7860 public int ValidateAndGetLength ( int value , NpgsqlParameter ? parameter ) => 4 ;
7961
@@ -84,29 +66,18 @@ public override void Write(DateTime value, NpgsqlWriteBuffer buf, NpgsqlParamete
8466 {
8567 if ( value == DateTime . MaxValue )
8668 {
87- Write ( NpgsqlDate . Infinity , buf , parameter ) ;
69+ buf . WriteInt32 ( int . MaxValue ) ;
8870 return ;
8971 }
9072
9173 if ( value == DateTime . MinValue )
9274 {
93- Write ( NpgsqlDate . NegativeInfinity , buf , parameter ) ;
75+ buf . WriteInt32 ( int . MinValue ) ;
9476 return ;
9577 }
9678 }
9779
98- Write ( new NpgsqlDate ( value ) , buf , parameter ) ;
99- }
100-
101- /// <inheritdoc />
102- public override void Write ( NpgsqlDate value , NpgsqlWriteBuffer buf , NpgsqlParameter ? parameter )
103- {
104- if ( value == NpgsqlDate . NegativeInfinity )
105- buf . WriteInt32 ( int . MinValue ) ;
106- else if ( value == NpgsqlDate . Infinity )
107- buf . WriteInt32 ( int . MaxValue ) ;
108- else
109- buf . WriteInt32 ( value . DaysSinceEra - 730119 ) ;
80+ buf . WriteInt32 ( ( value - BaseValueDateTime ) . Days ) ;
11081 }
11182
11283 /// <inheritdoc />
@@ -116,18 +87,19 @@ public void Write(int value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
11687 #endregion Write
11788
11889#if NET6_0_OR_GREATER
90+ static readonly DateOnly BaseValueDateOnly = new ( 2000 , 1 , 1 ) ;
91+
11992 DateOnly INpgsqlSimpleTypeHandler < DateOnly > . Read ( NpgsqlReadBuffer buf , int len , FieldDescription ? fieldDescription )
120- {
121- var npgsqlDate = ReadPsv ( buf , len , fieldDescription ) ;
122-
123- if ( npgsqlDate . IsFinite )
124- return ( DateOnly ) npgsqlDate ;
125- if ( DisableDateTimeInfinityConversions )
126- throw new InvalidCastException ( "Can't convert infinite date values to DateOnly" ) ;
127- if ( npgsqlDate . IsInfinity )
128- return DateOnly . MaxValue ;
129- return DateOnly . MinValue ;
130- }
93+ => buf . ReadInt32 ( ) switch
94+ {
95+ int . MaxValue => DisableDateTimeInfinityConversions
96+ ? throw new InvalidCastException ( InfinityExceptionMessage )
97+ : DateOnly . MaxValue ,
98+ int . MinValue => DisableDateTimeInfinityConversions
99+ ? throw new InvalidCastException ( InfinityExceptionMessage )
100+ : DateOnly . MinValue ,
101+ var value => BaseValueDateOnly . AddDays ( value )
102+ } ;
131103
132104 public int ValidateAndGetLength ( DateOnly value , NpgsqlParameter ? parameter ) => 4 ;
133105
@@ -137,18 +109,18 @@ public void Write(DateOnly value, NpgsqlWriteBuffer buf, NpgsqlParameter? parame
137109 {
138110 if ( value == DateOnly . MaxValue )
139111 {
140- Write ( NpgsqlDate . Infinity , buf , parameter ) ;
112+ buf . WriteInt32 ( int . MaxValue ) ;
141113 return ;
142114 }
143115
144116 if ( value == DateOnly . MinValue )
145117 {
146- Write ( NpgsqlDate . NegativeInfinity , buf , parameter ) ;
118+ buf . WriteInt32 ( int . MinValue ) ;
147119 return ;
148120 }
149121 }
150122
151- Write ( new NpgsqlDate ( value ) , buf , parameter ) ;
123+ buf . WriteInt32 ( value . DayNumber - BaseValueDateOnly . DayNumber ) ;
152124 }
153125
154126 public override NpgsqlTypeHandler CreateRangeHandler ( PostgresType pgRangeType )
0 commit comments