Skip to content

Commit 26dab31

Browse files
committed
Fix GetDateTime when AllowZeroDateTime=true. Fixes #597
1 parent 44df1ff commit 26dab31

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,13 @@ public ulong GetUInt64(int ordinal)
277277
return (ulong) value;
278278
}
279279

280-
public DateTime GetDateTime(int ordinal) => (DateTime) GetValue(ordinal);
280+
public DateTime GetDateTime(int ordinal)
281+
{
282+
var value = GetValue(ordinal);
283+
if (value is MySqlDateTime mySqlDateTime)
284+
return mySqlDateTime.GetDateTime();
285+
return (DateTime) value;
286+
}
281287

282288
public DateTimeOffset GetDateTimeOffset(int ordinal) => new DateTimeOffset(DateTime.SpecifyKind(GetDateTime(ordinal), DateTimeKind.Utc));
283289

tests/SideBySide/DataTypes.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -930,22 +930,22 @@ public void InsertLargeBlobSync(string column, int size)
930930
}
931931

932932
[Theory]
933-
[InlineData(false, "Date", typeof(DateTime))]
934-
[InlineData(true, "Date", typeof(MySqlDateTime))]
935-
[InlineData(false, "DateTime", typeof(DateTime))]
936-
[InlineData(true, "DateTime", typeof(MySqlDateTime))]
937-
[InlineData(false, "TimeStamp", typeof(DateTime))]
938-
[InlineData(true, "TimeStamp", typeof(MySqlDateTime))]
939-
[InlineData(false, "Time", typeof(TimeSpan))]
940-
[InlineData(true, "Time", typeof(TimeSpan))]
941-
public void AllowZeroDateTime(bool allowZeroDateTime, string columnName, Type expectedType)
933+
[InlineData(false, "Date", typeof(DateTime), "1000 01 01")]
934+
[InlineData(true, "Date", typeof(MySqlDateTime), "1000 01 01")]
935+
[InlineData(false, "DateTime", typeof(DateTime), "1000 01 01")]
936+
[InlineData(true, "DateTime", typeof(MySqlDateTime), "1000 01 01")]
937+
[InlineData(false, "TimeStamp", typeof(DateTime), "1970 01 01 0 0 1")]
938+
[InlineData(true, "TimeStamp", typeof(MySqlDateTime), "1970 01 01 0 0 1")]
939+
[InlineData(false, "Time", typeof(TimeSpan), null)]
940+
[InlineData(true, "Time", typeof(TimeSpan), null)]
941+
public void AllowZeroDateTime(bool allowZeroDateTime, string columnName, Type expectedType, string expectedDateTime)
942942
{
943943
var csb = CreateConnectionStringBuilder();
944944
csb.AllowZeroDateTime = allowZeroDateTime;
945945
using (var connection = new MySqlConnection(csb.ConnectionString))
946946
{
947947
connection.Open();
948-
using (var cmd = new MySqlCommand($"SELECT `{columnName}` FROM datatypes_times WHERE `{columnName}` IS NOT NULL", connection))
948+
using (var cmd = new MySqlCommand($"SELECT `{columnName}` FROM datatypes_times WHERE `{columnName}` IS NOT NULL ORDER BY rowid", connection))
949949
{
950950
cmd.Prepare();
951951
using (var reader = cmd.ExecuteReader())
@@ -957,6 +957,13 @@ public void AllowZeroDateTime(bool allowZeroDateTime, string columnName, Type ex
957957
var dt = reader.GetSchemaTable();
958958
Assert.Equal(expectedType, dt.Rows[0]["DataType"]);
959959
#endif
960+
961+
if (expectedDateTime != null)
962+
{
963+
var expected = (DateTime) ConvertToDateTime(new object[] { expectedDateTime }, DateTimeKind.Unspecified)[0];
964+
Assert.Equal(expected, reader.GetDateTime(0));
965+
Assert.Equal(new MySqlDateTime(expected), reader.GetMySqlDateTime(0));
966+
}
960967
}
961968
}
962969
}

0 commit comments

Comments
 (0)