Skip to content

Commit 301b186

Browse files
author
Erik Ovegard
committed
Added overloads for Duration
1 parent 4c2e6a0 commit 301b186

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

UnitsNet.Tests/CustomCode/DurationTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
// THE SOFTWARE.
2121

22+
using NUnit.Framework;
23+
using System;
24+
2225
namespace UnitsNet.Tests.CustomCode
2326
{
2427
public class DurationTests : DurationTestsBase
@@ -42,5 +45,71 @@ public class DurationTests : DurationTestsBase
4245
protected override double WeeksInOneSecond => 1.6534e-6;
4346

4447
protected override double YearsInOneSecond => 3.1689e-8;
48+
49+
[Test]
50+
public static void ToTimeSpanShouldThrowExceptionOnValuesLargerThanTimeSpanMax()
51+
{
52+
Duration duration = Duration.FromSeconds(TimeSpan.MaxValue.TotalSeconds + 1);
53+
Assert.Throws<ArgumentOutOfRangeException>(() => duration.ToTimeSpan());
54+
}
55+
56+
[Test]
57+
public static void ToTimeSpanShouldThrowExceptionOnValuesSmallerThanTimeSpanMin()
58+
{
59+
Duration duration = Duration.FromSeconds(TimeSpan.MinValue.TotalSeconds - 1);
60+
Assert.Throws<ArgumentOutOfRangeException>(() => duration.ToTimeSpan());
61+
}
62+
63+
[Test]
64+
public static void ToTimeSpanShouldNotThrowExceptionOnValuesSlightlyLargerThanTimeSpanMin()
65+
{
66+
Duration duration = Duration.FromSeconds(TimeSpan.MinValue.TotalSeconds + 1);
67+
TimeSpan timeSpan = duration.ToTimeSpan();
68+
Assert.AreEqual(duration.Seconds, timeSpan.TotalSeconds,1e-3);
69+
}
70+
71+
[Test]
72+
public static void ToTimeSpanShouldNotThrowExceptionOnValuesSlightlySmallerThanTimeSpanMax()
73+
{
74+
Duration duration = Duration.FromSeconds(TimeSpan.MaxValue.TotalSeconds - 1);
75+
TimeSpan timeSpan = duration.ToTimeSpan();
76+
Assert.AreEqual(duration.Seconds, timeSpan.TotalSeconds, 1e-3);
77+
}
78+
79+
[Test]
80+
public static void ExplicitCastToTimeSpanShouldReturnSameValue()
81+
{
82+
Duration duration = Duration.FromSeconds(60);
83+
TimeSpan timeSpan = (TimeSpan)duration;
84+
Assert.AreEqual(duration.Seconds, timeSpan.TotalSeconds, 1e-10);
85+
}
86+
87+
[Test]
88+
public static void ExplicitCastToDurationShouldReturnSameValue()
89+
{
90+
TimeSpan timeSpan = TimeSpan.FromSeconds(60);
91+
Duration duration = (Duration)timeSpan;
92+
Assert.AreEqual(timeSpan.TotalSeconds, duration.Seconds, 1e-10);
93+
}
94+
95+
[Test]
96+
public static void DateTimePlusDurationReturnsDateTime()
97+
{
98+
DateTime dateTime = new DateTime(2016, 1, 1);
99+
Duration oneDay = Duration.FromDays(1);
100+
DateTime result = dateTime + oneDay;
101+
DateTime expected = new DateTime(2016, 1, 2);
102+
Assert.AreEqual(expected, result);
103+
}
104+
105+
[Test]
106+
public static void DateTimeMinusDurationReturnsDateTime()
107+
{
108+
DateTime dateTime = new DateTime(2016, 1, 2);
109+
Duration oneDay = Duration.FromDays(1);
110+
DateTime result = dateTime - oneDay;
111+
DateTime expected = new DateTime(2016, 1, 1);
112+
Assert.AreEqual(expected, result);
113+
}
45114
}
46115
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright(c) 2007 Andreas Gullberg Larsen
2+
// https://github.com/anjdreas/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
using System;
23+
24+
namespace UnitsNet
25+
{
26+
public partial struct Duration
27+
{
28+
public static DateTime operator +(DateTime time, Duration duration)
29+
{
30+
return time.AddSeconds(duration.Seconds);
31+
}
32+
33+
public static DateTime operator -(DateTime time, Duration duration)
34+
{
35+
return time.AddSeconds(-duration.Seconds);
36+
}
37+
38+
/// <summary>
39+
/// Convert a Duration to a TimeSpan.
40+
/// </summary>
41+
/// <exception cref="ArgumentOutOfRangeException">Throws if the TimeSpan can't represent the Duration exactly </exception>
42+
/// <returns>The TimeSpan with the same time as the duration</returns>
43+
public TimeSpan ToTimeSpan()
44+
{
45+
if (Seconds > TimeSpan.MaxValue.TotalSeconds ||
46+
Seconds < TimeSpan.MinValue.TotalSeconds)
47+
{
48+
throw new ArgumentOutOfRangeException(nameof(Duration), "The duration is too large or small to fit in a TimeSpan");
49+
}
50+
return TimeSpan.FromSeconds(Seconds);
51+
}
52+
53+
public static explicit operator TimeSpan(Duration duration)
54+
{
55+
return duration.ToTimeSpan();
56+
}
57+
58+
public static explicit operator Duration(TimeSpan duration)
59+
{
60+
return FromSeconds(duration.TotalSeconds);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)