Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .NET 6 DateOnly and TimeOnly types #19

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Tomlyn.Tests/ModelTests/ReflectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public void TestPrimitives()
Float64Value = 2.5,
DateTime = new DateTime(1970, 1, 1),
DateTimeOffset = new DateTimeOffset(1980, 1, 1, 0, 23, 1, TimeSpan.FromHours(-2)),
DateOnly = new DateOnly(1970, 5, 27),
TimeOnly = new TimeOnly(7, 32, 0, 999),
TomlDateTime = new TomlDateTime(new DateTimeOffset(new DateTime(1990, 11, 15)), 0, TomlDateTimeKind.LocalDateTime)
};

Expand Down Expand Up @@ -244,13 +246,15 @@ public class PrimitiveModel : IEquatable<PrimitiveModel>
public double Float64Value { get; set; }
public DateTime DateTime { get; set; }
public DateTimeOffset DateTimeOffset { get; set; }
public DateOnly DateOnly { get; set; }
public TimeOnly TimeOnly { get; set; }
public TomlDateTime TomlDateTime { get; set; }

public bool Equals(PrimitiveModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Int8Value == other.Int8Value && Int16Value == other.Int16Value && Int32Value == other.Int32Value && Int64Value == other.Int64Value && UInt8Value == other.UInt8Value && UInt16Value == other.UInt16Value && UInt32Value == other.UInt32Value && UInt64Value == other.UInt64Value && Float32Value.Equals(other.Float32Value) && Float64Value.Equals(other.Float64Value) && DateTime.Equals(other.DateTime) && DateTimeOffset.Equals(other.DateTimeOffset) && TomlDateTime.Equals(other.TomlDateTime);
return Int8Value == other.Int8Value && Int16Value == other.Int16Value && Int32Value == other.Int32Value && Int64Value == other.Int64Value && UInt8Value == other.UInt8Value && UInt16Value == other.UInt16Value && UInt32Value == other.UInt32Value && UInt64Value == other.UInt64Value && Float32Value.Equals(other.Float32Value) && Float64Value.Equals(other.Float64Value) && DateTime.Equals(other.DateTime) && DateTimeOffset.Equals(other.DateTimeOffset) && DateOnly.Equals(other.DateOnly) && TimeOnly.Equals(other.TimeOnly) && TomlDateTime.Equals(other.TomlDateTime);
}

public override bool Equals(object? obj)
Expand All @@ -276,13 +280,15 @@ public override int GetHashCode()
hashCode.Add(Float64Value);
hashCode.Add(DateTime);
hashCode.Add(DateTimeOffset);
hashCode.Add(DateOnly);
hashCode.Add(TimeOnly);
hashCode.Add(TomlDateTime);
return hashCode.ToHashCode();
}

public override string ToString()
{
return $"{nameof(Int8Value)}: {Int8Value}, {nameof(Int16Value)}: {Int16Value}, {nameof(Int32Value)}: {Int32Value}, {nameof(Int64Value)}: {Int64Value}, {nameof(UInt8Value)}: {UInt8Value}, {nameof(UInt16Value)}: {UInt16Value}, {nameof(UInt32Value)}: {UInt32Value}, {nameof(UInt64Value)}: {UInt64Value}, {nameof(Float32Value)}: {Float32Value}, {nameof(Float64Value)}: {Float64Value}, {nameof(DateTime)}: {DateTime.ToUniversalTime()}, {nameof(DateTimeOffset)}: {DateTimeOffset.ToUniversalTime()}, {nameof(TomlDateTime)}: {TomlDateTime.DateTime.ToUniversalTime()}";
return $"{nameof(Int8Value)}: {Int8Value}, {nameof(Int16Value)}: {Int16Value}, {nameof(Int32Value)}: {Int32Value}, {nameof(Int64Value)}: {Int64Value}, {nameof(UInt8Value)}: {UInt8Value}, {nameof(UInt16Value)}: {UInt16Value}, {nameof(UInt32Value)}: {UInt32Value}, {nameof(UInt64Value)}: {UInt64Value}, {nameof(Float32Value)}: {Float32Value}, {nameof(Float64Value)}: {Float64Value}, {nameof(DateTime)}: {DateTime.ToUniversalTime()}, {nameof(DateTimeOffset)}: {DateTimeOffset.ToUniversalTime()}, {nameof(DateOnly)}: {DateOnly}, {nameof(TimeOnly)}: {TimeOnly}, {nameof(TomlDateTime)}: {TomlDateTime}";
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/Tomlyn/Helpers/TomlFormatHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ public static string ToString(DateTimeOffset dateTimeOffset, TomlPropertyDisplay
: displayKind)).ToString();
}

#if NET6_0_OR_GREATER
public static string ToString(DateOnly dateOnly, TomlPropertyDisplayKind displayKind)
{
return new TomlDateTime(dateOnly.ToDateTime(TimeOnly.MinValue), 0,
GetDateTimeDisplayKind(displayKind == TomlPropertyDisplayKind.Default
? TomlPropertyDisplayKind.LocalDate
: displayKind)).ToString();
}

public static string ToString(TimeOnly timeOnly, TomlPropertyDisplayKind displayKind)
{
return new TomlDateTime(DateOnly.MinValue.ToDateTime(timeOnly), 0,
GetDateTimeDisplayKind(displayKind == TomlPropertyDisplayKind.Default
? TomlPropertyDisplayKind.LocalTime
: displayKind)).ToString();
}
#endif

private static string AppendDecimalPoint(string text)
{
if (text == "0") return "0.0";
Expand Down
6 changes: 5 additions & 1 deletion src/Tomlyn/Model/Accessors/ReflectionObjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public ReflectionObjectInfo(ReflectionObjectKind kind, Type genericArgument1, Ty

public static ReflectionObjectInfo Get(Type type)
{
if (type == typeof(string) || type.IsPrimitive || type == typeof(TomlDateTime) || type == typeof(DateTime) || type == typeof(DateTimeOffset) || type == typeof(TomlDateTime))
if (type == typeof(string) || type.IsPrimitive || type == typeof(TomlDateTime) || type == typeof(DateTime) || type == typeof(DateTimeOffset)
#if NET6_0_OR_GREATER
|| type == typeof(DateOnly) || type == typeof(TimeOnly)
#endif
)
{
return new ReflectionObjectInfo(ReflectionObjectKind.Primitive);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Tomlyn/Model/ModelToTomlTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,16 @@ private void WritePrimitive(object primitive, TomlPropertyDisplayKind displayKin
{
_writer.Write(TomlFormatHelper.ToString(dateTimeOffset, displayKind));
}
#if NET6_0_OR_GREATER
else if (primitive is DateOnly dateOnly)
{
_writer.Write(TomlFormatHelper.ToString(dateOnly, displayKind));
}
else if (primitive is TimeOnly timeOnly)
{
_writer.Write(TomlFormatHelper.ToString(timeOnly, displayKind));
}
#endif
else
{
// Unexpected
Expand Down
12 changes: 12 additions & 0 deletions src/Tomlyn/TomlDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
return DateTime;
}

#if NET6_0_OR_GREATER
if (conversionType == typeof(DateOnly))
{
return DateOnly.FromDateTime(DateTime.DateTime);
}

if (conversionType == typeof(TimeOnly))
{
return TimeOnly.FromDateTime(DateTime.DateTime);
}
#endif

throw new InvalidCastException($"Unable to convert {nameof(TomlDateTime)} to destination type {conversionType.FullName}");
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tomlyn/Tomlyn.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VersionPrefix>0.9.1</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<Description>Tomlyn is a TOML parser, validator and authoring library for .NET.</Description>
<Copyright>Alexandre Mutel</Copyright>
Expand Down