From 3f96e704d140408a62c8964f753c20e08380971b Mon Sep 17 00:00:00 2001 From: "Oleg V. Kozlyuk" Date: Fri, 10 Mar 2023 14:07:57 +0300 Subject: [PATCH] Another piece for DateOnly puzzle (#282) --- ClickHouse.Client/Types/AbstractDateTimeType.cs | 4 ++++ ClickHouse.Client/Types/DateType.cs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/ClickHouse.Client/Types/AbstractDateTimeType.cs b/ClickHouse.Client/Types/AbstractDateTimeType.cs index 4311930e..e8100ffe 100644 --- a/ClickHouse.Client/Types/AbstractDateTimeType.cs +++ b/ClickHouse.Client/Types/AbstractDateTimeType.cs @@ -8,6 +8,10 @@ internal abstract class AbstractDateTimeType : ParameterizedType // DateTime.UnixEpoch is not available on .NET 4.8 public static readonly DateTime DateTimeEpochStart = DateTimeOffset.FromUnixTimeSeconds(0).UtcDateTime; +#if NET6_0_OR_GREATER + public static readonly DateOnly DateOnlyEpochStart = new DateOnly(1970,1,1); +#endif + public override Type FrameworkType => typeof(DateTime); public DateTimeZone TimeZone { get; set; } diff --git a/ClickHouse.Client/Types/DateType.cs b/ClickHouse.Client/Types/DateType.cs index b86cd645..ffd4e94c 100644 --- a/ClickHouse.Client/Types/DateType.cs +++ b/ClickHouse.Client/Types/DateType.cs @@ -16,6 +16,14 @@ internal class DateType : AbstractDateTimeType public override void Write(ExtendedBinaryWriter writer, object value) { +#if NET6_0_OR_GREATER + if (value is DateOnly @do) + { + var delta = @do.DayNumber - DateOnlyEpochStart.DayNumber; + writer.Write((ushort)delta); + return; + } +#endif var sinceEpoch = ((DateTime)value).Date - DateTimeEpochStart; writer.Write(Convert.ToUInt16(sinceEpoch.TotalDays)); }