Skip to content

Commit

Permalink
Fix: DateTimePeriod persists DateTime.Kind of original DateTime (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb authored Jan 27, 2024
1 parent 23c33e6 commit 2103e81
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
15 changes: 15 additions & 0 deletions TournamentManager/TournamentManager.Tests/DateTimePeriodTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public void CreationAndAssigningNullable(DateTime? date1, DateTime? date2)
});
}

[Test]
public void Create_With_Date_Kind_Specified()
{
var date1 = new DateTime(2024, 06, 01, 18, 18, 18, DateTimeKind.Utc);
var date2 = new DateTime(2024, 06, 02, 18, 18, 18, DateTimeKind.Local);
var dtp = new DateTimePeriod(date1, date2);
Assert.Multiple(() =>
{
Assert.That(dtp.Start, Is.EqualTo(date1));
Assert.That(dtp.End, Is.EqualTo(date2));
Assert.That(dtp.Start?.Kind, Is.EqualTo(DateTimeKind.Utc));
Assert.That(dtp.End?.Kind, Is.EqualTo(DateTimeKind.Local));
});
}

[TestCase("2020-06-01", "2060-06-02")]
public void CreationAndAssigning(DateTime date1, DateTime date2)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using NUnit.Framework;
using TournamentManager.Importers.ExcludeDates;

namespace TournamentManager.Tests.Importers;

[TestFixture]
internal class ExcludeDateRecordTests
{
[Test]
public void Create_Should_Succeed()
{
var dateTime = DateTime.UtcNow;
// DateTimePeriod truncates milliseconds
var expected = new DateTime(
dateTime.Ticks - (dateTime.Ticks % TimeSpan.TicksPerSecond),
dateTime.Kind
);

var record = new ExcludeDateRecord {
Period = new DateTimePeriod(dateTime, dateTime.AddDays(1)),
Reason = "Any reason"
};

Assert.Multiple(() =>
{
Assert.That(record.Period.Start, Is.EqualTo(expected));
Assert.That(record.Period.End, Is.EqualTo(expected.AddDays(1)));
Assert.That(record.Reason, Is.EqualTo("Any reason"));
Assert.That(record.ToString(), Is.EqualTo($"{expected:yyyy-MM-dd HH:mm:ss} - {expected.AddDays(1):yyyy-MM-dd HH:mm:ss}: Any reason"));
});
}

[Test]
public void Conversion_To_ExcludeMatchDate_Should_Succeed()
{
var dateTime = new DateTime(2024, 1, 31, 12, 58, 59);

var record = new ExcludeDateRecord
{
Period = new DateTimePeriod(dateTime, dateTime.AddDays(1)),
Reason = "Any reason"
};

var excludeMatchDate = record.ToExcludeMatchDateEntity();

Assert.Multiple(() =>
{
Assert.That(excludeMatchDate.DateFrom, Is.EqualTo(dateTime));
Assert.That(excludeMatchDate.DateTo, Is.EqualTo(dateTime.AddDays(1)));
Assert.That(excludeMatchDate.Reason, Is.EqualTo("Any reason"));
});
}

[Test]
public void Conversion_With_Invalid_Data_Should_Fail()
{
var dateTime = DateTime.MinValue;

var record = new ExcludeDateRecord
{
Period = new DateTimePeriod(dateTime, dateTime),
Reason = ""
};

Assert.That(() => record.ToExcludeMatchDateEntity(), Throws.TypeOf<ArgumentException>());
}
}


20 changes: 14 additions & 6 deletions TournamentManager/TournamentManager/DateTimePeriod.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace TournamentManager;

/// <summary>
/// A struct to hold a period of time with start and end <see cref="DateTime"/> values.
/// Note: Start and End will truncate the time to full seconds. <see cref="DateTime.Kind"/> is preserved.
/// </summary>
public record struct DateTimePeriod
{
private DateTime? _start;
Expand All @@ -14,7 +18,8 @@ public DateTimePeriod()

/// <summary>
/// Creates a new <see cref="DateTimePeriod"/>.
/// When setting Start and End, it is ensured that always Start &lt;= End
/// When setting Start and End, it is ensured that always Start &lt;= End.
/// Note: Start and End will truncate the time to full seconds. <see cref="DateTime.Kind"/> is preserved.
/// </summary>
/// <param name="start">Start <see cref="DateTime"/></param>
/// <param name="end">End <see cref="DateTime"/></param>
Expand All @@ -26,7 +31,8 @@ public DateTimePeriod(DateTime? start, DateTime? end)

/// <summary>
/// Creates a new <see cref="DateTimePeriod"/>.
/// When setting Start and End, it is ensured that always Start &lt;= End
/// When setting Start and End, it is ensured that always Start &lt;= End.
/// Note: Start and End will truncate the time to full seconds. <see cref="DateTime.Kind"/> is preserved.
/// </summary>
/// <param name="start">Start <see cref="DateTime"/></param>
/// <param name="end">End <see cref="DateTime"/></param>
Expand All @@ -38,7 +44,8 @@ public DateTimePeriod(DateTime start, DateTime end)

/// <summary>
/// Creates a new <see cref="DateTimePeriod"/>
/// When setting Start and End, it is ensured that always Start &lt;= End
/// When setting Start and End, it is ensured that always Start &lt;= End.
/// Note: Start and End will truncate the time to full seconds. <see cref="DateTime.Kind"/> is preserved.
/// </summary>
/// <param name="start">Start <see cref="DateTime"/></param>
/// <param name="timeSpanToEnd">Timespan to end <see cref="TimeSpan"/></param>
Expand All @@ -53,7 +60,8 @@ public DateTimePeriod(DateTime? start, TimeSpan? timeSpanToEnd)

/// <summary>
/// Creates a new <see cref="DateTimePeriod"/>
/// When setting Start and End, it is ensured that always Start &lt;= End
/// When setting Start and End, it is ensured that always Start &lt;= End.
/// Note: Start and End will truncate the time to full seconds. <see cref="DateTime.Kind"/> is preserved.
/// </summary>
/// <param name="start">Start <see cref="DateTime"/></param>
/// <param name="timeSpanToEnd">Timespan to end <see cref="TimeSpan"/></param>
Expand Down Expand Up @@ -84,7 +92,7 @@ public DateTime? Start
else
{
var d = value.Value;
_start = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);
_start = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second, d.Kind);
}

SwapIfNeeded();
Expand Down Expand Up @@ -112,7 +120,7 @@ public DateTime? End
else
{
var d = value.Value;
_end = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);
_end = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second, d.Kind);
}

SwapIfNeeded();
Expand Down

0 comments on commit 2103e81

Please sign in to comment.