From: Jon Hanna Date: Wed, 1 Mar 2017 16:53:33 +0000 (+0000) Subject: Timespan division and multiplication. (dotnet/coreclr#9804) X-Git-Tag: submit/tizen/20210909.063632~11030^2~7897 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a085a0c025ae388583f750c2eef57f2de056b82c;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Timespan division and multiplication. (dotnet/coreclr#9804) https://github.com/dotnet/corefx/issues/7175 Commit migrated from https://github.com/dotnet/coreclr/commit/6edb2a5dfa302ba78d8b6e535249accbca3c90d5 --- diff --git a/src/coreclr/src/mscorlib/src/System/TimeSpan.cs b/src/coreclr/src/mscorlib/src/System/TimeSpan.cs index f7c864d..cd7b454 100644 --- a/src/coreclr/src/mscorlib/src/System/TimeSpan.cs +++ b/src/coreclr/src/mscorlib/src/System/TimeSpan.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -405,6 +405,48 @@ namespace System return t1.Add(t2); } + public static TimeSpan operator *(TimeSpan timeSpan, double factor) + { + if (double.IsNaN(factor)) + { + throw new ArgumentException(Environment.GetResourceString("Arg_CannotBeNaN"), nameof(factor)); + } + + // Rounding to the nearest tick is as close to the result we would have with unlimited + // precision as possible, and so likely to have the least potential to surprise. + double ticks = Math.Round(timeSpan.Ticks * factor); + if (ticks > long.MaxValue | ticks < long.MinValue) + { + throw new OverflowException(Environment.GetResourceString("Overflow_TimeSpanTooLong")); + } + + return FromTicks((long)ticks); + } + + public static TimeSpan operator *(double factor, TimeSpan timeSpan) => timeSpan * factor; + + public static TimeSpan operator /(TimeSpan timeSpan, double divisor) + { + if (double.IsNaN(divisor)) + { + throw new ArgumentException(Environment.GetResourceString("Arg_CannotBeNaN"), nameof(divisor)); + } + + double ticks = Math.Round(timeSpan.Ticks / divisor); + if (ticks > long.MaxValue | ticks < long.MinValue || double.IsNaN(ticks)) + { + throw new OverflowException(Environment.GetResourceString("Overflow_TimeSpanTooLong")); + } + + return FromTicks((long)ticks); + } + + // Using floating-point arithmetic directly means that infinities can be returned, which is reasonable + // if we consider TimeSpan.FromHours(1) / TimeSpan.Zero asks how many zero-second intervals there are in + // an hour for which ∞ is the mathematic correct answer. Having TimeSpan.Zero / TimeSpan.Zero return NaN + // is perhaps less useful, but no less useful than an exception. + public static double operator /(TimeSpan t1, TimeSpan t2) => t1.Ticks / (double)t2.Ticks; + public static bool operator ==(TimeSpan t1, TimeSpan t2) { return t1._ticks == t2._ticks;