Skip to content
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

Added equality/comparison overloads for Duration and TimeSpan #171

Merged
merged 2 commits into from
Jun 28, 2016
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
98 changes: 97 additions & 1 deletion UnitsNet.Tests/CustomCode/DurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void ToTimeSpanShouldNotThrowExceptionOnValuesSlightlyLargerThanTi
{
Duration duration = Duration.FromSeconds(TimeSpan.MinValue.TotalSeconds + 1);
TimeSpan timeSpan = duration.ToTimeSpan();
Assert.AreEqual(duration.Seconds, timeSpan.TotalSeconds,1e-3);
Assert.AreEqual(duration.Seconds, timeSpan.TotalSeconds, 1e-3);
}

[Test]
Expand Down Expand Up @@ -111,5 +111,101 @@ public static void DateTimeMinusDurationReturnsDateTime()
DateTime expected = new DateTime(2016, 1, 1);
Assert.AreEqual(expected, result);
}

[Test]
public static void TimeSpanLessThanDurationShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(10);
Duration duration = Duration.FromHours(11);
Assert.IsTrue(timeSpan < duration, "timeSpan should be less than duration");
}

[Test]
public static void TimeSpanGreaterThanDurationShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(12);
Duration duration = Duration.FromHours(11);
Assert.IsTrue(timeSpan > duration, "timeSpan should be greater than duration");
}

[Test]
public static void DurationLessThanTimeSpanShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(11);
Duration duration = Duration.FromHours(10);
Assert.IsTrue(duration < timeSpan, "duration should be less than timeSpan");
}

[Test]
public static void DurationGreaterThanTimeSpanShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(11);
Duration duration = Duration.FromHours(12);
Assert.IsTrue(duration > timeSpan, "duration should be greater than timeSpan");
}

[Test]
public static void TimeSpanLessOrEqualThanDurationShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(10);
Duration duration = Duration.FromHours(11);
Assert.IsTrue(timeSpan <= duration, "timeSpan should be less than duration");
}

[Test]
public static void TimeSpanGreaterThanOrEqualDurationShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(12);
Duration duration = Duration.FromHours(11);
Assert.IsTrue(timeSpan >= duration, "timeSpan should be greater than duration");
}

[Test]
public static void DurationLessThanOrEqualTimeSpanShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(11);
Duration duration = Duration.FromHours(10);
Assert.IsTrue(duration <= timeSpan, "duration should be less than timeSpan");
}

[Test]
public static void DurationGreaterThanOrEqualTimeSpanShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(11);
Duration duration = Duration.FromHours(12);
Assert.IsTrue(duration >= timeSpan, "duration should be greater than timeSpan");
}

[Test]
public static void DurationEqualToTimeSpanShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(11);
Duration duration = Duration.FromHours(11);
Assert.IsTrue(duration == timeSpan, "duration should be equal to timeSpan");
}

[Test]
public static void TimeSpanEqualToDurationShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(11);
Duration duration = Duration.FromHours(11);
Assert.IsTrue(timeSpan == duration, "timeSpan should be equal to duration");
}

[Test]
public static void DurationNotEqualToTimeSpanShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(12);
Duration duration = Duration.FromHours(11);
Assert.IsTrue(duration != timeSpan, "duration should not be equal to timeSpan");
}

[Test]
public static void TimeSpanNotEqualToDurationShouldReturnCorrectly()
{
TimeSpan timeSpan = TimeSpan.FromHours(12);
Duration duration = Duration.FromHours(11);
Assert.IsTrue(timeSpan != duration, "timeSpan should not be equal to duration");
}
}
}
60 changes: 60 additions & 0 deletions UnitsNet/CustomCode/UnitClasses/Duration.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,66 @@ public static explicit operator Duration(TimeSpan duration)
{
return FromSeconds(duration.TotalSeconds);
}

public static bool operator <(Duration duration, TimeSpan timeSpan)
{
return duration.Seconds < timeSpan.TotalSeconds;
}

public static bool operator >(Duration duration, TimeSpan timeSpan)
{
return duration.Seconds > timeSpan.TotalSeconds;
}

public static bool operator <=(Duration duration, TimeSpan timeSpan)
{
return duration.Seconds <= timeSpan.TotalSeconds;
}

public static bool operator >=(Duration duration, TimeSpan timeSpan)
{
return duration.Seconds >= timeSpan.TotalSeconds;
}

public static bool operator ==(Duration duration, TimeSpan timeSpan)
{
return duration.Seconds == timeSpan.TotalSeconds;
}

public static bool operator !=(Duration duration, TimeSpan timeSpan)
{
return duration.Seconds != timeSpan.TotalSeconds;
}

public static bool operator <(TimeSpan timeSpan, Duration duration)
{
return timeSpan.TotalSeconds < duration.Seconds;
}

public static bool operator >(TimeSpan timeSpan, Duration duration)
{
return timeSpan.TotalSeconds > duration.Seconds;
}

public static bool operator <=(TimeSpan timeSpan, Duration duration)
{
return timeSpan.TotalSeconds <= duration.Seconds;
}

public static bool operator >=(TimeSpan timeSpan, Duration duration)
{
return timeSpan.TotalSeconds >= duration.Seconds;
}

public static bool operator ==(TimeSpan timeSpan, Duration duration)
{
return timeSpan.TotalSeconds == duration.Seconds;
}

public static bool operator !=(TimeSpan timeSpan, Duration duration)
{
return timeSpan.TotalSeconds != duration.Seconds;
}
#endif

/// <summary>
Expand Down