-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathLocalDateConverter.cs
More file actions
47 lines (42 loc) · 1.58 KB
/
LocalDateConverter.cs
File metadata and controls
47 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using NodaTime;
using Npgsql.Internal;
using Npgsql.NodaTime.Properties;
namespace Npgsql.NodaTime.Internal;
sealed class LocalDateConverter(bool dateTimeInfinityConversions) : PgBufferedConverter<LocalDate>
{
public override bool CanConvert(DataFormat format, out BufferRequirements bufferRequirements)
{
bufferRequirements = BufferRequirements.CreateFixedSize(sizeof(int));
return format is DataFormat.Binary;
}
protected override LocalDate ReadCore(PgReader reader)
=> reader.ReadInt32() switch
{
int.MaxValue => dateTimeInfinityConversions
? LocalDate.MaxIsoValue
: throw new InvalidCastException(NpgsqlNodaTimeStrings.CannotReadInfinityValue),
int.MinValue => dateTimeInfinityConversions
? LocalDate.MinIsoValue
: throw new InvalidCastException(NpgsqlNodaTimeStrings.CannotReadInfinityValue),
var value => new LocalDate().PlusDays(value + 730119)
};
protected override void WriteCore(PgWriter writer, LocalDate value)
{
if (dateTimeInfinityConversions)
{
if (value == LocalDate.MaxIsoValue)
{
writer.WriteInt32(int.MaxValue);
return;
}
if (value == LocalDate.MinIsoValue)
{
writer.WriteInt32(int.MinValue);
return;
}
}
var totalDaysSinceEra = Period.Between(default, value, PeriodUnits.Days).Days;
writer.WriteInt32(totalDaysSinceEra - 730119);
}
}