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

Fix FbZonedDateTime invalid cast #1194

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public void EqualityFalse(FbZonedDateTime expected, FbZonedDateTime actual)
Assert.AreNotEqual(expected, actual);
}

[Test]
public void ConvertToDateTimeShouldNotThrow()
{
FbZonedDateTime fbZonedDateTime = new(new DateTime(2020, 12, 4, 10, 38, 0, DateTimeKind.Utc), "UTC");

Assert.DoesNotThrow(() => Convert.ChangeType(fbZonedDateTime, typeof(DateTime)));
}

public void DateTimeShouldBeUtc()
{
Assert.Throws<ArgumentException>(() =>
Expand Down
48 changes: 45 additions & 3 deletions src/FirebirdSql.Data.FirebirdClient/Types/FbZonedDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace FirebirdSql.Data.Types;

[StructLayout(LayoutKind.Auto)]
public readonly struct FbZonedDateTime : IEquatable<FbZonedDateTime>
public readonly struct FbZonedDateTime : IConvertible, IEquatable<FbZonedDateTime>
{
public DateTime DateTime { get; }
public string TimeZone { get; }
Expand Down Expand Up @@ -72,8 +72,50 @@ public override int GetHashCode()
}
}

public bool Equals(FbZonedDateTime other) => DateTime.Equals(other.DateTime) && TimeZone.Equals(other.TimeZone, StringComparison.OrdinalIgnoreCase);

public bool Equals(FbZonedDateTime other) => DateTime.Equals(other.DateTime) && TimeZone.Equals(other.TimeZone, StringComparison.OrdinalIgnoreCase);

TypeCode IConvertible.GetTypeCode() => TypeCode.Object;

bool IConvertible.ToBoolean(IFormatProvider provider) => throw new InvalidCastException(nameof(Boolean));

byte IConvertible.ToByte(IFormatProvider provider) => throw new InvalidCastException(nameof(Byte));

char IConvertible.ToChar(IFormatProvider provider) => throw new InvalidCastException(nameof(Char));

DateTime IConvertible.ToDateTime(IFormatProvider provider) => DateTime;

decimal IConvertible.ToDecimal(IFormatProvider provider) => throw new InvalidCastException(nameof(Decimal));

double IConvertible.ToDouble(IFormatProvider provider) => throw new InvalidCastException(nameof(Double));

short IConvertible.ToInt16(IFormatProvider provider) => throw new InvalidCastException(nameof(Int16));

int IConvertible.ToInt32(IFormatProvider provider) => throw new InvalidCastException(nameof(Int32));

long IConvertible.ToInt64(IFormatProvider provider) => throw new InvalidCastException(nameof(Int64));

sbyte IConvertible.ToSByte(IFormatProvider provider) => throw new InvalidCastException(nameof(SByte));

float IConvertible.ToSingle(IFormatProvider provider) => throw new InvalidCastException(nameof(Single));

string IConvertible.ToString(IFormatProvider provider) => ToString();

object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{
if (ReferenceEquals(conversionType, typeof(FbZonedDateTime)))
{
return this;
}

throw new InvalidCastException(conversionType?.FullName);
}

ushort IConvertible.ToUInt16(IFormatProvider provider) => throw new InvalidCastException(nameof(UInt16));

uint IConvertible.ToUInt32(IFormatProvider provider) => throw new InvalidCastException(nameof(UInt32));

ulong IConvertible.ToUInt64(IFormatProvider provider) => throw new InvalidCastException(nameof(UInt64));

public static bool operator ==(FbZonedDateTime lhs, FbZonedDateTime rhs) => lhs.Equals(rhs);

public static bool operator !=(FbZonedDateTime lhs, FbZonedDateTime rhs) => lhs.Equals(rhs);
Expand Down