Skip to content

Commit cefbf2a

Browse files
committed
Create MySqlDateTime directly. Fixes #529
1 parent 4d43f06 commit cefbf2a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ private static void CheckBufferArguments<T>(long dataOffset, T[] buffer, int buf
466466

467467
private object ParseDateTime(ReadOnlySpan<byte> value)
468468
{
469+
Exception exception = null;
469470
if (!Utf8Parser.TryParse(value, out int year, out var bytesConsumed) || bytesConsumed != 4)
470471
goto InvalidDateTime;
471472
if (value.Length < 5 || value[4] != 45)
@@ -525,11 +526,18 @@ private object ParseDateTime(ReadOnlySpan<byte> value)
525526
}
526527
}
527528

528-
var dt = new DateTime(year, month, day, hour, minute, second, microseconds / 1000, Connection.DateTimeKind).AddTicks(microseconds % 1000 * 10);
529-
return Connection.AllowZeroDateTime ? (object) new MySqlDateTime(dt) : dt;
529+
try
530+
{
531+
return Connection.AllowZeroDateTime ? (object) new MySqlDateTime(year, month, day, hour, minute, second, microseconds) :
532+
new DateTime(year, month, day, hour, minute, second, microseconds / 1000, Connection.DateTimeKind).AddTicks(microseconds % 1000 * 10);
533+
}
534+
catch (Exception ex)
535+
{
536+
exception = ex;
537+
}
530538

531539
InvalidDateTime:
532-
throw new FormatException("Couldn't interpret '{0}' as a valid DateTime".FormatInvariant(Encoding.UTF8.GetString(value)));
540+
throw new FormatException("Couldn't interpret '{0}' as a valid DateTime".FormatInvariant(Encoding.UTF8.GetString(value)), exception);
533541
}
534542

535543
private static Guid CreateGuidFromBytes(MySqlGuidFormat guidFormat, ReadOnlySpan<byte> bytes)

0 commit comments

Comments
 (0)