Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 112e7d5

Browse files
committed
Fix TimeSpan.MinValue overflow
1 parent 923e666 commit 112e7d5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/ServiceStack.Text/Support/TimeSpanConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static string ToXsdDuration(TimeSpan timeSpan)
1212

1313
sb.Append(timeSpan.Ticks < 0 ? "-P" : "P");
1414

15-
double ticks = Math.Abs(timeSpan.Ticks);
15+
double ticks = timeSpan.Ticks > 0 ? timeSpan.Ticks : timeSpan.Ticks * -1L;
1616
double totalSeconds = ticks / TimeSpan.TicksPerSecond;
1717
long wholeSeconds = (long) totalSeconds;
1818
long seconds = wholeSeconds;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace ServiceStack.Text.Tests.Issues
5+
{
6+
public class StackOverflowIssues
7+
{
8+
public class MinTypes
9+
{
10+
public TimeSpan TimeSpan { get; set; }
11+
}
12+
13+
[Test]
14+
public void Can_convert_min_TimeSpan()
15+
{
16+
var c1 = new MinTypes {
17+
TimeSpan = TimeSpan.MinValue,
18+
};
19+
var json = JsonSerializer.SerializeToString(c1, typeof(MinTypes));
20+
var dto = JsonSerializer.SerializeToString(c1, typeof(MinTypes));
21+
Assert.That(json, Is.EqualTo(dto));
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)