Skip to content

Commit 4460be8

Browse files
ejballbgrainger
authored andcommitted
Use DateTimeKind for all returned dates and date/times.
1 parent 00fb13c commit 4460be8

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ env:
1717
OMIT_FEATURES=CachingSha2Password
1818
- IMAGE=mariadb:10.2
1919
NAME=mariadb
20-
OMIT_FEATURES=Json,Sha256Password,CachingSha2Password
20+
OMIT_FEATURES=Json,Sha256Password,CachingSha2Password,RoundDateTime
2121

2222
before_install:
2323
- .ci/docker-run.sh $IMAGE $NAME 3307 $OMIT_FEATURES

src/MySqlConnector/Core/Row.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,13 @@ private DateTime ParseDateTime(ArraySegment<byte> value)
447447
}
448448

449449
if (parts.Length == 3)
450-
return new DateTime(year, month, day);
450+
return new DateTime(year, month, day, 0, 0, 0, Connection.DateTimeKind);
451451

452452
var hour = int.Parse(parts[3], CultureInfo.InvariantCulture);
453453
var minute = int.Parse(parts[4], CultureInfo.InvariantCulture);
454454
var second = int.Parse(parts[5], CultureInfo.InvariantCulture);
455455
if (parts.Length == 6)
456-
return new DateTime(year, month, day, hour, minute, second);
456+
return new DateTime(year, month, day, hour, minute, second, Connection.DateTimeKind);
457457

458458
var microseconds = int.Parse(parts[6] + new string('0', 6 - parts[6].Length), CultureInfo.InvariantCulture);
459459
return new DateTime(year, month, day, hour, minute, second, microseconds / 1000, Connection.DateTimeKind).AddTicks(microseconds % 1000 * 10);

tests/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Otherwise, set the following options appropriately:
3232
* `ErrorCodes`: server returns error codes in error packet (some MySQL proxies do not)
3333
* `Tls11`: server supports TLS 1.1
3434
* `Tls12`: server supports TLS 1.2
35+
* `RoundDateTime`: server rounds `datetime` values to the specified precision (not implemented in MariaDB)
3536

3637
## Running Tests
3738

tests/SideBySide/DataTypes.cs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,22 +568,52 @@ public void QueryDateTimeKind(MySqlDateTimeKind kindOption, DateTimeKind kindIn,
568568
{
569569
connection.Open();
570570

571-
var dateTimeIn = new DateTime(2002, 3, 4, 5, 6, 7, 890, kindIn);
571+
var dateTimeIn = new DateTime(2001, 2, 3, 14, 5, 6, 789, kindIn);
572572
using (var cmd = new MySqlCommand(@"drop table if exists date_time_kind;
573-
create table date_time_kind(rowid integer not null primary key auto_increment, dt datetime(3) not null);
574-
insert into date_time_kind(dt) values(?)", connection)
573+
create table date_time_kind(
574+
rowid integer not null primary key auto_increment,
575+
d date,
576+
dt0 datetime(0),
577+
dt1 datetime(1),
578+
dt2 datetime(2),
579+
dt3 datetime(3),
580+
dt4 datetime(4),
581+
dt5 datetime(5),
582+
dt6 datetime(6));
583+
insert into date_time_kind(d, dt0, dt1, dt2, dt3, dt4, dt5, dt6) values(?, ?, ?, ?, ?, ?, ?, ?)", connection)
575584
{
576-
Parameters = { new MySqlParameter { Value = dateTimeIn } }
585+
Parameters =
586+
{
587+
new MySqlParameter { Value = dateTimeIn },
588+
new MySqlParameter { Value = dateTimeIn },
589+
new MySqlParameter { Value = dateTimeIn },
590+
new MySqlParameter { Value = dateTimeIn },
591+
new MySqlParameter { Value = dateTimeIn },
592+
new MySqlParameter { Value = dateTimeIn },
593+
new MySqlParameter { Value = dateTimeIn },
594+
new MySqlParameter { Value = dateTimeIn },
595+
}
577596
})
578597
{
579598
if (success)
580599
{
581600
cmd.ExecuteNonQuery();
582601
long lastInsertId = cmd.LastInsertedId;
583-
cmd.CommandText = $"select dt from date_time_kind where rowid = {lastInsertId};";
584-
var dateTimeOut = (DateTime?) cmd.ExecuteScalar();
585-
Assert.Equal(dateTimeIn, dateTimeOut);
586-
Assert.Equal(kindOption, (MySqlDateTimeKind) dateTimeOut.Value.Kind);
602+
cmd.CommandText = $"select d, dt0, dt1, dt2, dt3, dt4, dt5, dt6 from date_time_kind where rowid = {lastInsertId};";
603+
using (var reader = cmd.ExecuteReader())
604+
{
605+
Assert.True(reader.Read());
606+
Assert.Equal(new DateTime(2001, 2, 3), reader.GetValue(0));
607+
Assert.Equal(new DateTime(2001, 2, 3, 14, 5, AppConfig.SupportedFeatures.HasFlag(ServerFeatures.RoundDateTime) ? 7 : 6, kindIn), reader.GetValue(1));
608+
Assert.Equal(new DateTime(2001, 2, 3, 14, 5, 6, AppConfig.SupportedFeatures.HasFlag(ServerFeatures.RoundDateTime) ? 800 : 700, kindIn), reader.GetValue(2));
609+
Assert.Equal(new DateTime(2001, 2, 3, 14, 5, 6, AppConfig.SupportedFeatures.HasFlag(ServerFeatures.RoundDateTime) ? 790 : 780, kindIn), reader.GetValue(3));
610+
Assert.Equal(dateTimeIn, reader.GetValue(4));
611+
Assert.Equal(dateTimeIn, reader.GetValue(5));
612+
Assert.Equal(dateTimeIn, reader.GetValue(6));
613+
Assert.Equal(dateTimeIn, reader.GetValue(7));
614+
for (int i = 0; i < 7; i++)
615+
Assert.Equal(kindOption, (MySqlDateTimeKind) reader.GetDateTime(i).Kind);
616+
}
587617
}
588618
else
589619
{

tests/SideBySide/ServerFeatures.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public enum ServerFeatures
1818
KnownCertificateAuthority = 512,
1919
Tls11 = 1024,
2020
Tls12 = 2048,
21+
RoundDateTime = 4096,
2122
}
2223
}

0 commit comments

Comments
 (0)