Skip to content

Commit 10706ca

Browse files
Copilotniemyjski
andcommitted
Implement SubtractSaturating method for TimeSpan with comprehensive tests
Co-authored-by: niemyjski <[email protected]>
1 parent 9dcbef7 commit 10706ca

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Exceptionless.DateTimeExtensions/TimeSpanExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public static AgeSpan ToAgeSpan(this TimeSpan span)
8888
{
8989
return new AgeSpan(span);
9090
}
91+
92+
public static TimeSpan SubtractSaturating(this TimeSpan self, TimeSpan other)
93+
{
94+
return self >= other ? self.Subtract(other) : TimeSpan.Zero;
95+
}
9196
}
9297

9398
public struct AgeSpan

tests/Exceptionless.DateTimeExtensions.Tests/TimeSpanExtensionTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,43 @@ public void ApproximateAge()
6565
Assert.Equal("2 days ago", DateTime.Now.AddHours(-48).ToApproximateAgeString());
6666
Assert.Equal("1 week ago", DateTime.Now.AddDays(-7).ToApproximateAgeString());
6767
}
68+
69+
[Fact]
70+
public void SubtractSaturating()
71+
{
72+
// Basic case: normal subtraction when result is positive
73+
var timeSpan1 = TimeSpan.FromHours(5);
74+
var timeSpan2 = TimeSpan.FromHours(2);
75+
Assert.Equal(TimeSpan.FromHours(3), timeSpan1.SubtractSaturating(timeSpan2));
76+
77+
// Saturating case: result would be negative, should return TimeSpan.Zero
78+
var timeSpan3 = TimeSpan.FromHours(2);
79+
var timeSpan4 = TimeSpan.FromHours(5);
80+
Assert.Equal(TimeSpan.Zero, timeSpan3.SubtractSaturating(timeSpan4));
81+
82+
// Edge case: equal values should return TimeSpan.Zero
83+
var timeSpan5 = TimeSpan.FromMinutes(30);
84+
var timeSpan6 = TimeSpan.FromMinutes(30);
85+
Assert.Equal(TimeSpan.Zero, timeSpan5.SubtractSaturating(timeSpan6));
86+
87+
// Zero cases
88+
Assert.Equal(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10).SubtractSaturating(TimeSpan.Zero));
89+
Assert.Equal(TimeSpan.Zero, TimeSpan.Zero.SubtractSaturating(TimeSpan.FromSeconds(5)));
90+
Assert.Equal(TimeSpan.Zero, TimeSpan.Zero.SubtractSaturating(TimeSpan.Zero));
91+
92+
// Large values
93+
var largeTimeSpan1 = TimeSpan.FromDays(100);
94+
var largeTimeSpan2 = TimeSpan.FromDays(50);
95+
Assert.Equal(TimeSpan.FromDays(50), largeTimeSpan1.SubtractSaturating(largeTimeSpan2));
96+
97+
// Negative TimeSpan values - testing with negative self value
98+
var negativeTimeSpan = TimeSpan.FromHours(-2);
99+
var positiveTimeSpan = TimeSpan.FromHours(1);
100+
Assert.Equal(TimeSpan.Zero, negativeTimeSpan.SubtractSaturating(positiveTimeSpan));
101+
102+
// Negative other value (subtracting negative is like adding)
103+
var timeSpan7 = TimeSpan.FromHours(3);
104+
var negativeTimeSpan2 = TimeSpan.FromHours(-2);
105+
Assert.Equal(TimeSpan.FromHours(5), timeSpan7.SubtractSaturating(negativeTimeSpan2));
106+
}
68107
}

0 commit comments

Comments
 (0)