Skip to content

Fix FbZonedDateTime invalid cast #1194

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

Merged
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
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()
{
var fbZonedDateTime = new FbZonedDateTime(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
41 changes: 38 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 : IEquatable<FbZonedDateTime>, IConvertible
{
public DateTime DateTime { get; }
public string TimeZone { get; }
Expand Down Expand Up @@ -72,8 +72,43 @@ 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;

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

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

object IConvertible.ToType(Type conversionType, IFormatProvider provider)
=> ReferenceEquals(conversionType, typeof(FbZonedDateTime)) ? this : throw new InvalidCastException(conversionType?.FullName);

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));

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));

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