Skip to content

Commit 21bc0eb

Browse files
Copilotniemyjski
andauthored
Add SubtractSaturating extension method for TimeSpan (#114)
* Initial plan * Implement SubtractSaturating method for TimeSpan with comprehensive tests Co-authored-by: niemyjski <[email protected]> * Add XML documentation to SubtractSaturating method Co-authored-by: niemyjski <[email protected]> * Refactor SubtractSaturating tests to follow best practice naming convention Co-authored-by: niemyjski <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: niemyjski <[email protected]>
1 parent e246279 commit 21bc0eb

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/Exceptionless.DateTimeExtensions/TimeSpanExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ public static AgeSpan ToAgeSpan(this TimeSpan span)
8888
{
8989
return new AgeSpan(span);
9090
}
91+
92+
/// <summary>
93+
/// Subtracts the specified TimeSpan from this TimeSpan, ensuring the result never goes below TimeSpan.Zero.
94+
/// </summary>
95+
/// <param name="self">The TimeSpan to subtract from.</param>
96+
/// <param name="other">The TimeSpan to subtract.</param>
97+
/// <returns>The result of the subtraction, or TimeSpan.Zero if the result would be negative.</returns>
98+
public static TimeSpan SubtractSaturating(this TimeSpan self, TimeSpan other)
99+
{
100+
return self >= other ? self.Subtract(other) : TimeSpan.Zero;
101+
}
91102
}
92103

93104
public struct AgeSpan

tests/Exceptionless.DateTimeExtensions.Tests/TimeSpanExtensionTests.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,90 @@ 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_WithLargerValue_ReturnsNormalSubtraction()
71+
{
72+
var timeSpan1 = TimeSpan.FromHours(5);
73+
var timeSpan2 = TimeSpan.FromHours(2);
74+
75+
var result = timeSpan1.SubtractSaturating(timeSpan2);
76+
77+
Assert.Equal(TimeSpan.FromHours(3), result);
78+
}
79+
80+
[Fact]
81+
public void SubtractSaturating_WithSmallerValue_ReturnsZero()
82+
{
83+
var timeSpan1 = TimeSpan.FromHours(2);
84+
var timeSpan2 = TimeSpan.FromHours(5);
85+
86+
var result = timeSpan1.SubtractSaturating(timeSpan2);
87+
88+
Assert.Equal(TimeSpan.Zero, result);
89+
}
90+
91+
[Fact]
92+
public void SubtractSaturating_WithEqualValues_ReturnsZero()
93+
{
94+
var timeSpan1 = TimeSpan.FromMinutes(30);
95+
var timeSpan2 = TimeSpan.FromMinutes(30);
96+
97+
var result = timeSpan1.SubtractSaturating(timeSpan2);
98+
99+
Assert.Equal(TimeSpan.Zero, result);
100+
}
101+
102+
[Fact]
103+
public void SubtractSaturating_WithZeroValue_ReturnsOriginalValue()
104+
{
105+
var timeSpan = TimeSpan.FromSeconds(10);
106+
107+
var result = timeSpan.SubtractSaturating(TimeSpan.Zero);
108+
109+
Assert.Equal(TimeSpan.FromSeconds(10), result);
110+
}
111+
112+
[Fact]
113+
public void SubtractSaturating_WithZeroAsBase_ReturnsZero()
114+
{
115+
var result1 = TimeSpan.Zero.SubtractSaturating(TimeSpan.FromSeconds(5));
116+
var result2 = TimeSpan.Zero.SubtractSaturating(TimeSpan.Zero);
117+
118+
Assert.Equal(TimeSpan.Zero, result1);
119+
Assert.Equal(TimeSpan.Zero, result2);
120+
}
121+
122+
[Fact]
123+
public void SubtractSaturating_WithLargeValues_ReturnsCorrectResult()
124+
{
125+
var largeTimeSpan1 = TimeSpan.FromDays(100);
126+
var largeTimeSpan2 = TimeSpan.FromDays(50);
127+
128+
var result = largeTimeSpan1.SubtractSaturating(largeTimeSpan2);
129+
130+
Assert.Equal(TimeSpan.FromDays(50), result);
131+
}
132+
133+
[Fact]
134+
public void SubtractSaturating_WithNegativeBase_ReturnsZero()
135+
{
136+
var negativeTimeSpan = TimeSpan.FromHours(-2);
137+
var positiveTimeSpan = TimeSpan.FromHours(1);
138+
139+
var result = negativeTimeSpan.SubtractSaturating(positiveTimeSpan);
140+
141+
Assert.Equal(TimeSpan.Zero, result);
142+
}
143+
144+
[Fact]
145+
public void SubtractSaturating_WithNegativeSubtrahend_ReturnsAddedValue()
146+
{
147+
var timeSpan = TimeSpan.FromHours(3);
148+
var negativeTimeSpan = TimeSpan.FromHours(-2);
149+
150+
var result = timeSpan.SubtractSaturating(negativeTimeSpan);
151+
152+
Assert.Equal(TimeSpan.FromHours(5), result);
153+
}
68154
}

0 commit comments

Comments
 (0)