Skip to content

Commit bdf94d9

Browse files
committed
Add NumberToTimeSpanExtensions + tests
Shorthand extensions, such as 1.s() or 1.h() for TimeSpan.FromSeconds() and .FromHours().
1 parent d5b8888 commit bdf94d9

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright © 2007 Andreas Gullberg Larsen ([email protected]).
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+
using NUnit.Framework;
24+
using UnitsNet.Extensions.Number;
25+
26+
namespace UnitsNet.Tests.Extensions.Number
27+
{
28+
[TestFixture]
29+
public class NumberToTimeSpanExtensionsTest
30+
{
31+
[Test]
32+
public void ExtensionMethodsReturnTimeSpanOfSameValue()
33+
{
34+
Assert.AreEqual(TimeSpan.FromDays(1), 1.d());
35+
Assert.AreEqual(TimeSpan.FromDays(1), 1L.d());
36+
Assert.AreEqual(TimeSpan.FromDays(1), 1f.d());
37+
Assert.AreEqual(TimeSpan.FromDays(1), 1d.d());
38+
Assert.AreEqual(TimeSpan.FromDays(1), 1m.d());
39+
40+
Assert.AreEqual(TimeSpan.FromHours(1), 1.h());
41+
Assert.AreEqual(TimeSpan.FromHours(1), 1L.h());
42+
Assert.AreEqual(TimeSpan.FromHours(1), 1f.h());
43+
Assert.AreEqual(TimeSpan.FromHours(1), 1d.h());
44+
Assert.AreEqual(TimeSpan.FromHours(1), 1m.h());
45+
46+
Assert.AreEqual(TimeSpan.FromMinutes(1), 1.m());
47+
Assert.AreEqual(TimeSpan.FromMinutes(1), 1L.m());
48+
Assert.AreEqual(TimeSpan.FromMinutes(1), 1f.m());
49+
Assert.AreEqual(TimeSpan.FromMinutes(1), 1d.m());
50+
Assert.AreEqual(TimeSpan.FromMinutes(1), 1m.m());
51+
52+
Assert.AreEqual(TimeSpan.FromSeconds(1), 1.s());
53+
Assert.AreEqual(TimeSpan.FromSeconds(1), 1L.s());
54+
Assert.AreEqual(TimeSpan.FromSeconds(1), 1f.s());
55+
Assert.AreEqual(TimeSpan.FromSeconds(1), 1d.s());
56+
Assert.AreEqual(TimeSpan.FromSeconds(1), 1m.s());
57+
58+
Assert.AreEqual(TimeSpan.FromMilliseconds(1), 1.ms());
59+
Assert.AreEqual(TimeSpan.FromMilliseconds(1), 1L.ms());
60+
Assert.AreEqual(TimeSpan.FromMilliseconds(1), 1f.ms());
61+
Assert.AreEqual(TimeSpan.FromMilliseconds(1), 1d.ms());
62+
Assert.AreEqual(TimeSpan.FromMilliseconds(1), 1m.ms());
63+
}
64+
}
65+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright © 2007 Andreas Gullberg Larsen ([email protected]).
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+
#if !WINDOWS_UWP
23+
// Extension methods/overloads not supported in Universal Windows Platform (WinRT Components)
24+
using System;
25+
26+
namespace UnitsNet.Extensions.Number
27+
{
28+
public static class NumberToTimeSpanExtensions
29+
{
30+
// ReSharper disable InconsistentNaming
31+
/// <summary>Shorthand for <see cref="TimeSpan.FromDays" />.</summary>
32+
public static TimeSpan d(this int days) => TimeSpan.FromDays(days);
33+
34+
/// <summary>Shorthand for <see cref="TimeSpan.FromDays" />.</summary>
35+
public static TimeSpan d(this long days) => TimeSpan.FromDays(days);
36+
37+
/// <summary>Shorthand for <see cref="TimeSpan.FromDays" />.</summary>
38+
public static TimeSpan d(this float days) => TimeSpan.FromDays(days);
39+
40+
/// <summary>Shorthand for <see cref="TimeSpan.FromDays" />.</summary>
41+
public static TimeSpan d(this double days) => TimeSpan.FromDays(days);
42+
43+
/// <summary>Shorthand for <see cref="TimeSpan.FromDays" />.</summary>
44+
public static TimeSpan d(this decimal days) => TimeSpan.FromDays(Convert.ToDouble(days));
45+
46+
/// <summary>Shorthand for <see cref="TimeSpan.FromHours" />.</summary>
47+
public static TimeSpan h(this int hours) => TimeSpan.FromHours(hours);
48+
49+
/// <summary>Shorthand for <see cref="TimeSpan.FromHours" />.</summary>
50+
public static TimeSpan h(this long hours) => TimeSpan.FromHours(hours);
51+
52+
/// <summary>Shorthand for <see cref="TimeSpan.FromHours" />.</summary>
53+
public static TimeSpan h(this float hours) => TimeSpan.FromHours(hours);
54+
55+
/// <summary>Shorthand for <see cref="TimeSpan.FromHours" />.</summary>
56+
public static TimeSpan h(this double hours) => TimeSpan.FromHours(hours);
57+
58+
/// <summary>Shorthand for <see cref="TimeSpan.FromHours" />.</summary>
59+
public static TimeSpan h(this decimal hours) => TimeSpan.FromHours(Convert.ToDouble(hours));
60+
61+
/// <summary>Shorthand for <see cref="TimeSpan.FromMinutes" />.</summary>
62+
public static TimeSpan m(this int minutes) => TimeSpan.FromMinutes(minutes);
63+
64+
/// <summary>Shorthand for <see cref="TimeSpan.FromMinutes" />.</summary>
65+
public static TimeSpan m(this long minutes) => TimeSpan.FromMinutes(minutes);
66+
67+
/// <summary>Shorthand for <see cref="TimeSpan.FromMinutes" />.</summary>
68+
public static TimeSpan m(this float minutes) => TimeSpan.FromMinutes(minutes);
69+
70+
/// <summary>Shorthand for <see cref="TimeSpan.FromMinutes" />.</summary>
71+
public static TimeSpan m(this double minutes) => TimeSpan.FromMinutes(minutes);
72+
73+
/// <summary>Shorthand for <see cref="TimeSpan.FromMinutes" />.</summary>
74+
public static TimeSpan m(this decimal minutes) => TimeSpan.FromMinutes(Convert.ToDouble(minutes));
75+
76+
/// <summary>Shorthand for <see cref="TimeSpan.FromSeconds" />.</summary>
77+
public static TimeSpan s(this int seconds) => TimeSpan.FromSeconds(seconds);
78+
79+
/// <summary>Shorthand for <see cref="TimeSpan.FromSeconds" />.</summary>
80+
public static TimeSpan s(this long seconds) => TimeSpan.FromSeconds(seconds);
81+
82+
/// <summary>Shorthand for <see cref="TimeSpan.FromSeconds" />.</summary>
83+
public static TimeSpan s(this float seconds) => TimeSpan.FromSeconds(seconds);
84+
85+
/// <summary>Shorthand for <see cref="TimeSpan.FromSeconds" />.</summary>
86+
public static TimeSpan s(this double seconds) => TimeSpan.FromSeconds(seconds);
87+
88+
/// <summary>Shorthand for <see cref="TimeSpan.FromSeconds" />.</summary>
89+
public static TimeSpan s(this decimal seconds) => TimeSpan.FromSeconds(Convert.ToDouble(seconds));
90+
91+
/// <summary>Shorthand for <see cref="TimeSpan.FromMilliseconds" />.</summary>
92+
public static TimeSpan ms(this int milliseconds) => TimeSpan.FromMilliseconds(milliseconds);
93+
94+
/// <summary>Shorthand for <see cref="TimeSpan.FromMilliseconds" />.</summary>
95+
public static TimeSpan ms(this long milliseconds) => TimeSpan.FromMilliseconds(milliseconds);
96+
97+
/// <summary>Shorthand for <see cref="TimeSpan.FromMilliseconds" />.</summary>
98+
public static TimeSpan ms(this float milliseconds) => TimeSpan.FromMilliseconds(milliseconds);
99+
100+
/// <summary>Shorthand for <see cref="TimeSpan.FromMilliseconds" />.</summary>
101+
public static TimeSpan ms(this double milliseconds) => TimeSpan.FromMilliseconds(milliseconds);
102+
103+
/// <summary>Shorthand for <see cref="TimeSpan.FromMilliseconds" />.</summary>
104+
public static TimeSpan ms(this decimal milliseconds) => TimeSpan.FromMilliseconds(Convert.ToDouble(milliseconds));
105+
106+
// ReSharper restore InconsistentNaming
107+
}
108+
}
109+
110+
#endif

0 commit comments

Comments
 (0)