Skip to content

Commit a1d366c

Browse files
authored
Remove the obsoleted provider-specific date/time types (#4138)
Closes #4020
1 parent e406cda commit a1d366c

File tree

21 files changed

+386
-2763
lines changed

21 files changed

+386
-2763
lines changed

src/Npgsql.NodaTime/Internal/DateHandler.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
using static Npgsql.NodaTime.Internal.NodaTimeUtils;
99
using BclDateHandler = Npgsql.Internal.TypeHandlers.DateTimeHandlers.DateHandler;
1010

11-
#pragma warning disable 618 // NpgsqlDate is obsolete, remove in 7.0
12-
1311
namespace Npgsql.NodaTime.Internal
1412
{
1513
sealed partial class DateHandler : NpgsqlSimpleTypeHandler<LocalDate>,
16-
INpgsqlSimpleTypeHandler<DateTime>, INpgsqlSimpleTypeHandler<NpgsqlDate>, INpgsqlSimpleTypeHandler<int>
14+
INpgsqlSimpleTypeHandler<DateTime>, INpgsqlSimpleTypeHandler<int>
1715
#if NET6_0_OR_GREATER
1816
, INpgsqlSimpleTypeHandler<DateOnly>
1917
#endif
@@ -57,15 +55,6 @@ public override void Write(LocalDate value, NpgsqlWriteBuffer buf, NpgsqlParamet
5755
buf.WriteInt32(totalDaysSinceEra - 730119);
5856
}
5957

60-
NpgsqlDate INpgsqlSimpleTypeHandler<NpgsqlDate>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
61-
=> _bclHandler.Read<NpgsqlDate>(buf, len, fieldDescription);
62-
63-
int INpgsqlSimpleTypeHandler<NpgsqlDate>.ValidateAndGetLength(NpgsqlDate value, NpgsqlParameter? parameter)
64-
=> _bclHandler.ValidateAndGetLength(value, parameter);
65-
66-
void INpgsqlSimpleTypeHandler<NpgsqlDate>.Write(NpgsqlDate value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
67-
=> _bclHandler.Write(value, buf, parameter);
68-
6958
DateTime INpgsqlSimpleTypeHandler<DateTime>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
7059
=> _bclHandler.Read<DateTime>(buf, len, fieldDescription);
7160

src/Npgsql.NodaTime/Internal/IntervalHandler.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
using NpgsqlTypes;
88
using BclIntervalHandler = Npgsql.Internal.TypeHandlers.DateTimeHandlers.IntervalHandler;
99

10-
#pragma warning disable 618 // NpgsqlTimeSpan is obsolete, remove in 7.0
11-
1210
namespace Npgsql.NodaTime.Internal
1311
{
1412
sealed partial class IntervalHandler :
1513
NpgsqlSimpleTypeHandler<Period>,
1614
INpgsqlSimpleTypeHandler<Duration>,
17-
INpgsqlSimpleTypeHandler<NpgsqlTimeSpan>,
1815
INpgsqlSimpleTypeHandler<TimeSpan>,
1916
INpgsqlSimpleTypeHandler<NpgsqlInterval>
2017
{
@@ -89,15 +86,6 @@ public void Write(Duration value, NpgsqlWriteBuffer buf, NpgsqlParameter? parame
8986
buf.WriteInt32(0); // months
9087
}
9188

92-
NpgsqlTimeSpan INpgsqlSimpleTypeHandler<NpgsqlTimeSpan>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
93-
=> _bclHandler.Read<NpgsqlTimeSpan>(buf, len, fieldDescription);
94-
95-
int INpgsqlSimpleTypeHandler<NpgsqlTimeSpan>.ValidateAndGetLength(NpgsqlTimeSpan value, NpgsqlParameter? parameter)
96-
=> _bclHandler.ValidateAndGetLength(value, parameter);
97-
98-
void INpgsqlSimpleTypeHandler<NpgsqlTimeSpan>.Write(NpgsqlTimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
99-
=> _bclHandler.Write(value, buf, parameter);
100-
10189
TimeSpan INpgsqlSimpleTypeHandler<TimeSpan>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
10290
=> _bclHandler.Read<TimeSpan>(buf, len, fieldDescription);
10391

src/Npgsql/Internal/TypeHandlers/DateTimeHandlers/DateHandler.cs

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using NpgsqlTypes;
66
using static Npgsql.Util.Statics;
77

8-
#pragma warning disable 618 // NpgsqlDate is obsolete, remove in 7.0
9-
108
namespace 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)
Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Runtime.CompilerServices;
3-
using Npgsql.BackendMessages;
4-
using NpgsqlTypes;
53
using static Npgsql.Util.Statics;
64

75
namespace Npgsql.Internal.TypeHandlers.DateTimeHandlers
@@ -38,43 +36,6 @@ internal static DateTime ReadDateTime(NpgsqlReadBuffer buf, DateTimeKind kind)
3836
}
3937
}
4038

41-
#pragma warning disable 618 // NpgsqlDateTime is obsolete, remove in 7.0
42-
internal static NpgsqlDateTime ReadNpgsqlDateTime(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
43-
{
44-
var value = buf.ReadInt64();
45-
if (value == long.MaxValue)
46-
return NpgsqlDateTime.Infinity;
47-
if (value == long.MinValue)
48-
return NpgsqlDateTime.NegativeInfinity;
49-
if (value >= 0)
50-
{
51-
var date = (int)(value / 86400000000L);
52-
var time = value % 86400000000L;
53-
54-
date += 730119; // 730119 = days since era (0001-01-01) for 2000-01-01
55-
time *= 10; // To 100ns
56-
57-
return new NpgsqlDateTime(new NpgsqlDate(date), new TimeSpan(time));
58-
}
59-
else
60-
{
61-
value = -value;
62-
var date = (int)(value / 86400000000L);
63-
var time = value % 86400000000L;
64-
if (time != 0)
65-
{
66-
++date;
67-
time = 86400000000L - time;
68-
}
69-
70-
date = 730119 - date; // 730119 = days since era (0001-01-01) for 2000-01-01
71-
time *= 10; // To 100ns
72-
73-
return new NpgsqlDateTime(new NpgsqlDate(date), new TimeSpan(time));
74-
}
75-
}
76-
#pragma warning restore 618
77-
7839
internal static void WriteTimestamp(DateTime value, NpgsqlWriteBuffer buf)
7940
{
8041
if (!DisableDateTimeInfinityConversions)
@@ -95,35 +56,5 @@ internal static void WriteTimestamp(DateTime value, NpgsqlWriteBuffer buf)
9556
var postgresTimestamp = EncodeTimestamp(value);
9657
buf.WriteInt64(postgresTimestamp);
9758
}
98-
99-
#pragma warning disable 618 // NpgsqlDateTime is obsolete, remove in 7.0
100-
internal static void WriteTimestamp(NpgsqlDateTime value, NpgsqlWriteBuffer buf)
101-
{
102-
if (value.IsInfinity)
103-
{
104-
buf.WriteInt64(long.MaxValue);
105-
return;
106-
}
107-
108-
if (value.IsNegativeInfinity)
109-
{
110-
buf.WriteInt64(long.MinValue);
111-
return;
112-
}
113-
114-
var uSecsTime = value.Time.Ticks / 10;
115-
116-
if (value >= new NpgsqlDateTime(2000, 1, 1, 0, 0, 0))
117-
{
118-
var uSecsDate = (value.Date.DaysSinceEra - 730119) * 86400000000L;
119-
buf.WriteInt64(uSecsDate + uSecsTime);
120-
}
121-
else
122-
{
123-
var uSecsDate = (730119 - value.Date.DaysSinceEra) * 86400000000L;
124-
buf.WriteInt64(uSecsTime - uSecsDate);
125-
}
126-
}
127-
#pragma warning restore 618
12859
}
12960
}

src/Npgsql/Internal/TypeHandlers/DateTimeHandlers/IntervalHandler.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using Npgsql.PostgresTypes;
55
using NpgsqlTypes;
66

7-
#pragma warning disable 618 // NpgsqlTimeSpan is obsolete, remove in 7.0
8-
97
namespace Npgsql.Internal.TypeHandlers.DateTimeHandlers
108
{
119
/// <summary>
@@ -18,8 +16,7 @@ namespace Npgsql.Internal.TypeHandlers.DateTimeHandlers
1816
/// should be considered somewhat unstable, and may change in breaking ways, including in non-major releases.
1917
/// Use it at your own risk.
2018
/// </remarks>
21-
public partial class IntervalHandler : NpgsqlSimpleTypeHandlerWithPsv<TimeSpan, NpgsqlTimeSpan>,
22-
INpgsqlSimpleTypeHandler<NpgsqlInterval>
19+
public partial class IntervalHandler : NpgsqlSimpleTypeHandler<TimeSpan>, INpgsqlSimpleTypeHandler<NpgsqlInterval>
2320
{
2421
/// <summary>
2522
/// Constructs an <see cref="IntervalHandler"/>
@@ -28,15 +25,15 @@ public IntervalHandler(PostgresType postgresType) : base(postgresType) {}
2825

2926
/// <inheritdoc />
3027
public override TimeSpan Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
31-
=> (TimeSpan)((INpgsqlSimpleTypeHandler<NpgsqlTimeSpan>)this).Read(buf, len, fieldDescription);
32-
33-
/// <inheritdoc />
34-
protected override NpgsqlTimeSpan ReadPsv(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
3528
{
36-
var ticks = buf.ReadInt64();
37-
var day = buf.ReadInt32();
38-
var month = buf.ReadInt32();
39-
return new NpgsqlTimeSpan(month, day, ticks * 10);
29+
var microseconds = buf.ReadInt64();
30+
var days = buf.ReadInt32();
31+
var months = buf.ReadInt32();
32+
33+
if (months > 0)
34+
throw new InvalidCastException("Cannot convert interval value with non-zero months to TimeSpan");
35+
36+
return new(microseconds * 10 + days * TimeSpan.TicksPerDay);
4037
}
4138

4239
NpgsqlInterval INpgsqlSimpleTypeHandler<NpgsqlInterval>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
@@ -50,25 +47,19 @@ NpgsqlInterval INpgsqlSimpleTypeHandler<NpgsqlInterval>.Read(NpgsqlReadBuffer bu
5047
/// <inheritdoc />
5148
public override int ValidateAndGetLength(TimeSpan value, NpgsqlParameter? parameter) => 16;
5249

53-
/// <inheritdoc />
54-
public override int ValidateAndGetLength(NpgsqlTimeSpan value, NpgsqlParameter? parameter) => 16;
55-
5650
/// <inheritdoc />
5751
public int ValidateAndGetLength(NpgsqlInterval value, NpgsqlParameter? parameter) => 16;
5852

5953
/// <inheritdoc />
60-
public override void Write(NpgsqlTimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
54+
public override void Write(TimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
6155
{
62-
buf.WriteInt64(value.Ticks / 10); // TODO: round?
56+
var ticksInDay = value.Ticks - TimeSpan.TicksPerDay * value.Days;
57+
58+
buf.WriteInt64(ticksInDay / 10);
6359
buf.WriteInt32(value.Days);
64-
buf.WriteInt32(value.Months);
60+
buf.WriteInt32(0);
6561
}
6662

67-
// TODO: Can write directly from TimeSpan
68-
/// <inheritdoc />
69-
public override void Write(TimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
70-
=> Write(value, buf, parameter);
71-
7263
public void Write(NpgsqlInterval value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
7364
{
7465
buf.WriteInt64(value.Time);

0 commit comments

Comments
 (0)