Skip to content

Bugfix #129 #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Tynamix.ObjectFiller.Test.BugfixTests
{
[TestClass]
public class Bug129WrongDateTimeGeneration
{
public class TestEntity
{
public DateTime Date { get; set; }
}

[TestMethod]
public void InvalidDateTimeValuesDueToDaylightSavingsTime()
{
Filler<TestEntity> filler = new Filler<TestEntity>();
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
filler.Setup().OnType<DateTime>().Use(new DateTimeRange(new DateTime(2007, 3, 11, 1, 0, 0, DateTimeKind.Unspecified), new DateTime(2007, 3, 11, 4, 00, 0, DateTimeKind.Unspecified), timeZoneInfo));

for (int i = 0; i < 1000; i++)
{
var result = filler.Create();
Assert.IsFalse(timeZoneInfo.IsInvalidTime(result.Date), $"{result.Date} is invalid");
}

filler = new Filler<TestEntity>();
filler.Setup().OnType<DateTime>().Use(new DateTimeRange(new DateTime(2022, 3, 27, 1, 0, 0, DateTimeKind.Unspecified), new DateTime(2022, 3, 27, 4, 00, 0, DateTimeKind.Unspecified)));

for (int i = 0; i < 1000; i++)
{
var result = filler.Create();
Assert.IsFalse(TimeZoneInfo.Local.IsInvalidTime(result.Date), $"{result.Date} is invalid");
}
}
}
}
59 changes: 57 additions & 2 deletions Tynamix.ObjectFiller/Plugins/DateTime/DateTimeRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@ public class DateTimeRange : IRandomizerPlugin<DateTime>, IRandomizerPlugin<Date
/// </summary>
private readonly DateTime latestDate;

/// <summary>
/// The target timezone to generate only valid date times
/// </summary>
private readonly TimeZoneInfo timeZoneInfo;

/// <summary>
/// Initializes a new instance of the <see cref="DateTimeRange"/> class.
/// </summary>
public DateTimeRange()
: this(TimeZoneInfo.Local)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DateTimeRange"/> class.
/// </summary>
/// <param name="timeZoneInfo">
/// The target timezone to generate only valid date times
/// </param>
public DateTimeRange(TimeZoneInfo timeZoneInfo)
: this(DateTime.MinValue, timeZoneInfo)
{
}

Expand All @@ -31,10 +48,26 @@ public DateTimeRange()
/// The earliest date.
/// </param>
public DateTimeRange(DateTime earliestDate)
: this(earliestDate, DateTime.Now)
: this(earliestDate, TimeZoneInfo.Local)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DateTimeRange"/> class.
/// </summary>
/// <param name="earliestDate">
/// The earliest date.
/// </param>
/// <param name="timeZoneInfo">
/// The target timezone to generate only valid date times
/// </param>
public DateTimeRange(DateTime earliestDate, TimeZoneInfo timeZoneInfo)
: this(earliestDate, DateTime.Now, timeZoneInfo)
{

}


/// <summary>
/// Initializes a new instance of the <see cref="DateTimeRange"/> class.
/// </summary>
Expand All @@ -45,7 +78,26 @@ public DateTimeRange(DateTime earliestDate)
/// The latest date.
/// </param>
public DateTimeRange(DateTime earliestDate, DateTime latestDate)
: this(earliestDate, latestDate, TimeZoneInfo.Local)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="DateTimeRange"/> class.
/// </summary>
/// <param name="earliestDate">
/// The earliest date.
/// </param>
/// <param name="latestDate">
/// The latest date.
/// </param>
/// <param name="timeZoneInfo">
/// The target timezone to generate only valid date times
/// </param>
public DateTimeRange(DateTime earliestDate, DateTime latestDate, TimeZoneInfo timeZoneInfo)
{
this.timeZoneInfo = timeZoneInfo;
if (earliestDate > latestDate)
{
this.latestDate = earliestDate;
Expand Down Expand Up @@ -73,7 +125,10 @@ public DateTime GetValue()

var diff = Random.NextLong(0, timeSpan.Ticks);

return this.latestDate.AddTicks(diff * -1);
var generatedDate = this.latestDate.AddTicks(diff * -1);
return this.timeZoneInfo.IsInvalidTime(generatedDate)
? GetValue()
: generatedDate;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Tynamix.ObjectFiller/Tynamix.ObjectFiller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.0;netstandard1.3;netstandard2.0;net35;net452;</TargetFrameworks>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<Version>1.5.6</Version>
<Version>1.5.7</Version>
<Authors>Roman Lautner, Hendrik L., Christian Harlass, GothikX</Authors>
<Company>Tynamix</Company>
<PackageLicenseUrl></PackageLicenseUrl>
Expand Down