Skip to content

Commit

Permalink
Another piece for DateOnly puzzle (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkWanderer authored Mar 10, 2023
1 parent fabff4f commit 3f96e70
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ClickHouse.Client/Types/AbstractDateTimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
8 changes: 8 additions & 0 deletions ClickHouse.Client/Types/DateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 3f96e70

Please sign in to comment.