Skip to content

Commit

Permalink
Fix FbZonedDateTime invalid cast (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
willibrandon authored Oct 17, 2024
1 parent 795b51b commit c5b9fb9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
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

0 comments on commit c5b9fb9

Please sign in to comment.