diff --git a/UnitsNet.Tests/CustomCode/DurationTests.cs b/UnitsNet.Tests/CustomCode/DurationTests.cs index 3fd943cea4..0ae975e379 100644 --- a/UnitsNet.Tests/CustomCode/DurationTests.cs +++ b/UnitsNet.Tests/CustomCode/DurationTests.cs @@ -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] @@ -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"); + } } } \ No newline at end of file diff --git a/UnitsNet/CustomCode/UnitClasses/Duration.extra.cs b/UnitsNet/CustomCode/UnitClasses/Duration.extra.cs index f52b655759..9588c84afd 100644 --- a/UnitsNet/CustomCode/UnitClasses/Duration.extra.cs +++ b/UnitsNet/CustomCode/UnitClasses/Duration.extra.cs @@ -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 ///