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

Commit be3a106

Browse files
committed
Add support for TimeSpan > long
1 parent f760cca commit be3a106

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public static object ChangeValueType(object from, Type toType)
103103
return c.ToString();
104104
if (toType == typeof(TimeSpan) && from is long ticks)
105105
return new TimeSpan(ticks);
106+
if (toType == typeof(long) && from is TimeSpan time)
107+
return time.Ticks;
106108

107109
var destNumberType = DynamicNumber.GetNumber(toType);
108110
var value = destNumberType?.ConvertFrom(from);

tests/ServiceStack.Text.Tests/AutoMappingObjectDictionaryTests.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,28 @@ public class ModelWithTimeSpan
327327
public TimeSpan Time { get; set; }
328328
}
329329

330+
public class ModelWithLong
331+
{
332+
public long Time { get; set; }
333+
}
334+
330335
[Test]
331-
public void FromObjectDictionary_does_try_to_Convert_different_types()
336+
public void FromObjectDictionary_does_Convert_long_to_TimeSpan()
332337
{
333338
var time = new TimeSpan(1,1,1,1);
334339
var map = new Dictionary<string, object> {
335340
[nameof(ModelWithTimeSpan.Time)] = time.Ticks
336341
};
337342

338343
var dto = map.FromObjectDictionary<ModelWithTimeSpan>();
339-
340344
Assert.That(dto.Time, Is.EqualTo(time));
345+
346+
map = new Dictionary<string, object> {
347+
[nameof(ModelWithTimeSpan.Time)] = time
348+
};
349+
350+
var dtoLong = map.FromObjectDictionary<ModelWithLong>();
351+
Assert.That(dtoLong.Time, Is.EqualTo(time.Ticks));
341352
}
342353
}
343354

0 commit comments

Comments
 (0)