From 5479f2dd1e5ffc84b62bc22ed514a8c5fecf5b74 Mon Sep 17 00:00:00 2001 From: Muximize Date: Mon, 19 Feb 2024 18:21:56 +0100 Subject: [PATCH 1/8] Implicitly cast between Duration and TimeSpan --- UnitsNet.Tests/CustomCode/DurationTests.cs | 14 -------------- UnitsNet/CustomCode/Quantities/Duration.extra.cs | 14 +++++--------- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/UnitsNet.Tests/CustomCode/DurationTests.cs b/UnitsNet.Tests/CustomCode/DurationTests.cs index eda0611105..088c87c33c 100644 --- a/UnitsNet.Tests/CustomCode/DurationTests.cs +++ b/UnitsNet.Tests/CustomCode/DurationTests.cs @@ -33,20 +33,6 @@ public class DurationTests : DurationTestsBase protected override double JulianYearsInOneSecond => 3.16880878140289e-08; - [Fact] - public static void ToTimeSpanShouldThrowExceptionOnValuesLargerThanTimeSpanMax() - { - Duration duration = Duration.FromSeconds(TimeSpan.MaxValue.TotalSeconds + 1); - Assert.Throws(() => duration.ToTimeSpan()); - } - - [Fact] - public static void ToTimeSpanShouldThrowExceptionOnValuesSmallerThanTimeSpanMin() - { - Duration duration = Duration.FromSeconds(TimeSpan.MinValue.TotalSeconds - 1); - Assert.Throws(() => duration.ToTimeSpan()); - } - [Fact] public static void ToTimeSpanShouldNotThrowExceptionOnValuesSlightlyLargerThanTimeSpanMin() { diff --git a/UnitsNet/CustomCode/Quantities/Duration.extra.cs b/UnitsNet/CustomCode/Quantities/Duration.extra.cs index 4333d3c745..3a65661f21 100644 --- a/UnitsNet/CustomCode/Quantities/Duration.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Duration.extra.cs @@ -11,14 +11,10 @@ public partial struct Duration /// /// Convert a Duration to a TimeSpan. /// - /// Throws if the TimeSpan can't represent the Duration exactly /// The TimeSpan with the same time as the duration public TimeSpan ToTimeSpan() { - if ( Seconds > TimeSpan.MaxValue.TotalSeconds || - Seconds < TimeSpan.MinValue.TotalSeconds ) - throw new ArgumentOutOfRangeException( nameof( Duration ), "The duration is too large or small to fit in a TimeSpan" ); - return TimeSpan.FromTicks((long)(Seconds * TimeSpan.TicksPerSecond)); + return TimeSpan.FromSeconds(Seconds); } /// Get from plus . @@ -33,14 +29,14 @@ public TimeSpan ToTimeSpan() return time.AddSeconds(-duration.Seconds); } - /// Explicitly cast to . - public static explicit operator TimeSpan(Duration duration) + /// Implicitly cast to . + public static implicit operator TimeSpan(Duration duration) { return duration.ToTimeSpan(); } - /// Explicitly cast to . - public static explicit operator Duration(TimeSpan duration) + /// Implicitly cast to . + public static implicit operator Duration(TimeSpan duration) { return FromSeconds(duration.TotalSeconds); } From 8d85d3f749c2e9bb1da1c4f206c7768f432c0f81 Mon Sep 17 00:00:00 2001 From: Muximize Date: Mon, 19 Feb 2024 18:22:39 +0100 Subject: [PATCH 2/8] Don't infer operators where TimeSpan is the right operand --- CodeGen/Generators/QuantityRelationsParser.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CodeGen/Generators/QuantityRelationsParser.cs b/CodeGen/Generators/QuantityRelationsParser.cs index 38f7abe5e8..a1c0a08dd2 100644 --- a/CodeGen/Generators/QuantityRelationsParser.cs +++ b/CodeGen/Generators/QuantityRelationsParser.cs @@ -60,15 +60,13 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities) .ToList()); // We can infer TimeSpan relations from Duration relations. + // Because TimeSpan can be implicitly cast to Duration and vice versa, + // we only need to infer the relations where Duration is the left operand. var timeSpanQuantity = pseudoQuantity with { Name = "TimeSpan" }; relations.AddRange(relations .Where(r => r.LeftQuantity.Name is "Duration") .Select(r => r with { LeftQuantity = timeSpanQuantity }) .ToList()); - relations.AddRange(relations - .Where(r => r.RightQuantity.Name is "Duration") - .Select(r => r with { RightQuantity = timeSpanQuantity }) - .ToList()); // Sort all relations to keep generated operators in a consistent order. relations.Sort(); From 69cd748fbfc746a661d8385d9b37db4897a1d2de Mon Sep 17 00:00:00 2001 From: Muximize Date: Mon, 19 Feb 2024 18:23:02 +0100 Subject: [PATCH 3/8] Generate code --- .../GeneratedCode/Quantities/Acceleration.g.cs | 14 -------------- UnitsNet/GeneratedCode/Quantities/Angle.g.cs | 7 ------- .../GeneratedCode/Quantities/ElectricCharge.g.cs | 7 ------- .../GeneratedCode/Quantities/ElectricCurrent.g.cs | 14 -------------- .../Quantities/ElectricCurrentGradient.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/Energy.g.cs | 7 ------- .../GeneratedCode/Quantities/ForceChangeRate.g.cs | 7 ------- .../Quantities/KinematicViscosity.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/Length.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/Mass.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/Power.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/Pressure.g.cs | 7 ------- .../Quantities/PressureChangeRate.g.cs | 7 ------- .../GeneratedCode/Quantities/RotationalSpeed.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/Speed.g.cs | 14 -------------- .../Quantities/TemperatureChangeRate.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/Volume.g.cs | 7 ------- UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs | 7 ------- 20 files changed, 161 deletions(-) diff --git a/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs b/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs index 72ac25caa1..07c77dd99f 100644 --- a/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs @@ -44,10 +44,8 @@ namespace UnitsNet #if NET7_0_OR_GREATER IMultiplyOperators, IDivisionOperators, - IDivisionOperators, IMultiplyOperators, IMultiplyOperators, - IMultiplyOperators, #endif IComparable, IComparable, @@ -689,12 +687,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Accel return Jerk.FromMetersPerSecondCubed(acceleration.MetersPerSecondSquared / duration.Seconds); } - /// Get from / . - public static Jerk operator /(Acceleration acceleration, TimeSpan timeSpan) - { - return Jerk.FromMetersPerSecondCubed(acceleration.MetersPerSecondSquared / timeSpan.TotalSeconds); - } - /// Get from * . public static SpecificWeight operator *(Acceleration acceleration, Density density) { @@ -707,12 +699,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Accel return Speed.FromMetersPerSecond(acceleration.MetersPerSecondSquared * duration.Seconds); } - /// Get from * . - public static Speed operator *(Acceleration acceleration, TimeSpan timeSpan) - { - return Speed.FromMetersPerSecond(acceleration.MetersPerSecondSquared * timeSpan.TotalSeconds); - } - /// Get from * . public static Speed operator *(TimeSpan timeSpan, Acceleration acceleration) { diff --git a/UnitsNet/GeneratedCode/Quantities/Angle.g.cs b/UnitsNet/GeneratedCode/Quantities/Angle.g.cs index 346157c773..fd6084a8f1 100644 --- a/UnitsNet/GeneratedCode/Quantities/Angle.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Angle.g.cs @@ -43,7 +43,6 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IDivisionOperators, - IDivisionOperators, IMultiplyOperators, #endif IComparable, @@ -716,12 +715,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Angle return RotationalSpeed.FromRadiansPerSecond(angle.Radians / duration.Seconds); } - /// Get from / . - public static RotationalSpeed operator /(Angle angle, TimeSpan timeSpan) - { - return RotationalSpeed.FromRadiansPerSecond(angle.Radians / timeSpan.TotalSeconds); - } - /// Get from * . public static Torque operator *(Angle angle, RotationalStiffness rotationalStiffness) { diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricCharge.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricCharge.g.cs index ea6a9b18f9..9dab927ee0 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricCharge.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricCharge.g.cs @@ -47,7 +47,6 @@ namespace UnitsNet #if NET7_0_OR_GREATER IDivisionOperators, IDivisionOperators, - IDivisionOperators, IMultiplyOperators, #endif IComparable, @@ -636,12 +635,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Elect return ElectricCurrent.FromAmperes(electricCharge.AmpereHours / duration.Hours); } - /// Get from / . - public static ElectricCurrent operator /(ElectricCharge electricCharge, TimeSpan timeSpan) - { - return ElectricCurrent.FromAmperes(electricCharge.AmpereHours / timeSpan.TotalHours); - } - /// Get from * . public static Energy operator *(ElectricCharge electricCharge, ElectricPotential electricPotential) { diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.g.cs index c54ddcf125..438d0ec079 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.g.cs @@ -43,9 +43,7 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IMultiplyOperators, - IMultiplyOperators, IDivisionOperators, - IDivisionOperators, IMultiplyOperators, IMultiplyOperators, #endif @@ -593,12 +591,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Elect return ElectricCharge.FromAmpereHours(electricCurrent.Amperes * duration.Hours); } - /// Get from * . - public static ElectricCharge operator *(ElectricCurrent electricCurrent, TimeSpan timeSpan) - { - return ElectricCharge.FromAmpereHours(electricCurrent.Amperes * timeSpan.TotalHours); - } - /// Get from * . public static ElectricCharge operator *(TimeSpan timeSpan, ElectricCurrent electricCurrent) { @@ -611,12 +603,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Elect return ElectricCurrentGradient.FromAmperesPerSecond(electricCurrent.Amperes / duration.Seconds); } - /// Get from / . - public static ElectricCurrentGradient operator /(ElectricCurrent electricCurrent, TimeSpan timeSpan) - { - return ElectricCurrentGradient.FromAmperesPerSecond(electricCurrent.Amperes / timeSpan.TotalSeconds); - } - /// Get from * . public static ElectricPotential operator *(ElectricCurrent electricCurrent, ElectricResistance electricResistance) { diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.g.cs index 6683af5fd6..2d71692fbf 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.g.cs @@ -43,7 +43,6 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IMultiplyOperators, - IMultiplyOperators, #endif IComparable, IComparable, @@ -553,12 +552,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Elect return ElectricCurrent.FromAmperes(electricCurrentGradient.AmperesPerSecond * duration.Seconds); } - /// Get from * . - public static ElectricCurrent operator *(ElectricCurrentGradient electricCurrentGradient, TimeSpan timeSpan) - { - return ElectricCurrent.FromAmperes(electricCurrentGradient.AmperesPerSecond * timeSpan.TotalSeconds); - } - /// Get from * . public static ElectricCurrent operator *(TimeSpan timeSpan, ElectricCurrentGradient electricCurrentGradient) { diff --git a/UnitsNet/GeneratedCode/Quantities/Energy.g.cs b/UnitsNet/GeneratedCode/Quantities/Energy.g.cs index 305333c407..98044e2891 100644 --- a/UnitsNet/GeneratedCode/Quantities/Energy.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Energy.g.cs @@ -49,7 +49,6 @@ namespace UnitsNet IDivisionOperators, IMultiplyOperators, IDivisionOperators, - IDivisionOperators, IDivisionOperators, IDivisionOperators, #endif @@ -1191,12 +1190,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Energ return Power.FromWatts(energy.Joules / duration.Seconds); } - /// Get from / . - public static Power operator /(Energy energy, TimeSpan timeSpan) - { - return Power.FromWatts(energy.Joules / timeSpan.TotalSeconds); - } - /// Get from / . public static SpecificEnergy operator /(Energy energy, Mass mass) { diff --git a/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.g.cs b/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.g.cs index 6b54cb1e24..41ac499fc0 100644 --- a/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.g.cs @@ -43,7 +43,6 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IMultiplyOperators, - IMultiplyOperators, #endif IComparable, IComparable, @@ -697,12 +696,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Force return Force.FromNewtons(forceChangeRate.NewtonsPerSecond * duration.Seconds); } - /// Get from * . - public static Force operator *(ForceChangeRate forceChangeRate, TimeSpan timeSpan) - { - return Force.FromNewtons(forceChangeRate.NewtonsPerSecond * timeSpan.TotalSeconds); - } - /// Get from * . public static Force operator *(TimeSpan timeSpan, ForceChangeRate forceChangeRate) { diff --git a/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.g.cs b/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.g.cs index cc7dc19d31..ca95592f63 100644 --- a/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.g.cs @@ -46,7 +46,6 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IMultiplyOperators, - IMultiplyOperators, IMultiplyOperators, IDivisionOperators, #endif @@ -594,12 +593,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Kinem return Area.FromSquareMeters(kinematicViscosity.SquareMetersPerSecond * duration.Seconds); } - /// Get from * . - public static Area operator *(KinematicViscosity kinematicViscosity, TimeSpan timeSpan) - { - return Area.FromSquareMeters(kinematicViscosity.SquareMetersPerSecond * timeSpan.TotalSeconds); - } - /// Get from * . public static Area operator *(TimeSpan timeSpan, KinematicViscosity kinematicViscosity) { diff --git a/UnitsNet/GeneratedCode/Quantities/Length.g.cs b/UnitsNet/GeneratedCode/Quantities/Length.g.cs index e97ae64cdc..563023d25f 100644 --- a/UnitsNet/GeneratedCode/Quantities/Length.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Length.g.cs @@ -50,7 +50,6 @@ namespace UnitsNet IMultiplyOperators, IMultiplyOperators, IDivisionOperators, - IDivisionOperators, IMultiplyOperators, IMultiplyOperators, IMultiplyOperators, @@ -1242,12 +1241,6 @@ public ReciprocalLength Inverse() return Speed.FromMetersPerSecond(length.Meters / duration.Seconds); } - /// Get from / . - public static Speed operator /(Length length, TimeSpan timeSpan) - { - return Speed.FromMetersPerSecond(length.Meters / timeSpan.TotalSeconds); - } - /// Get from * . public static TemperatureDelta operator *(Length length, TemperatureGradient temperatureGradient) { diff --git a/UnitsNet/GeneratedCode/Quantities/Mass.g.cs b/UnitsNet/GeneratedCode/Quantities/Mass.g.cs index 1ff055bda1..721cd69c1a 100644 --- a/UnitsNet/GeneratedCode/Quantities/Mass.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Mass.g.cs @@ -54,7 +54,6 @@ namespace UnitsNet IMultiplyOperators, IDivisionOperators, IDivisionOperators, - IDivisionOperators, IMultiplyOperators, IDivisionOperators, #endif @@ -992,12 +991,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out MassU return MassFlow.FromKilogramsPerSecond(mass.Kilograms / duration.Seconds); } - /// Get from / . - public static MassFlow operator /(Mass mass, TimeSpan timeSpan) - { - return MassFlow.FromKilogramsPerSecond(mass.Kilograms / timeSpan.TotalSeconds); - } - /// Get from * . public static Volume operator *(Mass mass, SpecificVolume specificVolume) { diff --git a/UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs b/UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs index bc6331a141..ae9f0776ce 100644 --- a/UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs @@ -46,7 +46,6 @@ namespace UnitsNet IDivisionOperators, IDivisionOperators, IMultiplyOperators, - IMultiplyOperators, IDivisionOperators, IMultiplyOperators, IDivisionOperators, @@ -1046,12 +1045,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out MassF return Mass.FromKilograms(massFlow.KilogramsPerSecond * duration.Seconds); } - /// Get from * . - public static Mass operator *(MassFlow massFlow, TimeSpan timeSpan) - { - return Mass.FromKilograms(massFlow.KilogramsPerSecond * timeSpan.TotalSeconds); - } - /// Get from * . public static Mass operator *(TimeSpan timeSpan, MassFlow massFlow) { diff --git a/UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs b/UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs index c9bd4c5d32..c450e87e51 100644 --- a/UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs @@ -43,7 +43,6 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IMultiplyOperators, - IMultiplyOperators, IMultiplyOperators, IDivisionOperators, #endif @@ -591,12 +590,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Molar return AmountOfSubstance.FromKilomoles(molarFlow.KilomolesPerSecond * duration.Seconds); } - /// Get from * . - public static AmountOfSubstance operator *(MolarFlow molarFlow, TimeSpan timeSpan) - { - return AmountOfSubstance.FromKilomoles(molarFlow.KilomolesPerSecond * timeSpan.TotalSeconds); - } - /// Get from * . public static AmountOfSubstance operator *(TimeSpan timeSpan, MolarFlow molarFlow) { diff --git a/UnitsNet/GeneratedCode/Quantities/Power.g.cs b/UnitsNet/GeneratedCode/Quantities/Power.g.cs index cfd9e0cf5d..83bd3a9b9e 100644 --- a/UnitsNet/GeneratedCode/Quantities/Power.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Power.g.cs @@ -46,7 +46,6 @@ namespace UnitsNet IDivisionOperators, IDivisionOperators, IMultiplyOperators, - IMultiplyOperators, IDivisionOperators, IDivisionOperators, IMultiplyOperators, @@ -923,12 +922,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Power return Energy.FromJoules(power.Watts * duration.Seconds); } - /// Get from * . - public static Energy operator *(Power power, TimeSpan timeSpan) - { - return Energy.FromJoules(power.Watts * timeSpan.TotalSeconds); - } - /// Get from * . public static Energy operator *(TimeSpan timeSpan, Power power) { diff --git a/UnitsNet/GeneratedCode/Quantities/Pressure.g.cs b/UnitsNet/GeneratedCode/Quantities/Pressure.g.cs index 38e86ad76c..85562db9cc 100644 --- a/UnitsNet/GeneratedCode/Quantities/Pressure.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Pressure.g.cs @@ -47,7 +47,6 @@ namespace UnitsNet IDivisionOperators, IDivisionOperators, IDivisionOperators, - IDivisionOperators, IDivisionOperators, #endif IComparable, @@ -1338,12 +1337,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Press return PressureChangeRate.FromPascalsPerSecond(pressure.Pascals / duration.Seconds); } - /// Get from / . - public static PressureChangeRate operator /(Pressure pressure, TimeSpan timeSpan) - { - return PressureChangeRate.FromPascalsPerSecond(pressure.Pascals / timeSpan.TotalSeconds); - } - /// Get from / . public static SpecificWeight operator /(Pressure pressure, Length length) { diff --git a/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.g.cs b/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.g.cs index 4a857e3e2a..0d72ed6717 100644 --- a/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.g.cs @@ -43,7 +43,6 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IMultiplyOperators, - IMultiplyOperators, #endif IComparable, IComparable, @@ -751,12 +750,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Press return Pressure.FromPascals(pressureChangeRate.PascalsPerSecond * duration.Seconds); } - /// Get from * . - public static Pressure operator *(PressureChangeRate pressureChangeRate, TimeSpan timeSpan) - { - return Pressure.FromPascals(pressureChangeRate.PascalsPerSecond * timeSpan.TotalSeconds); - } - /// Get from * . public static Pressure operator *(TimeSpan timeSpan, PressureChangeRate pressureChangeRate) { diff --git a/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.g.cs b/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.g.cs index 72fb597d12..7a9cd8f736 100644 --- a/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.g.cs @@ -43,7 +43,6 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IMultiplyOperators, - IMultiplyOperators, #endif IComparable, IComparable, @@ -661,12 +660,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Rotat return Angle.FromRadians(rotationalSpeed.RadiansPerSecond * duration.Seconds); } - /// Get from * . - public static Angle operator *(RotationalSpeed rotationalSpeed, TimeSpan timeSpan) - { - return Angle.FromRadians(rotationalSpeed.RadiansPerSecond * timeSpan.TotalSeconds); - } - /// Get from * . public static Angle operator *(TimeSpan timeSpan, RotationalSpeed rotationalSpeed) { diff --git a/UnitsNet/GeneratedCode/Quantities/Speed.g.cs b/UnitsNet/GeneratedCode/Quantities/Speed.g.cs index 06ade9b4bb..fb689d65c5 100644 --- a/UnitsNet/GeneratedCode/Quantities/Speed.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Speed.g.cs @@ -43,11 +43,9 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IDivisionOperators, - IDivisionOperators, IDivisionOperators, IMultiplyOperators, IMultiplyOperators, - IMultiplyOperators, IMultiplyOperators, IMultiplyOperators, IMultiplyOperators, @@ -1029,12 +1027,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Speed return Acceleration.FromMetersPerSecondSquared(speed.MetersPerSecond / duration.Seconds); } - /// Get from / . - public static Acceleration operator /(Speed speed, TimeSpan timeSpan) - { - return Acceleration.FromMetersPerSecondSquared(speed.MetersPerSecond / timeSpan.TotalSeconds); - } - /// Get from / . public static Duration operator /(Speed speed, Acceleration acceleration) { @@ -1053,12 +1045,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Speed return Length.FromMeters(speed.MetersPerSecond * duration.Seconds); } - /// Get from * . - public static Length operator *(Speed speed, TimeSpan timeSpan) - { - return Length.FromMeters(speed.MetersPerSecond * timeSpan.TotalSeconds); - } - /// Get from * . public static Length operator *(TimeSpan timeSpan, Speed speed) { diff --git a/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.g.cs b/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.g.cs index 3a397fbf7c..5b3d1a3dfb 100644 --- a/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.g.cs @@ -43,7 +43,6 @@ namespace UnitsNet IArithmeticQuantity, #if NET7_0_OR_GREATER IMultiplyOperators, - IMultiplyOperators, #endif IComparable, IComparable, @@ -607,12 +606,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Tempe return TemperatureDelta.FromDegreesCelsius(temperatureChangeRate.DegreesCelsiusPerSecond * duration.Seconds); } - /// Get from * . - public static TemperatureDelta operator *(TemperatureChangeRate temperatureChangeRate, TimeSpan timeSpan) - { - return TemperatureDelta.FromDegreesCelsius(temperatureChangeRate.DegreesCelsiusPerSecond * timeSpan.TotalSeconds); - } - /// Get from * . public static TemperatureDelta operator *(TimeSpan timeSpan, TemperatureChangeRate temperatureChangeRate) { diff --git a/UnitsNet/GeneratedCode/Quantities/Volume.g.cs b/UnitsNet/GeneratedCode/Quantities/Volume.g.cs index 0f241b68e8..fc7dbc642c 100644 --- a/UnitsNet/GeneratedCode/Quantities/Volume.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Volume.g.cs @@ -48,7 +48,6 @@ namespace UnitsNet IMultiplyOperators, IMultiplyOperators, IDivisionOperators, - IDivisionOperators, #endif IComparable, IComparable, @@ -1434,12 +1433,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Volum return VolumeFlow.FromCubicMetersPerSecond(volume.CubicMeters / duration.Seconds); } - /// Get from / . - public static VolumeFlow operator /(Volume volume, TimeSpan timeSpan) - { - return VolumeFlow.FromCubicMetersPerSecond(volume.CubicMeters / timeSpan.TotalSeconds); - } - #endregion #region Equality / IComparable diff --git a/UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs b/UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs index a76a9f150a..de921bc96a 100644 --- a/UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs @@ -46,7 +46,6 @@ namespace UnitsNet IMultiplyOperators, IDivisionOperators, IMultiplyOperators, - IMultiplyOperators, #endif IComparable, IComparable, @@ -1660,12 +1659,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Volum return Volume.FromCubicMeters(volumeFlow.CubicMetersPerSecond * duration.Seconds); } - /// Get from * . - public static Volume operator *(VolumeFlow volumeFlow, TimeSpan timeSpan) - { - return Volume.FromCubicMeters(volumeFlow.CubicMetersPerSecond * timeSpan.TotalSeconds); - } - #endregion #region Equality / IComparable From 676ec311e7b7dc737c1c0ed3141b5ab175ebcc33 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 23 Feb 2024 23:46:50 +0100 Subject: [PATCH 4/8] Fix test case, fix regression in Duration.ToTimespan() --- UnitsNet/CustomCode/Quantities/Duration.extra.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitsNet/CustomCode/Quantities/Duration.extra.cs b/UnitsNet/CustomCode/Quantities/Duration.extra.cs index 3a65661f21..5f0f241b43 100644 --- a/UnitsNet/CustomCode/Quantities/Duration.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Duration.extra.cs @@ -14,7 +14,7 @@ public partial struct Duration /// The TimeSpan with the same time as the duration public TimeSpan ToTimeSpan() { - return TimeSpan.FromSeconds(Seconds); + return TimeSpan.FromTicks((long)(Seconds * TimeSpan.TicksPerSecond)); } /// Get from plus . From 5f46257cfd2a9528768bd2a7a5c36e227df21ee2 Mon Sep 17 00:00:00 2001 From: Muximize Date: Sat, 24 Feb 2024 10:42:28 +0100 Subject: [PATCH 5/8] Don't infer TimeSpan from Duration --- CodeGen/Generators/QuantityRelationsParser.cs | 13 ++----------- CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs | 10 ---------- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/CodeGen/Generators/QuantityRelationsParser.cs b/CodeGen/Generators/QuantityRelationsParser.cs index a1c0a08dd2..9c28695af3 100644 --- a/CodeGen/Generators/QuantityRelationsParser.cs +++ b/CodeGen/Generators/QuantityRelationsParser.cs @@ -59,15 +59,6 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities) }) .ToList()); - // We can infer TimeSpan relations from Duration relations. - // Because TimeSpan can be implicitly cast to Duration and vice versa, - // we only need to infer the relations where Duration is the left operand. - var timeSpanQuantity = pseudoQuantity with { Name = "TimeSpan" }; - relations.AddRange(relations - .Where(r => r.LeftQuantity.Name is "Duration") - .Select(r => r with { LeftQuantity = timeSpanQuantity }) - .ToList()); - // Sort all relations to keep generated operators in a consistent order. relations.Sort(); @@ -94,9 +85,9 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities) // The left operand of a relation is responsible for generating the operator. quantityRelations.Add(relation); } - else if (relation.RightQuantity == quantity && relation.LeftQuantity.Name is "double" or "TimeSpan") + else if (relation.RightQuantity == quantity && relation.LeftQuantity.Name is "double") { - // Because we cannot add generated operators to double or TimeSpan, we make the right operand responsible in this case. + // Because we cannot add operators to double we make the right operand responsible in this case. quantityRelations.Add(relation); } } diff --git a/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs b/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs index e0d5f41066..cec73cdefb 100644 --- a/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs +++ b/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs @@ -748,16 +748,6 @@ private void GenerateRelationalOperators() var rightParameter = relation.RightQuantity.Name.ToCamelCase(); var rightConversionProperty = relation.RightUnit.PluralName; - if (relation.LeftQuantity.Name is nameof(TimeSpan)) - { - leftConversionProperty = "Total" + leftConversionProperty; - } - - if (relation.RightQuantity.Name is nameof(TimeSpan)) - { - rightConversionProperty = "Total" + rightConversionProperty; - } - if (leftParameter == rightParameter) { leftParameter = "left"; From da2e6fea7f8224081b30bd601723296f3d453e67 Mon Sep 17 00:00:00 2001 From: Muximize Date: Sat, 24 Feb 2024 10:43:43 +0100 Subject: [PATCH 6/8] Remove tests where TimeSpan is left operand. --- UnitsNet.Tests/CustomCode/KinematicViscosityTests.cs | 7 ------- UnitsNet.Tests/CustomCode/MassFlowTests.cs | 7 ------- UnitsNet.Tests/CustomCode/PowerTests.cs | 7 ------- UnitsNet.Tests/CustomCode/RotationalSpeedTests.cs | 7 ------- UnitsNet.Tests/CustomCode/SpeedTests.cs | 7 ------- UnitsNet.Tests/CustomCode/TemperatureChangeRateTests.cs | 7 ------- 6 files changed, 42 deletions(-) diff --git a/UnitsNet.Tests/CustomCode/KinematicViscosityTests.cs b/UnitsNet.Tests/CustomCode/KinematicViscosityTests.cs index d10e48c3a8..f1a487b86e 100644 --- a/UnitsNet.Tests/CustomCode/KinematicViscosityTests.cs +++ b/UnitsNet.Tests/CustomCode/KinematicViscosityTests.cs @@ -56,13 +56,6 @@ public static void KinematicViscosityTimesTimeSpanEqualsArea() Assert.Equal(area, Area.FromSquareMeters(8)); } - [Fact] - public static void TimeSpanTimesKinematicViscosityEqualsArea() - { - Area area = TimeSpan.FromSeconds(2)*KinematicViscosity.FromSquareMetersPerSecond(4); - Assert.Equal(area, Area.FromSquareMeters(8)); - } - [Fact] public static void KinematicViscosityTimesDensityEqualsDynamicViscosity() { diff --git a/UnitsNet.Tests/CustomCode/MassFlowTests.cs b/UnitsNet.Tests/CustomCode/MassFlowTests.cs index 3c0ca9836b..19fe286228 100644 --- a/UnitsNet.Tests/CustomCode/MassFlowTests.cs +++ b/UnitsNet.Tests/CustomCode/MassFlowTests.cs @@ -97,13 +97,6 @@ public void MassFlowTimesTimeSpanEqualsMass() Assert.Equal(mass, Mass.FromKilograms(80.0)); } - [Fact] - public void TimeSpanTimesMassFlowEqualsMass() - { - Mass mass = TimeSpan.FromSeconds(4.0) * MassFlow.FromKilogramsPerSecond(20.0); - Assert.Equal(mass, Mass.FromKilograms(80.0)); - } - [Fact] public void MassFlowDividedByBrakeSpecificFuelConsumptionEqualsPower() { diff --git a/UnitsNet.Tests/CustomCode/PowerTests.cs b/UnitsNet.Tests/CustomCode/PowerTests.cs index 1b80aad460..d55b2e2e21 100644 --- a/UnitsNet.Tests/CustomCode/PowerTests.cs +++ b/UnitsNet.Tests/CustomCode/PowerTests.cs @@ -104,13 +104,6 @@ public void PowerTimesTimeSpanEqualsEnergy() Assert.Equal(energy, Energy.FromJoules(40.0)); } - [Fact] - public void TimeSpanTimesPowerEqualsEnergy() - { - Energy energy = TimeSpan.FromSeconds(8.0) * Power.FromWatts(5.0); - Assert.Equal(energy, Energy.FromJoules(40.0)); - } - [Fact] public void PowerTimesBrakeSpecificFuelConsumptionEqualsMassFlow() { diff --git a/UnitsNet.Tests/CustomCode/RotationalSpeedTests.cs b/UnitsNet.Tests/CustomCode/RotationalSpeedTests.cs index fcfb11b2db..e6be52c886 100644 --- a/UnitsNet.Tests/CustomCode/RotationalSpeedTests.cs +++ b/UnitsNet.Tests/CustomCode/RotationalSpeedTests.cs @@ -55,12 +55,5 @@ public void RotationalSpeedTimesTimeSpanEqualsAngle() Angle angle = RotationalSpeed.FromRadiansPerSecond(10.0)*TimeSpan.FromSeconds(9.0); Assert.Equal(angle, Angle.FromRadians(90.0)); } - - [Fact] - public void TimeSpanTimesRotationalSpeedEqualsAngle() - { - Angle angle = TimeSpan.FromSeconds(9.0)*RotationalSpeed.FromRadiansPerSecond(10.0); - Assert.Equal(angle, Angle.FromRadians(90.0)); - } } } diff --git a/UnitsNet.Tests/CustomCode/SpeedTests.cs b/UnitsNet.Tests/CustomCode/SpeedTests.cs index 629c61358e..068af70c58 100644 --- a/UnitsNet.Tests/CustomCode/SpeedTests.cs +++ b/UnitsNet.Tests/CustomCode/SpeedTests.cs @@ -131,13 +131,6 @@ public void SpeedTimesTimeSpanEqualsLength() Assert.Equal(length, Length.FromMeters(40)); } - [Fact] - public void TimeSpanTimesSpeedEqualsLength() - { - Length length = TimeSpan.FromSeconds(2)*Speed.FromMetersPerSecond(20); - Assert.Equal(length, Length.FromMeters(40)); - } - [Fact] public void SpeedTimesLengthEqualsKinematicViscosity() { diff --git a/UnitsNet.Tests/CustomCode/TemperatureChangeRateTests.cs b/UnitsNet.Tests/CustomCode/TemperatureChangeRateTests.cs index e4c0da0547..fe250c327b 100644 --- a/UnitsNet.Tests/CustomCode/TemperatureChangeRateTests.cs +++ b/UnitsNet.Tests/CustomCode/TemperatureChangeRateTests.cs @@ -36,13 +36,6 @@ public void TemperatureChangeRateMultipliedWithTimeSpanEqualsTemperatureDelta() Assert.Equal(TemperatureDelta.FromDegreesCelsius(20), d); } - [Fact] - public void TimeSpanMultipliedWithTemperatureChangeRateEqualsTemperatureDelta() - { - TemperatureDelta d = new TimeSpan(0, 0, -10) * TemperatureChangeRate.FromDegreesCelsiusPerSecond(2); - Assert.Equal(TemperatureDelta.FromDegreesCelsius(-20), d); - } - [Fact] public void TemperatureChangeRateMultipliedWithDurationEqualsTemperatureDelta() { From 4971e89dfb54ded75f2d6c9b328383719664b380 Mon Sep 17 00:00:00 2001 From: Muximize Date: Sat, 24 Feb 2024 10:44:20 +0100 Subject: [PATCH 7/8] Generate code --- UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/ElectricCurrent.g.cs | 6 ------ .../GeneratedCode/Quantities/ElectricCurrentGradient.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/ForceChangeRate.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/KinematicViscosity.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/Power.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/PressureChangeRate.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/RotationalSpeed.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/Speed.g.cs | 6 ------ .../GeneratedCode/Quantities/TemperatureChangeRate.g.cs | 6 ------ UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs | 6 ------ 13 files changed, 78 deletions(-) diff --git a/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs b/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs index 3ccf8842ad..0c3d580a62 100644 --- a/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs @@ -685,12 +685,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Accel return Speed.FromMetersPerSecond(acceleration.MetersPerSecondSquared * duration.Seconds); } - /// Get from * . - public static Speed operator *(TimeSpan timeSpan, Acceleration acceleration) - { - return Speed.FromMetersPerSecond(timeSpan.TotalSeconds * acceleration.MetersPerSecondSquared); - } - #endregion #region Equality / IComparable diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.g.cs index cadaa98477..c411db4ff3 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.g.cs @@ -582,12 +582,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Elect return ElectricCharge.FromAmpereHours(electricCurrent.Amperes * duration.Hours); } - /// Get from * . - public static ElectricCharge operator *(TimeSpan timeSpan, ElectricCurrent electricCurrent) - { - return ElectricCharge.FromAmpereHours(timeSpan.TotalHours * electricCurrent.Amperes); - } - /// Get from / . public static ElectricCurrentGradient operator /(ElectricCurrent electricCurrent, Duration duration) { diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.g.cs index 9770434e19..4b11beec0e 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.g.cs @@ -545,12 +545,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Elect return ElectricCurrent.FromAmperes(electricCurrentGradient.AmperesPerSecond * duration.Seconds); } - /// Get from * . - public static ElectricCurrent operator *(TimeSpan timeSpan, ElectricCurrentGradient electricCurrentGradient) - { - return ElectricCurrent.FromAmperes(timeSpan.TotalSeconds * electricCurrentGradient.AmperesPerSecond); - } - #endregion #region Equality / IComparable diff --git a/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.g.cs b/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.g.cs index ed6a67a673..2aa95e2782 100644 --- a/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.g.cs @@ -681,12 +681,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Force return Force.FromNewtons(forceChangeRate.NewtonsPerSecond * duration.Seconds); } - /// Get from * . - public static Force operator *(TimeSpan timeSpan, ForceChangeRate forceChangeRate) - { - return Force.FromNewtons(timeSpan.TotalSeconds * forceChangeRate.NewtonsPerSecond); - } - #endregion #region Equality / IComparable diff --git a/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.g.cs b/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.g.cs index 6f294bf8f7..0685d71283 100644 --- a/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.g.cs @@ -584,12 +584,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Kinem return Area.FromSquareMeters(kinematicViscosity.SquareMetersPerSecond * duration.Seconds); } - /// Get from * . - public static Area operator *(TimeSpan timeSpan, KinematicViscosity kinematicViscosity) - { - return Area.FromSquareMeters(timeSpan.TotalSeconds * kinematicViscosity.SquareMetersPerSecond); - } - /// Get from * . public static DynamicViscosity operator *(KinematicViscosity kinematicViscosity, Density density) { diff --git a/UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs b/UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs index 4a2e9a7a34..e9dc09a28a 100644 --- a/UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MassFlow.g.cs @@ -1012,12 +1012,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out MassF return Mass.FromKilograms(massFlow.KilogramsPerSecond * duration.Seconds); } - /// Get from * . - public static Mass operator *(TimeSpan timeSpan, MassFlow massFlow) - { - return Mass.FromKilograms(timeSpan.TotalSeconds * massFlow.KilogramsPerSecond); - } - /// Get from / . public static MassFlux operator /(MassFlow massFlow, Area area) { diff --git a/UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs b/UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs index 1253ce5b7a..ca27ebcf52 100644 --- a/UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MolarFlow.g.cs @@ -581,12 +581,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Molar return AmountOfSubstance.FromKilomoles(molarFlow.KilomolesPerSecond * duration.Seconds); } - /// Get from * . - public static AmountOfSubstance operator *(TimeSpan timeSpan, MolarFlow molarFlow) - { - return AmountOfSubstance.FromKilomoles(timeSpan.TotalSeconds * molarFlow.KilomolesPerSecond); - } - /// Get from * . public static MassFlow operator *(MolarFlow molarFlow, MolarMass molarMass) { diff --git a/UnitsNet/GeneratedCode/Quantities/Power.g.cs b/UnitsNet/GeneratedCode/Quantities/Power.g.cs index 250fcff699..5b76af233d 100644 --- a/UnitsNet/GeneratedCode/Quantities/Power.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Power.g.cs @@ -896,12 +896,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Power return Energy.FromJoules(power.Watts * duration.Seconds); } - /// Get from * . - public static Energy operator *(TimeSpan timeSpan, Power power) - { - return Energy.FromJoules(timeSpan.TotalSeconds * power.Watts); - } - /// Get from / . public static Force operator /(Power power, Speed speed) { diff --git a/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.g.cs b/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.g.cs index 9e6c181b01..02e85f47ae 100644 --- a/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.g.cs @@ -732,12 +732,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Press return Pressure.FromPascals(pressureChangeRate.PascalsPerSecond * duration.Seconds); } - /// Get from * . - public static Pressure operator *(TimeSpan timeSpan, PressureChangeRate pressureChangeRate) - { - return Pressure.FromPascals(timeSpan.TotalSeconds * pressureChangeRate.PascalsPerSecond); - } - #endregion #region Equality / IComparable diff --git a/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.g.cs b/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.g.cs index 8046f507e0..faa4a01de7 100644 --- a/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.g.cs @@ -647,12 +647,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Rotat return Angle.FromRadians(rotationalSpeed.RadiansPerSecond * duration.Seconds); } - /// Get from * . - public static Angle operator *(TimeSpan timeSpan, RotationalSpeed rotationalSpeed) - { - return Angle.FromRadians(timeSpan.TotalSeconds * rotationalSpeed.RadiansPerSecond); - } - #endregion #region Equality / IComparable diff --git a/UnitsNet/GeneratedCode/Quantities/Speed.g.cs b/UnitsNet/GeneratedCode/Quantities/Speed.g.cs index 08b02e3374..26a37d3360 100644 --- a/UnitsNet/GeneratedCode/Quantities/Speed.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Speed.g.cs @@ -1012,12 +1012,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Speed return Length.FromMeters(speed.MetersPerSecond * duration.Seconds); } - /// Get from * . - public static Length operator *(TimeSpan timeSpan, Speed speed) - { - return Length.FromMeters(timeSpan.TotalSeconds * speed.MetersPerSecond); - } - /// Get from * . public static MassFlux operator *(Speed speed, Density density) { diff --git a/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.g.cs b/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.g.cs index c06b58e157..0854e80f9a 100644 --- a/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.g.cs @@ -596,12 +596,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Tempe return TemperatureDelta.FromDegreesCelsius(temperatureChangeRate.DegreesCelsiusPerSecond * duration.Seconds); } - /// Get from * . - public static TemperatureDelta operator *(TimeSpan timeSpan, TemperatureChangeRate temperatureChangeRate) - { - return TemperatureDelta.FromDegreesCelsius(timeSpan.TotalSeconds * temperatureChangeRate.DegreesCelsiusPerSecond); - } - #endregion #region Equality / IComparable diff --git a/UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs b/UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs index 1711406686..98ad245221 100644 --- a/UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs @@ -1580,12 +1580,6 @@ public static bool TryParseUnit(string str, IFormatProvider? provider, out Volum return Speed.FromMetersPerSecond(volumeFlow.CubicMetersPerSecond / area.SquareMeters); } - /// Get from * . - public static Volume operator *(TimeSpan timeSpan, VolumeFlow volumeFlow) - { - return Volume.FromCubicMeters(timeSpan.TotalSeconds * volumeFlow.CubicMetersPerSecond); - } - /// Get from * . public static Volume operator *(VolumeFlow volumeFlow, Duration duration) { From d8c9ed1f1df484f7c12def0448e8794054f3c0bb Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Mon, 26 Feb 2024 07:29:31 +0100 Subject: [PATCH 8/8] Add back Min/Max checks in ToTimeSpan() --- UnitsNet.Tests/CustomCode/DurationTests.cs | 14 ++++++++++++++ UnitsNet/CustomCode/Quantities/Duration.extra.cs | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/UnitsNet.Tests/CustomCode/DurationTests.cs b/UnitsNet.Tests/CustomCode/DurationTests.cs index 088c87c33c..eda0611105 100644 --- a/UnitsNet.Tests/CustomCode/DurationTests.cs +++ b/UnitsNet.Tests/CustomCode/DurationTests.cs @@ -33,6 +33,20 @@ public class DurationTests : DurationTestsBase protected override double JulianYearsInOneSecond => 3.16880878140289e-08; + [Fact] + public static void ToTimeSpanShouldThrowExceptionOnValuesLargerThanTimeSpanMax() + { + Duration duration = Duration.FromSeconds(TimeSpan.MaxValue.TotalSeconds + 1); + Assert.Throws(() => duration.ToTimeSpan()); + } + + [Fact] + public static void ToTimeSpanShouldThrowExceptionOnValuesSmallerThanTimeSpanMin() + { + Duration duration = Duration.FromSeconds(TimeSpan.MinValue.TotalSeconds - 1); + Assert.Throws(() => duration.ToTimeSpan()); + } + [Fact] public static void ToTimeSpanShouldNotThrowExceptionOnValuesSlightlyLargerThanTimeSpanMin() { diff --git a/UnitsNet/CustomCode/Quantities/Duration.extra.cs b/UnitsNet/CustomCode/Quantities/Duration.extra.cs index 5f0f241b43..b72b51285a 100644 --- a/UnitsNet/CustomCode/Quantities/Duration.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Duration.extra.cs @@ -11,9 +11,19 @@ public partial struct Duration /// /// Convert a Duration to a TimeSpan. /// + /// + /// Throws if the duration exceeds the . or + /// , which would cause it to roll over from positive to negative and vice versa. + /// /// The TimeSpan with the same time as the duration public TimeSpan ToTimeSpan() { + if (Seconds > TimeSpan.MaxValue.TotalSeconds) throw new ArgumentOutOfRangeException(nameof(Seconds), + "The duration is too large for a TimeSpan, which would roll over from positive to negative."); + + if (Seconds < TimeSpan.MinValue.TotalSeconds) throw new ArgumentOutOfRangeException(nameof(Seconds), + "The duration is too small for a TimeSpan, which would roll over from negative to positive."); + return TimeSpan.FromTicks((long)(Seconds * TimeSpan.TicksPerSecond)); }