Skip to content

Commit 4d75a96

Browse files
committed
Fix #23 Add Speed Units
* Meters per second * Feet per second * Kilometers per hour * Miles per hour * Knots
1 parent 08e7e01 commit 4d75a96

File tree

9 files changed

+553
-0
lines changed

9 files changed

+553
-0
lines changed

Src/UnitsNet/Attributes/Attributes.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ public RotationalSpeedAttribute(double slope, string pluralName = (string)null)
115115
}
116116
}
117117

118+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
119+
public class SpeedAttribute : UnitAttribute
120+
{
121+
public override Unit BaseUnit { get { return Unit.MeterPerSecond; } }
122+
public override string XmlDocSummary { get { return "In everyday use and in kinematics, the speed of an object is the magnitude of its velocity (the rate of change of its position); it is thus a scalar quantity.[1] The average speed of an object in an interval of time is the distance travelled by the object divided by the duration of the interval;[2] the instantaneous speed is the limit of the average speed as the duration of the time interval approaches zero."; } }
123+
public SpeedAttribute(double slope, string pluralName = (string)null)
124+
: base(pluralName, slope, offset: 0)
125+
{
126+
}
127+
}
128+
118129
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
119130
public class TemperatureAttribute : UnitAttribute
120131
{
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
// Copyright © 2007 by Initial Force AS. All rights reserved.
2+
// https://github.com/InitialForce/SIUnits
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+
// ReSharper disable once CheckNamespace
25+
namespace UnitsNet
26+
{
27+
/// <summary>
28+
/// In everyday use and in kinematics, the speed of an object is the magnitude of its velocity (the rate of change of its position); it is thus a scalar quantity.[1] The average speed of an object in an interval of time is the distance travelled by the object divided by the duration of the interval;[2] the instantaneous speed is the limit of the average speed as the duration of the time interval approaches zero.
29+
/// </summary>
30+
public partial struct Speed : IComparable, IComparable<Speed>
31+
{
32+
/// <summary>
33+
/// Base unit of Speed.
34+
/// </summary>
35+
public readonly double MetersPerSecond;
36+
37+
public Speed(double meterspersecond) : this()
38+
{
39+
MetersPerSecond = meterspersecond;
40+
}
41+
42+
#region Unit Properties
43+
44+
/// <summary>
45+
/// Get Speed in FeetPerSecond.
46+
/// </summary>
47+
/// <remarks>Example: x = (y - b) / a where x is value in FeetPerSecond and y is value in base unit MetersPerSecond.</remarks>
48+
public double FeetPerSecond
49+
{
50+
get { return (MetersPerSecond - (0)) / 0.3048; }
51+
}
52+
53+
/// <summary>
54+
/// Get Speed in KilometersPerHour.
55+
/// </summary>
56+
/// <remarks>Example: x = (y - b) / a where x is value in KilometersPerHour and y is value in base unit MetersPerSecond.</remarks>
57+
public double KilometersPerHour
58+
{
59+
get { return (MetersPerSecond - (0)) / 0.277777777777778; }
60+
}
61+
62+
/// <summary>
63+
/// Get Speed in Knots.
64+
/// </summary>
65+
/// <remarks>Example: x = (y - b) / a where x is value in Knots and y is value in base unit MetersPerSecond.</remarks>
66+
public double Knots
67+
{
68+
get { return (MetersPerSecond - (0)) / 0.514444; }
69+
}
70+
71+
/// <summary>
72+
/// Get Speed in MilesPerHour.
73+
/// </summary>
74+
/// <remarks>Example: x = (y - b) / a where x is value in MilesPerHour and y is value in base unit MetersPerSecond.</remarks>
75+
public double MilesPerHour
76+
{
77+
get { return (MetersPerSecond - (0)) / 0.44704; }
78+
}
79+
80+
#endregion
81+
82+
#region Static
83+
84+
public static Speed Zero
85+
{
86+
get { return new Speed(); }
87+
}
88+
89+
/// <summary>
90+
/// Get Speed from FeetPerSecond.
91+
/// </summary>
92+
/// <remarks>Example: y = ax + b where x is value in FeetPerSecond and y is value in base unit MetersPerSecond.</remarks>
93+
public static Speed FromFeetPerSecond(double feetpersecond)
94+
{
95+
return new Speed(0.3048 * feetpersecond + 0);
96+
}
97+
98+
/// <summary>
99+
/// Get Speed from KilometersPerHour.
100+
/// </summary>
101+
/// <remarks>Example: y = ax + b where x is value in KilometersPerHour and y is value in base unit MetersPerSecond.</remarks>
102+
public static Speed FromKilometersPerHour(double kilometersperhour)
103+
{
104+
return new Speed(0.277777777777778 * kilometersperhour + 0);
105+
}
106+
107+
/// <summary>
108+
/// Get Speed from Knots.
109+
/// </summary>
110+
/// <remarks>Example: y = ax + b where x is value in Knots and y is value in base unit MetersPerSecond.</remarks>
111+
public static Speed FromKnots(double knots)
112+
{
113+
return new Speed(0.514444 * knots + 0);
114+
}
115+
116+
/// <summary>
117+
/// Get Speed from MetersPerSecond.
118+
/// </summary>
119+
/// <remarks>Example: y = ax + b where x is value in MetersPerSecond and y is value in base unit MetersPerSecond.</remarks>
120+
public static Speed FromMetersPerSecond(double meterspersecond)
121+
{
122+
return new Speed(1 * meterspersecond + 0);
123+
}
124+
125+
/// <summary>
126+
/// Get Speed from MilesPerHour.
127+
/// </summary>
128+
/// <remarks>Example: y = ax + b where x is value in MilesPerHour and y is value in base unit MetersPerSecond.</remarks>
129+
public static Speed FromMilesPerHour(double milesperhour)
130+
{
131+
return new Speed(0.44704 * milesperhour + 0);
132+
}
133+
134+
#endregion
135+
136+
#region Arithmetic Operators
137+
138+
public static Speed operator -(Speed right)
139+
{
140+
return new Speed(-right.MetersPerSecond);
141+
}
142+
143+
public static Speed operator +(Speed left, Speed right)
144+
{
145+
return new Speed(left.MetersPerSecond + right.MetersPerSecond);
146+
}
147+
148+
public static Speed operator -(Speed left, Speed right)
149+
{
150+
return new Speed(left.MetersPerSecond - right.MetersPerSecond);
151+
}
152+
153+
public static Speed operator *(double left, Speed right)
154+
{
155+
return new Speed(left*right.MetersPerSecond);
156+
}
157+
158+
public static Speed operator *(Speed left, double right)
159+
{
160+
return new Speed(left.MetersPerSecond*right);
161+
}
162+
163+
public static Speed operator /(Speed left, double right)
164+
{
165+
return new Speed(left.MetersPerSecond/right);
166+
}
167+
168+
public static double operator /(Speed left, Speed right)
169+
{
170+
return left.MetersPerSecond/right.MetersPerSecond;
171+
}
172+
173+
#endregion
174+
175+
#region Equality / IComparable
176+
177+
public int CompareTo(object obj)
178+
{
179+
if (obj == null) throw new ArgumentNullException("obj");
180+
if (!(obj is Speed)) throw new ArgumentException("Expected type Speed.", "obj");
181+
return CompareTo((Speed) obj);
182+
}
183+
184+
public int CompareTo(Speed other)
185+
{
186+
return MetersPerSecond.CompareTo(other.MetersPerSecond);
187+
}
188+
189+
public static bool operator <=(Speed left, Speed right)
190+
{
191+
return left.MetersPerSecond <= right.MetersPerSecond;
192+
}
193+
194+
public static bool operator >=(Speed left, Speed right)
195+
{
196+
return left.MetersPerSecond >= right.MetersPerSecond;
197+
}
198+
199+
public static bool operator <(Speed left, Speed right)
200+
{
201+
return left.MetersPerSecond < right.MetersPerSecond;
202+
}
203+
204+
public static bool operator >(Speed left, Speed right)
205+
{
206+
return left.MetersPerSecond > right.MetersPerSecond;
207+
}
208+
209+
public static bool operator ==(Speed left, Speed right)
210+
{
211+
return left.MetersPerSecond == right.MetersPerSecond;
212+
}
213+
214+
public static bool operator !=(Speed left, Speed right)
215+
{
216+
return left.MetersPerSecond != right.MetersPerSecond;
217+
}
218+
219+
public override bool Equals(object obj)
220+
{
221+
if (obj == null || GetType() != obj.GetType())
222+
{
223+
return false;
224+
}
225+
226+
return MetersPerSecond.Equals(((Speed) obj).MetersPerSecond);
227+
}
228+
229+
public override int GetHashCode()
230+
{
231+
return MetersPerSecond.GetHashCode();
232+
}
233+
234+
#endregion
235+
236+
public override string ToString()
237+
{
238+
return string.Format("{0:0.##} {1}", MetersPerSecond, UnitSystem.Create().GetDefaultAbbreviation(Unit.MeterPerSecond));
239+
}
240+
}
241+
}

Src/UnitsNet/GeneratedCode/UnitConverter.g.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static double Convert(double value, Unit fromUnit, Unit toUnit)
5151
if (TryConvertFromMass(value, fromUnit, toUnit, out newValue)) return newValue;
5252
if (TryConvertFromPressure(value, fromUnit, toUnit, out newValue)) return newValue;
5353
if (TryConvertFromRotationalSpeed(value, fromUnit, toUnit, out newValue)) return newValue;
54+
if (TryConvertFromSpeed(value, fromUnit, toUnit, out newValue)) return newValue;
5455
if (TryConvertFromTemperature(value, fromUnit, toUnit, out newValue)) return newValue;
5556
if (TryConvertFromTorque(value, fromUnit, toUnit, out newValue)) return newValue;
5657
if (TryConvertFromVolume(value, fromUnit, toUnit, out newValue)) return newValue;
@@ -85,6 +86,7 @@ public static bool TryConvert(double value, Unit fromUnit, Unit toUnit, out doub
8586
if (TryConvertFromMass(value, fromUnit, toUnit, out newValue)) return true;
8687
if (TryConvertFromPressure(value, fromUnit, toUnit, out newValue)) return true;
8788
if (TryConvertFromRotationalSpeed(value, fromUnit, toUnit, out newValue)) return true;
89+
if (TryConvertFromSpeed(value, fromUnit, toUnit, out newValue)) return true;
8890
if (TryConvertFromTemperature(value, fromUnit, toUnit, out newValue)) return true;
8991
if (TryConvertFromTorque(value, fromUnit, toUnit, out newValue)) return true;
9092
if (TryConvertFromVolume(value, fromUnit, toUnit, out newValue)) return true;
@@ -392,6 +394,35 @@ private static bool TryConvertFromRotationalSpeed(double value, Unit fromUnit, U
392394
}
393395
}
394396

397+
/// <summary>
398+
/// Try to dynamically convert from Speed to <paramref name="toUnit"/>.
399+
/// </summary>
400+
/// <param name="value">Value to convert from.</param>
401+
/// <param name="fromUnit">Unit to convert from.</param>
402+
/// <param name="toUnit">Compatible unit to convert to.</param>
403+
/// <param name="newValue">Value in new unit if successful, zero otherwise.</param>
404+
/// <returns>True if the two units were compatible and the conversion was successful.</returns>
405+
private static bool TryConvertFromSpeed(double value, Unit fromUnit, Unit toUnit, out double newValue)
406+
{
407+
switch (fromUnit)
408+
{
409+
case Unit.FootPerSecond:
410+
return TryConvert(Speed.FromFeetPerSecond(value), toUnit, out newValue);
411+
case Unit.KilometerPerHour:
412+
return TryConvert(Speed.FromKilometersPerHour(value), toUnit, out newValue);
413+
case Unit.Knot:
414+
return TryConvert(Speed.FromKnots(value), toUnit, out newValue);
415+
case Unit.MeterPerSecond:
416+
return TryConvert(Speed.FromMetersPerSecond(value), toUnit, out newValue);
417+
case Unit.MilePerHour:
418+
return TryConvert(Speed.FromMilesPerHour(value), toUnit, out newValue);
419+
420+
default:
421+
newValue = 0;
422+
return false;
423+
}
424+
}
425+
395426
/// <summary>
396427
/// Try to dynamically convert from Temperature to <paramref name="toUnit"/>.
397428
/// </summary>
@@ -863,6 +894,40 @@ private static bool TryConvert(RotationalSpeed value, Unit toUnit, out double ne
863894
}
864895

865896

897+
/// <summary>
898+
/// Try to dynamically convert from Speed to <paramref name="toUnit"/>.
899+
/// </summary>
900+
/// <param name="value">Value to convert from.</param>
901+
/// <param name="toUnit">Compatible unit to convert to.</param>
902+
/// <param name="newValue">Value in new unit if successful, zero otherwise.</param>
903+
/// <returns>True if the two units were compatible and the conversion was successful.</returns>
904+
private static bool TryConvert(Speed value, Unit toUnit, out double newValue)
905+
{
906+
switch (toUnit)
907+
{
908+
case Unit.FootPerSecond:
909+
newValue = value.FeetPerSecond;
910+
return true;
911+
case Unit.KilometerPerHour:
912+
newValue = value.KilometersPerHour;
913+
return true;
914+
case Unit.Knot:
915+
newValue = value.Knots;
916+
return true;
917+
case Unit.MeterPerSecond:
918+
newValue = value.MetersPerSecond;
919+
return true;
920+
case Unit.MilePerHour:
921+
newValue = value.MilesPerHour;
922+
return true;
923+
924+
default:
925+
newValue = 0;
926+
return false;
927+
}
928+
}
929+
930+
866931
/// <summary>
867932
/// Try to dynamically convert from Temperature to <paramref name="toUnit"/>.
868933
/// </summary>

Src/UnitsNet/Unit.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ public enum Unit
178178
[RotationalSpeed(1, "RevolutionsPerSecond")] RevolutionPerSecond,
179179
[RotationalSpeed(1.0/60, "RevolutionsPerMinute")] RevolutionPerMinute,
180180

181+
// Sources: http://en.wikipedia.org/wiki/Speed
182+
[Speed(0.3048, "FeetPerSecond")] FootPerSecond,
183+
[Speed(1d / 3.6, "KilometersPerHour")] KilometerPerHour,
184+
[Speed(0.514444)] Knot,
185+
[Speed(1, "MetersPerSecond")] MeterPerSecond, // Base unit.
186+
[Speed(0.44704, "MilesPerHour")] MilePerHour,
187+
181188
[Temperature(slope: 1, offset: 273.15, pluralName: "DegreesCelsius")] DegreeCelsius,
182189
[Temperature(slope: -2d/3, offset: 373.15, pluralName: "DegreesDelisle")] DegreeDelisle,
183190
[Temperature(slope: 5d/9, offset: 459.67*5d/9, pluralName: "DegreesFahrenheit")] DegreeFahrenheit,

Src/UnitsNet/UnitSystem.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,13 @@ private void CreateCultureInvariants()
427427
MapUnitToAbbreviation(Unit.RevolutionPerSecond, "r/s");
428428
MapUnitToAbbreviation(Unit.RevolutionPerMinute, "r/min");
429429

430+
// Speed
431+
MapUnitToAbbreviation(Unit.FootPerSecond, "ft/s");
432+
MapUnitToAbbreviation(Unit.KilometerPerHour, "km/h");
433+
MapUnitToAbbreviation(Unit.Knot, "kt", "kn", "knot", "knots");
434+
MapUnitToAbbreviation(Unit.MeterPerSecond, "m/s");
435+
MapUnitToAbbreviation(Unit.MilePerHour, "mph");
436+
430437
// Temperature
431438
MapUnitToAbbreviation(Unit.Kelvin, "K");
432439
MapUnitToAbbreviation(Unit.DegreeCelsius, "°C");

Src/UnitsNet/UnitsNet.net35.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
<Compile Include="GeneratedCode\RotationalSpeedUnit.g.cs">
7474
<DependentUpon>UnitClasses.tt</DependentUpon>
7575
</Compile>
76+
<Compile Include="GeneratedCode\SpeedUnit.g.cs">
77+
<DependentUpon>UnitClasses.tt</DependentUpon>
78+
</Compile>
7679
<Compile Include="GeneratedCode\TemperatureUnit.g.cs">
7780
<DependentUpon>UnitClasses.tt</DependentUpon>
7881
</Compile>

0 commit comments

Comments
 (0)