Skip to content

Commit

Permalink
Use DateOnly by default on .net6
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkWanderer committed Feb 15, 2024
1 parent 4818468 commit dc802fa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
9 changes: 9 additions & 0 deletions ClickHouse.Client.Tests/TestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ public static IEnumerable<DataTypeSample> GetDataTypeSamples()
yield return new DataTypeSample("Tuple(Int32, Tuple(UInt8, String, Nullable(Int32)))", typeof(Tuple<int, Tuple<byte, string, int?>>), "tuple(123, tuple(5, 'a', 7))", Tuple.Create(123, Tuple.Create((byte)5, "a", 7)));
yield return new DataTypeSample("Tuple(a Int32, b Int32)", typeof(Tuple<int, ushort>), "tuple(123, 456)", Tuple.Create(123, 456));

#if NET6_0_OR_GREATER
yield return new DataTypeSample("Date", typeof(DateOnly), "toDateOrNull('1999-11-12')", new DateOnly(1999, 11, 12));
#else
yield return new DataTypeSample("Date", typeof(DateTime), "toDateOrNull('1999-11-12')", new DateTime(1999, 11, 12, 0, 0, 0, DateTimeKind.Unspecified));
#endif
yield return new DataTypeSample("DateTime('UTC')", typeof(DateTime), "toDateTime('1988-08-28 11:22:33', 'UTC')", new DateTime(1988, 08, 28, 11, 22, 33, DateTimeKind.Unspecified));
yield return new DataTypeSample("DateTime('Pacific/Fiji')", typeof(DateTime), "toDateTime('1999-01-01 13:00:00', 'Pacific/Fiji')", new DateTime(1999, 01, 01, 13, 00, 00, DateTimeKind.Unspecified));

Expand Down Expand Up @@ -190,8 +194,13 @@ public static IEnumerable<DataTypeSample> GetDataTypeSamples()

if (SupportedFeatures.HasFlag(Feature.Date32))
{
#if NET6_0_OR_GREATER
yield return new DataTypeSample("Date32", typeof(DateTime), "toDate32('2001-02-03')", new DateOnly(2001, 02, 03));
yield return new DataTypeSample("Date32", typeof(DateTime), "toDate32('1925-01-02')", new DateOnly(1925, 01, 02));
#else
yield return new DataTypeSample("Date32", typeof(DateTime), "toDate32('2001-02-03')", new DateTime(2001, 02, 03));
yield return new DataTypeSample("Date32", typeof(DateTime), "toDate32('1925-01-02')", new DateTime(1925, 01, 02));
#endif
}

if (SupportedFeatures.HasFlag(Feature.WideTypes))
Expand Down
6 changes: 6 additions & 0 deletions ClickHouse.Client.Tests/Types/TypeMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ public class TypeMappingTests

[TestCase("LowCardinality(String)", ExpectedResult = typeof(string))]

#if NET6_0_OR_GREATER
[TestCase("Date", ExpectedResult = typeof(DateOnly))]
[TestCase("Date32", ExpectedResult = typeof(DateOnly))]
#else
[TestCase("Date", ExpectedResult = typeof(DateTime))]
[TestCase("Date32", ExpectedResult = typeof(DateTime))]
#endif
[TestCase("DateTime", ExpectedResult = typeof(DateTime))]
[TestCase("DateTime('Etc/UTC')", ExpectedResult = typeof(DateTime))]
[TestCase("DateTime64(3)", ExpectedResult = typeof(DateTime))]
Expand Down
12 changes: 12 additions & 0 deletions ClickHouse.Client/Types/Date32Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ internal class Date32Type : DateType
public override object Read(ExtendedBinaryReader reader)
{
var days = reader.ReadInt32();
#if NET6_0_OR_GREATER
return DateOnlyEpochStart.AddDays(days);
#else
return DateTimeEpochStart.AddDays(days);
#endif
}

public override ParameterizedType Parse(SyntaxTreeNode typeName, Func<SyntaxTreeNode, ClickHouseType> parseClickHouseTypeFunc, TypeSettings settings) => throw new NotImplementedException();

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((int)delta);
return;
}
#endif
var sinceEpoch = ((DateTime)value).Date - DateTimeEpochStart;
writer.Write(Convert.ToInt32(sinceEpoch.TotalDays));
}
Expand Down
13 changes: 12 additions & 1 deletion ClickHouse.Client/Types/DateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ namespace ClickHouse.Client.Types;

internal class DateType : AbstractDateTimeType
{
#if NET6_0_OR_GREATER
public override Type FrameworkType => typeof(DateOnly);
#endif

public override string Name { get; }

public override string ToString() => "Date";

public override object Read(ExtendedBinaryReader reader) => DateTimeEpochStart.AddDays(reader.ReadUInt16());
public override object Read(ExtendedBinaryReader reader)
{
#if NET6_0_OR_GREATER
return DateOnlyEpochStart.AddDays(reader.ReadUInt16());
#else
return DateTimeEpochStart.AddDays(reader.ReadUInt16());
#endif
}

public override ParameterizedType Parse(SyntaxTreeNode typeName, Func<SyntaxTreeNode, ClickHouseType> parseClickHouseTypeFunc, TypeSettings settings) => throw new NotImplementedException();

Expand Down
3 changes: 0 additions & 3 deletions ClickHouse.Client/Types/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ static TypeConverter()
// Mapping fixups
ReverseMapping.Add(typeof(ClickHouseDecimal), new Decimal128Type());
ReverseMapping.Add(typeof(decimal), new Decimal128Type());
#if NET6_0_OR_GREATER
ReverseMapping.Add(typeof(DateOnly), new DateType());
#endif
ReverseMapping[typeof(DateTime)] = new DateTimeType();
ReverseMapping[typeof(DateTimeOffset)] = new DateTimeType();
}
Expand Down

0 comments on commit dc802fa

Please sign in to comment.