Skip to content

Commit e38d23f

Browse files
committed
Add MySqlDateTime and basic tests.
Signed-off-by: Bradley Grainger <[email protected]>
1 parent dce98de commit e38d23f

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
3+
namespace MySql.Data.Types
4+
{
5+
public struct MySqlDateTime : IComparable
6+
{
7+
public MySqlDateTime(int year, int month, int day, int hour, int minute, int second, int microsecond)
8+
{
9+
Year = year;
10+
Month = month;
11+
Day = day;
12+
Hour = hour;
13+
Minute = minute;
14+
Second = second;
15+
Microsecond = microsecond;
16+
}
17+
18+
public MySqlDateTime(DateTime dt)
19+
: this(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, (int) (dt.Ticks % 10_000_000) / 10)
20+
{
21+
}
22+
23+
public MySqlDateTime(MySqlDateTime other)
24+
{
25+
Year = other.Year;
26+
Month = other.Month;
27+
Day = other.Day;
28+
Hour = other.Hour;
29+
Minute = other.Minute;
30+
Second = other.Second;
31+
Microsecond = other.Microsecond;
32+
}
33+
34+
public bool IsValidDateTime => Year != 0 && Month != 0 && Day != 0;
35+
36+
public int Year { get; set; }
37+
public int Month { get; set; }
38+
public int Day { get; set; }
39+
public int Hour { get; set; }
40+
public int Minute { get; set; }
41+
public int Second { get; set; }
42+
public int Microsecond { get; set; }
43+
44+
public int Millisecond
45+
{
46+
get => Microsecond / 1000;
47+
set => Microsecond = value * 1000;
48+
}
49+
50+
public DateTime GetDateTime() =>
51+
!IsValidDateTime ? throw new InvalidOperationException("Cannot convert to DateTime when IsValidDateTime is false.") :
52+
new DateTime(Year, Month, Day, Hour, Minute, Second, DateTimeKind.Unspecified).AddTicks(Microsecond * 10);
53+
54+
public override string ToString() => IsValidDateTime ? GetDateTime().ToString() : "0000-00-00";
55+
56+
public static explicit operator DateTime(MySqlDateTime val) => !val.IsValidDateTime ? DateTime.MinValue : val.GetDateTime();
57+
58+
int IComparable.CompareTo(object obj)
59+
{
60+
if (!(obj is MySqlDateTime other))
61+
throw new ArgumentException("CompareTo can only be called with another MySqlDateTime", nameof(obj));
62+
63+
if (Year < other.Year)
64+
return -1;
65+
if (Year > other.Year)
66+
return 1;
67+
if (Month < other.Month)
68+
return -1;
69+
if (Month > other.Month)
70+
return 1;
71+
if (Day < other.Day)
72+
return -1;
73+
if (Day > other.Day)
74+
return 1;
75+
if (Hour < other.Hour)
76+
return -1;
77+
if (Hour > other.Hour)
78+
return 1;
79+
if (Minute < other.Minute)
80+
return -1;
81+
if (Minute > other.Minute)
82+
return 1;
83+
if (Second < other.Second)
84+
return -1;
85+
if (Second > other.Second)
86+
return 1;
87+
return Microsecond.CompareTo(other.Microsecond);
88+
}
89+
}
90+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using MySql.Data.Types;
3+
using Xunit;
4+
5+
namespace MySqlConnector.Tests
6+
{
7+
public class MySqlDateTimeTests
8+
{
9+
[Fact]
10+
public void NewMySqlDateTimeIsNotValidDateTime()
11+
{
12+
var msdt = new MySqlDateTime();
13+
Assert.False(msdt.IsValidDateTime);
14+
}
15+
16+
[Fact]
17+
public void ZeroMySqlDateTimeIsNotValidDateTime()
18+
{
19+
var msdt = new MySqlDateTime(0, 0, 0, 0, 0, 0, 0);
20+
Assert.False(msdt.IsValidDateTime);
21+
}
22+
23+
[Fact]
24+
public void NonZeroMySqlDateTimeIsValidDateTime()
25+
{
26+
var msdt = new MySqlDateTime(2018, 6, 9, 0, 0, 0, 0);
27+
Assert.True(msdt.IsValidDateTime);
28+
}
29+
30+
[Fact]
31+
public void CreateFromDateTime()
32+
{
33+
var msdt = new MySqlDateTime(new DateTime(2018, 6, 9, 12, 34, 56, 123).AddTicks(4560));
34+
Assert.True(msdt.IsValidDateTime);
35+
Assert.Equal(2018, msdt.Year);
36+
Assert.Equal(6, msdt.Month);
37+
Assert.Equal(9, msdt.Day);
38+
Assert.Equal(12, msdt.Hour);
39+
Assert.Equal(34, msdt.Minute);
40+
Assert.Equal(56, msdt.Second);
41+
Assert.Equal(123, msdt.Millisecond);
42+
Assert.Equal(123456, msdt.Microsecond);
43+
}
44+
45+
[Fact]
46+
public void GetDateTime()
47+
{
48+
var msdt = new MySqlDateTime(2018, 6, 9, 12, 34, 56, 123456);
49+
Assert.True(msdt.IsValidDateTime);
50+
var dt = msdt.GetDateTime();
51+
Assert.Equal(new DateTime(2018, 6, 9, 12, 34, 56, 123).AddTicks(4560), dt);
52+
}
53+
54+
[Fact]
55+
public void SetMicrosecond()
56+
{
57+
var msdt = new MySqlDateTime();
58+
Assert.Equal(0, msdt.Microsecond);
59+
msdt.Microsecond = 123456;
60+
Assert.Equal(123, msdt.Millisecond);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)