Skip to content

Commit

Permalink
Create MySqlDateTime directly. Fixes #529
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Jul 6, 2018
1 parent 4d43f06 commit cefbf2a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/MySqlConnector/Core/Row.cs
Expand Up @@ -466,6 +466,7 @@ private static void CheckBufferArguments<T>(long dataOffset, T[] buffer, int buf

private object ParseDateTime(ReadOnlySpan<byte> value)
{
Exception exception = null;
if (!Utf8Parser.TryParse(value, out int year, out var bytesConsumed) || bytesConsumed != 4)
goto InvalidDateTime;
if (value.Length < 5 || value[4] != 45)
Expand Down Expand Up @@ -525,11 +526,18 @@ private object ParseDateTime(ReadOnlySpan<byte> value)
}
}

var dt = new DateTime(year, month, day, hour, minute, second, microseconds / 1000, Connection.DateTimeKind).AddTicks(microseconds % 1000 * 10);
return Connection.AllowZeroDateTime ? (object) new MySqlDateTime(dt) : dt;
try
{
return Connection.AllowZeroDateTime ? (object) new MySqlDateTime(year, month, day, hour, minute, second, microseconds) :
new DateTime(year, month, day, hour, minute, second, microseconds / 1000, Connection.DateTimeKind).AddTicks(microseconds % 1000 * 10);
}
catch (Exception ex)
{
exception = ex;
}

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

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

0 comments on commit cefbf2a

Please sign in to comment.